docs(nix): split let binding rules
This commit is contained in:
34
docs/nix.md
34
docs/nix.md
@@ -131,11 +131,10 @@ Use:
|
||||
|
||||
## `let` Bindings
|
||||
|
||||
### Use `let` to name concepts
|
||||
### Name the role, not the type
|
||||
|
||||
Use a `let` binding when the name explains the value's role.
|
||||
Use a `let` binding when multiple expressions share one important value.
|
||||
Do not use a `let` binding only to shorten a function call.
|
||||
Do not introduce a vague name only to shorten an expression.
|
||||
|
||||
Avoid:
|
||||
|
||||
@@ -155,11 +154,36 @@ in
|
||||
lib.genAttrs hostNames mkHost
|
||||
```
|
||||
|
||||
### Keep `let` bindings near their use
|
||||
### Share important repeated values
|
||||
|
||||
Use a `let` binding when multiple expressions share one important value.
|
||||
Do not repeat an expression when repetition makes the relationship less clear.
|
||||
|
||||
Avoid:
|
||||
|
||||
```nix
|
||||
{
|
||||
hostNames = lib.attrNames hosts;
|
||||
hostCount = lib.length (lib.attrNames hosts);
|
||||
}
|
||||
```
|
||||
|
||||
Use:
|
||||
|
||||
```nix
|
||||
let
|
||||
hostNames = lib.attrNames hosts;
|
||||
in
|
||||
{
|
||||
inherit hostNames;
|
||||
hostCount = lib.length hostNames;
|
||||
}
|
||||
```
|
||||
|
||||
### Keep bindings near their use
|
||||
|
||||
Place a `let` binding in the smallest expression that needs it.
|
||||
Do not lift a value to a larger scope only for convenience.
|
||||
A larger scope makes the dependency look more important than it is.
|
||||
|
||||
Avoid:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user