Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write best practices and style guide #904

Merged
merged 38 commits into from
Sep 30, 2023
Merged
Changes from 15 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9c2037d
feat(practices): options and parameters
May 10, 2023
efc10cb
feat(practices): require docs for exported things
May 11, 2023
1338aac
feat(practices): explain formatting
May 11, 2023
1b63ecf
feat(practices): add example
May 11, 2023
d649d4a
feat(practices): extend first formatting rule
May 11, 2023
89bf85b
feat(practices): rewrite formatting rules
May 11, 2023
1e8c1d8
feat(practices): add examples
May 11, 2023
062e1d0
feat(practices): note about keeping indent
May 11, 2023
ecfb91c
feat(practices): add list formatting
May 11, 2023
8980677
feat(practices): make rules sound more polite
May 11, 2023
d8aca9f
feat(practices): add note
May 11, 2023
ef39d60
feat(practices): clarify one-line format rules
May 11, 2023
3fdc3d3
feat(practice): clean-up
May 11, 2023
aedbc9c
fix(practices): remove duplicates
May 11, 2023
930df0d
feat(practices): update examples
May 11, 2023
0639f71
feat(practices): recommend omit commas
May 11, 2023
ca6ce66
feat(practices): allow short format more
May 11, 2023
a2dac57
feat(practices): make intro less authoritative
May 13, 2023
b8de74f
feat(practices): move comments on separate lines
May 13, 2023
d18a100
feat(practices): note about escape sequences
May 13, 2023
aef4ac8
feat(practices): unify collection syntax
May 13, 2023
1cfefea
feat(practices): narrow down amount of args
May 13, 2023
6a22d9d
fix(practices): deduplicate terms
May 13, 2023
5efba81
fix(practices): require trailing space omission
May 13, 2023
c9f61d2
tweaked some grammar, removed some optional commas
fdncred May 13, 2023
cd765c2
feat(practices): join sections
May 14, 2023
2ffc8ab
Merge branch 'feature/add-best-practices' of https://github.com/Emily…
May 14, 2023
6852d1e
feat(practices): clarify when use and what format
May 14, 2023
80af021
feat(practices): update examples
May 17, 2023
4269691
feat(practices): dealing with parenthesis
May 17, 2023
300cf67
feat(practices): forbid consecutive spaces
May 17, 2023
4b47366
feat(practices): add spacing between constructs
May 17, 2023
88565ff
feat(practices): simplify rules
May 17, 2023
d969521
feat(practices): clarify rules for parenthesis
May 17, 2023
ebf66a4
feat(practices): spacing when no params defined
May 17, 2023
3616f66
Update book/style_guide.md
May 17, 2023
8d01d4f
Merge branch 'main' into feature/add-best-practices
Jun 18, 2023
a433170
Merge branch 'feature/add-best-practices' of https://github.com/Emily…
Jun 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 203 additions & 0 deletions book/style_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# Best practices

This page lists our best practices we advice to follow. Keep in mind that they are not required to be
used in external repositories (not ours), you can change them in the way you want,
but please be consistent and follow your rules.

## Formatting

### Defaults

**It's recommended to** assume that by default no spaces or tabs allowed, but the following rules define where they are allowed.

### Basic

**It's recommended to** put one space before and after pipe `|` symbol, commands, subcommands, their options and arguments.

Correct:

```nushell
'Hello, Nushell! This is a gradient.' | ansi gradient --fgstart '0x40c9ff' --fgend '0xe81cff'
```

Incorrect:

```nushell
'Hello, Nushell! This is a gradient.' | ansi gradient --fgstart '0x40c9ff' --fgend '0xe81cff' # two many spaces after "|": 2 instead of 1
```
amtoine marked this conversation as resolved.
Show resolved Hide resolved

#### One-line format

One-line format is a format for writing all commands in one line.

1. parameters:
1. **It's recommended to** put no spaces before and after pipe `|` symbol denoting block or closure parameter list beginning.
2. **It's recommended to** put one space after comma `,` after block or closure parameter parameter.
amtoine marked this conversation as resolved.
Show resolved Hide resolved
3. **It's recommended to** put no space before and one space after pipe `|` symbol denoting block or closure parameter list end.
2. block and closure bodies:
1. **It's recommended to** put one space before closing block or closure curly brace `}`.
3. records:
1. **It's recommended to** put one space before first record key and after last record key value.
amtoine marked this conversation as resolved.
Show resolved Hide resolved
2. **It's recommended to** put one space after `:` after record key.
3. **It's recommended to** put one space after comma `,` after key value.
4. lists:
1. **It's recommended to** put no spaces before first list value and after last list value.
2. **It's recommended to** put one space after comma `,` after list value.
5. surrounding constructs:
1. **It's recommended to** put one space before opening square `[` or curly brace `{` if preceding symbol is not the same.
2. **It's recommended to** put one space after closing square `]` or curly brace `}` if following symbol is not the same.
3. **It's recommended to** put no spaces between square `[]` or curly brackets `{}` with nothing between them.

Correct:

```nushell
[[status]; [UP] [UP]] | all {|el| $el.status == UP }
[1 2 3 4] | reduce {|it, acc| $it + $acc }
{ x: 1, y: 2 }
[1 2] | zip [3 4]
[]
```

Incorrect:

```nushell
[[status]; [UP] [UP]] | all { |el| $el.status == UP } # two many spaces before "|el|": 2 instead of 0
[1 2 3 4] | reduce {|it , acc | $it + $acc } # two many spaces after "it": 1 instead of 0
# two many spaces before "| $it + $acc": 1 instead of 0
{x: 1, y : 2 } # two few spaces before "x: 1": 0 instead of 1
# two many spaces before ": 2": 1 instead of 0
[1 2] | zip [3 4] # two many spaces before "[3 4]": 2 instead of 1
[ ] # two many spaces before "]": 1 instead of 0
```

#### Multi-line format

Multi-line format is a format for writing all commands in several lines. It inherits all rules from one-line format
and modifies them slightly.

1. general:
1. **It's recommended to omit** trailing spaces.
amtoine marked this conversation as resolved.
Show resolved Hide resolved
2. block and closure bodies:
1. **It's recommended to** put each body pipeline on a separate line.
3. records:
1. **It's recommended to** put each record key-value pair on separate line.
4. lists:
1. **It's recommended to** put each list item on separate line.
5. surrounding constructs:
1. **It's recommended to** put one `\n` before opening square `[` or curly brace `{` if preceding symbol is not the same.
2. **It's recommended to** put one `\n` after closing square `]` or curly brace `}` if following symbol is not the same.
amtoine marked this conversation as resolved.
Show resolved Hide resolved

Correct:

```nushell
[[status]; [UP] [UP]] | all {|el|
$el.status == UP
}

[
1
2
3
4
] | reduce {|it, acc|
$it + $acc
}
amtoine marked this conversation as resolved.
Show resolved Hide resolved

{
x: 1,
y: 2
}

[
{
name: "Teresa",
age: 24
},
{
name: "Thomas",
age: 26
}
]
```

Incorrect:

```nushell
[[status]; [UP] [UP]] | all {|el|
$el.status == UP } # two many spaces before "}": 1 instead of "\n"

[ 1 # two many spaces before "1": 1 instead of "\n"
2
3
4
] | reduce {|it, acc|
$it + $acc
}

{ x: 1, # two many spaces before "x: 1": 1 instead of "\n"
y: 2
}

[{ # two few "\n" before "{": 0 instead of 1
name: "Teresa",
age: 24
} , # two many spaces before ",": 1 instead of 0
{
name: "Thomas",
age: 26
}
]
```

### Spreading long lines
fdncred marked this conversation as resolved.
Show resolved Hide resolved

- **It's recommended to** default to short format unless you are writing scripts.
- **It's recommended to** default to short format in scripts for lists unless they more than 80 characters long.
- **It's recommended to** use long format for pipelines more than 80 characters long.

Correct (in scripts):

```nushell
[1, 2, 3, 4] | reduce {|it, acc|
$it + $acc
}

[1 2 3 4] | reduce {|it acc|
$it + $acc
}
```

Incorrect (in scripts):

```nushell
[
1,
2,
3,
4
] | reduce {|it, acc|
$it + $acc
}

[
1
2
3
4
] | reduce {|it acc|
$it + $acc
}
```

## Options and parameters of custom commands

- **It's recommended to** keep count of all positional parameters less than or equal to 4, for remaining inputs use options.
amtoine marked this conversation as resolved.
Show resolved Hide resolved
- **It's recommended to** use positional parameters unless they can't be used due to rules listed here or technical restrictions.
For instance, when there are several kinds of optional parameters (but at least one parameter should be provided)
use options. Great example of this is `ansi gradient` command where at least foreground or background must be passed.
- **It's recommended to** provide both long and short options.

## Documentation

- **It's recommended to** provide documentation for all exported entities (like custom commands) and their
inputs (like custom command parameters and options).