Personal styleguides.
- space between
function
and the identifier - no space between the identifier and the argument list
- arguments delimited by
,
- curly brace on same line
Single-Line:
function f(x, y, z) {
}
Multiline:
function f(
very_long_argument_1,
very_long_argument_2,
very_long_argument_3
) {
}
- space between braces, key/colon pairs, and values
Single-Line:
const obj = { a: 1, b: 2, c: { d: 3 } }
Multiline:
const obj = {
a: 1,
b: 2,
c: {
d: 3
}
}
- elements delimited by
,
Single-Line:
const arr = [1, 2, 3, 4, 5]
Multiline:
const arr = [
1,
2,
3,
4,
5
]
- space between
if
and conditional
Single-Line:
if (p) f()
Multiline:
if (p) {
f()
}
- space between expressions,
?
, and:
Single-Line:
const result = x ? y : z
Multiline:
const result = x
? y
: z
Else-If Chain:
const result =
p1 ? a :
p2 ? b :
p3 ? c :
d
Conditional Tree:
const result =
p1
? p2
? a
: b
: p3
? c
: d
Single-Line:
arr.map(x => x + 1)
Multiline:
fs.readdir('.', (err, files) => {
if (err) return handle_error(err)
// ...
})
Constants use UPPER_SNAKE_CASE
Variables and functions use lower_snake_case
String identifiers use lower-kebab-case
const SOME_CONSTANT = 42
const some_variable = some_function(x)
const package_name = 'some-package'
Values with Maybe
semantics should always be either the value or null
. Don't use undefined
(it's not a function).
Use hard tabs with four spaces.
Only use semicolons to delimit multiline expressions or in a for-loop.
;(1 + 2).toString()
;[1, 2, 3].forEach(console.log)
for (;;) {
// ...
}
When a multiline function call contains only object/array literals or when an array literal contains only object literals, the child constructions can be collapsed one indentation level into the parent construction.
f({
a: 1
}, {
b: 2
}, [
1,
2,
3,
4
])
const arr = [{
a: 1
}, {
b: 2
}]