Skip to content

Commit 1e5705b

Browse files
committed
blog: Add 2025-07-20.md
1 parent 50984d5 commit 1e5705b

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

blog/2025-07-20.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: YAML Best Practices
3+
date: 2025-07-20
4+
draft: false
5+
authors: [ingydotnet]
6+
categories: [Summer-of-YS]
7+
edit: blog/2025-07-20.md
8+
comments: true
9+
---
10+
11+
YAML has a [Core Development Team](https://yaml.org/spec/1.2.2/ext/team/) of
12+
five people that I am a member of.
13+
14+
In May 2024, 4 of us met in Berlin.
15+
One of the things we did was to write a list of best practices for YAML.
16+
We came up with a list of 40 or so things that we all agreed on.
17+
18+
Today I'll share some of those with you.
19+
20+
<!-- more -->
21+
22+
23+
## Don't quote strings that don't need to be quoted
24+
25+
One of the main points of YAML was to create a format that consisted of more
26+
data and less syntax.
27+
28+
People tend to quote strings that contain anything they are not sure about,
29+
instead of learning the rules for when to quote and when to not quote.
30+
31+
Scalar values without quotes are called "plain scalars".
32+
33+
The basic rules for plain scalars are pretty simple:
34+
35+
* They can't start with these characters: `!`, `@`, `#`, `%`, `&`, `*`, `|`,
36+
`>`, `,`, `{`, `}`, `[`, `]`, `>`, `?`, `'`, `"`, a backtick or whitespace
37+
* They can't contain `: ` or ` #` combinations
38+
* They can't end with whitespace
39+
40+
That's pretty much it.
41+
42+
If you're not sure, don't quote it!
43+
44+
45+
## Prefer single quotes over double quotes
46+
47+
For some reason, people tend to use double quotes for strings much more than
48+
single quotes.
49+
50+
Single and double quoted strings have different semantics in YAML.
51+
52+
Double quoted strings provide a way to escape characters.
53+
54+
Single quoted strings only provide a way to escape a single quote (use 2 single
55+
quotes to escape a single quote).
56+
57+
Only use double quotes if you need to escape something.
58+
59+
Note: In YS (code mode) double quoted strings also provide a way to interpolate
60+
variables.
61+
62+
63+
## Use JSON's `true`, `false` and `null`
64+
65+
The YAML 1.2 core schema lets you use `TRUE` and `True` variants of `true`.
66+
Same for `false` and `null`.
67+
68+
Stick to the JSON standard of `true`, `false` and `null`.
69+
70+
For null values in mappings you can also use the empty string (nothing) to
71+
represent `null`.
72+
73+
```yaml
74+
a null value: null
75+
also null:
76+
don't use: NULL
77+
```
78+
79+
That can be useful in YS too.
80+
Print a blank line with:
81+
82+
```
83+
say: 'one'
84+
say: # blank line
85+
say: 'two'
86+
```
87+
88+
89+
## Non-empty YAML files should end with a newline
90+
91+
There are some edge cases where different YAML parsers will disagree on the
92+
meaning of a YAML file when it doesn't end with a newline.
93+
94+
It's best to end your YAML files with a newline.
95+
Most editors can be configured to add a newline at the end of the file.
96+
97+
Do it.
98+
99+
100+
## Don't block sequences in mappings
101+
102+
This one is a bit controversial.
103+
104+
When you have a sequence in a mapping, the `- ` essentially counts as
105+
indentation.
106+
107+
```yaml
108+
a mapping:
109+
- a sequence
110+
```
111+
112+
Many people will indent it like this:
113+
114+
```yaml
115+
a mapping:
116+
- a sequence
117+
```
118+
119+
You won't be arrested by the YAML police for doing this, but the core team
120+
prefers the former style.
121+
122+
123+
## Use `.yaml` file extensions
124+
125+
We always wished that people would use the `.yaml` file extension for YAML
126+
files.
127+
At some point people started using `.yml` extensions and we didn't yell about
128+
it.
129+
130+
We know that ship has sailed, but we still prefer `.yaml` over `.yml` if you
131+
are starting something new and have the choice.
132+
133+
134+
## 50 Posts!
135+
136+
If you haven't noticed the Summer of YS blog series is over half way done!
137+
138+
Today is the 50th post in the series!!!

0 commit comments

Comments
 (0)