Skip to content

Commit 489850e

Browse files
committed
docs: add style guide
[skip ci]
1 parent 609d1e1 commit 489850e

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@
7777

7878
Beauty is subjective, right?
7979

80-
We started from the wisdom of the crowd,
81-
which comes in big part
82-
from the 2.3 million lines of code of [Nixpkgs](https://github.com/NixOS/nixpkgs).
83-
Then we applied the feedback of developers
84-
who have used [Nix](https://nixos.org) on a day to day basis for several years.
80+
We started from the original style of
81+
[Nixpkgs](https://github.com/NixOS/nixpkgs),
82+
and then we applied the feedback of developers
83+
who have used [Nix](https://nixos.org) at scale
84+
for several years.
8585

8686
- ✔️ **Transparent**
8787

STYLE.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Alejandra's Style Guide
2+
3+
Alejandra's mission is to produce a **consistent style**
4+
that is **easy to read**
5+
and produces **clean diffs**.
6+
This means trading aggressively compact code
7+
for regularity and ease of modification.
8+
9+
## If-Then-Else
10+
11+
✅ Good:
12+
13+
```nix
14+
if predicate
15+
then foo
16+
else bar
17+
```
18+
19+
- The keyword at the beginning of the line
20+
states clearly the meaning of the content that follows.
21+
- Produces a clean diff when you add more code.
22+
For example: adding content to the `else`
23+
only produces a diff in the `else`.
24+
25+
❌ Bad:
26+
27+
<!-- nixfmt -->
28+
29+
```nix
30+
if predicate then foo else bar
31+
```
32+
33+
- One-liners are hard to understand,
34+
specially when nested,
35+
or when logic gets long.
36+
- Adding content produces a diff in the entire `if-then-else`.
37+
38+
✅ Good:
39+
40+
```nix
41+
if something <= 2.0
42+
then
43+
if somethingElse
44+
then foo
45+
else bar
46+
else if something <= 4.0
47+
then baz
48+
else if something <= 6.0
49+
then foo
50+
else bar
51+
```
52+
53+
- It's easy to follow that there are many conditionals.
54+
- The indentation makes it easy to read
55+
which expression is associated to each conditional.
56+
- Adding or modifying the branches produces a clean diff.
57+
58+
❌ Bad:
59+
60+
<!-- nixpkgs-fmt -->
61+
62+
```nix
63+
if cond
64+
then if
65+
looooooooooooooooooooooooooooooooooooooooooooooooooooong
66+
then foo
67+
else bar
68+
else if cond
69+
then foo
70+
else bar
71+
```
72+
73+
- It's complex to distinct the parent `if-then-else`
74+
from the child `if-then-else`

0 commit comments

Comments
 (0)