Skip to content

Commit 34969b5

Browse files
committed
docs: style guide of functions with destructuring
[skip ci]
1 parent 7f0ddda commit 34969b5

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

STYLE.md

+149
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,155 @@ and produces **clean diffs**.
66
This means trading aggressively compact code
77
for regularity and ease of modification.
88

9+
## Function
10+
11+
### With Destructured Arguments
12+
13+
✅ Good:
14+
15+
```nix
16+
{mkDerivation, ...} @ attrs:
17+
mkDerivation # ...
18+
```
19+
20+
- Indenting the body relative to the function signature
21+
hints that a new scope is introduced by the
22+
function arguments.
23+
- Keeping the signature in one line
24+
when there is only 1 argument in the destructuring (`mkDerivation`)
25+
helps saving vertical space.
26+
- Spacing between elements of the destructuring,
27+
and between opening and closing elements
28+
is consistent with _List_ and _Map_.
29+
30+
✅ Good:
31+
32+
```nix
33+
{mkDerivation, ...} @ attrs:
34+
mkDerivation # ...
35+
```
36+
37+
- When there is only 1 function in the whole file
38+
it's valid not to indent the body
39+
because it's clear when reading the file from top to bottom
40+
that the whole remaining of the file
41+
is the scope of the function,
42+
Therefore saving an unneeded indent.
43+
44+
✅ Good:
45+
46+
```nix
47+
{
48+
mkDerivation,
49+
lib,
50+
fetchurl,
51+
...
52+
} @ attrs:
53+
stdenv.mkDerivation # ...
54+
```
55+
56+
- Adding an argument produces a minimal diff
57+
(including the first and last elements):
58+
59+
```patch
60+
mkDerivation,
61+
lib,
62+
fetchurl,
63+
+ google-chrome-stable,
64+
```
65+
66+
- Removing an argument produces a minimal diff
67+
(including the first and last elements):
68+
69+
```patch
70+
mkDerivation,
71+
- lib,
72+
fetchurl,
73+
```
74+
75+
- The comma at the end is consistent with _Let-In_, and _Map_,
76+
where the separator goes after the element
77+
instead of at the beginning.
78+
79+
❌ Bad:
80+
81+
<!-- nixpkgs-fmt -->
82+
83+
```nix
84+
{ lib
85+
, mkDerivation
86+
, fetchurl
87+
, ...
88+
} @ attrs:
89+
stdenv.mkDerivation # ...
90+
```
91+
92+
- Removing the first element
93+
produces a diff in two elements:
94+
95+
```diff
96+
- { lib
97+
- , mkDerivation
98+
+ { mkDerivation
99+
, fetchurl
100+
, ...
101+
} @ attrs:
102+
stdenv.mkDerivation # ...
103+
```
104+
105+
- Documenting the first argument creates an inconsistency
106+
between the way argument start:
107+
108+
```nix
109+
{
110+
# Lorem Ipsum
111+
lib
112+
, mkDerivation
113+
, fetchurl
114+
, ...
115+
} @ attrs:
116+
stdenv.mkDerivation # ...
117+
```
118+
119+
- This is not consistent with _Let-In_, and _Map_,
120+
where the separator goes after the element
121+
instead of at the beginning.
122+
123+
❌ Bad:
124+
125+
<!-- nixfmt -->
126+
127+
```nix
128+
{ mkDerivation, lib, fetchurl, ... }@attrs: stdenv.mkDerivation # ...
129+
```
130+
131+
- One-liners are unreadable.
132+
133+
❌ Bad:
134+
135+
<!-- nixfmt -->
136+
137+
```nix
138+
{ mkDerivation, lib, fetchurl, extra-cmake-modules, kdoctools, wrapGAppsHook
139+
, karchive, kconfig, kcrash, kguiaddons, kinit, kparts, kwind, ... }@attrs:
140+
stdenv.mkDerivation # ...
141+
```
142+
143+
- It's hard to tell this destructuring has an ellipsis (`...`) at a first glance,
144+
because it's mixed with the other arguments.
145+
- Moving elements becomes harder
146+
than a simple whole-line movement.
147+
(Moving a whole line is normally a keyboard-shortcut
148+
or command in major code editors).
149+
- Excessively compact:
150+
adding, removing, or editing an argument
151+
produces a diff in more than one argument.
152+
- `}@attrs` is not intuitive
153+
with the rules of written english,
154+
where you add whitespace
155+
after the end of the previous phrase
156+
(`phrase. Other phrase`).
157+
9158
## If-Then-Else
10159

11160
✅ Good:

0 commit comments

Comments
 (0)