JavaScript lets us do a lot of things. It’s sometimes too forgiving in its syntax.
In this article, we’ll look at some antipatterns that we should avoid when we format JavaScript code, naming, and white spaces.
Coding Conventions
We got to establish some coding conventions so that it’s predictable and neat.
That helps with understanding a lot better.
Indentation
It’s impossible to read code without indentation. Therefore, we should indent our code.
To do that, we should indent code inside blocks.
So we write:
const foo = () => {
console.log('foo')
}
or:
for (let i = 0; i < max; i++) {
console.log('foo')
}
or:
if (val === 'foo') {
console.log('foo')
}
We do the same with any kind of nesting.
2 spaces is a good idea. It’s more portable than tabs as they’re the same everywhere.
A space character is always a space character, but tabs are up for interpretation.
Curly Braces
As we can see, we have curly braces in all our blocks.
We can skip them if we only have one line in the if
or loop body.
But it’s not a good idea to skip them since it makes reading the blocks much easier.
Therefore, we should always use curly braces to delimit blocks.
For instance, we shouldn’t write:
for (let i = 0; i < 10; i++)
console.log(i);
But instead, we write:
for (let i = 0; i < 10; i++) {
console.log(i);
}
Similar instead of writing the following if
block:
if (i === 1)
console.log(i);
We write:
if (i === 1) {
console.log(i);
}
Opening Brace Location
The opening brace is location is important. We may interpret the code differently in different places.
In JavaScript, it’s usually in the first line of the block.
For instance, we have:
if (i === 1) {
console.log(i);
}
and:
for (let i = 0; i < 10; i++) {
console.log(i);
}
and:
const foo = () => {
console.log('foo')
}
where the opening brace is always on the first line.
This is especially important is we’re returning something.
For instance, the following:
const foo = () => {
return {
foo: 'foo'
}
}
is different from:
const foo = () => {
return
{
foo: 'foo'
}
}
The code above is actually the same as:
const foo = () => {
return;
{
foo: 'foo'
}
}
So it won’t return the object.
Javascript automatically inserts semicolons when it thinks it makes sense.
Therefore, in return
statements, we should have the opening curly brace in the same line as the return
keyword.
White Space
Having white space also improves readability.
For instance, if we have:
x<1
That’s not too readable.
However, if we have:
x < 1
That’s a lot clearer.
Good places to put white spaces include the following locations:
- after the semicolons for separating parts of a loop —
for (let i = 0; i < 10; i++){ }
- initializing multiple variables —
let a = 1, b = 2;
- after the comma that separate array item s—
[1, 2, 3]
- after commas in object properties —
{a: 1, b: 2}
- separating function arguments —
fn(a, b, c)
- before curly braces in functions declarations —
function fn() {}
- after the
function
keyword —function () {};
We should also put spaces between operands in arithmetic or comparison operations.
For instance, 1 + 2
is better than 1+2
;
Naming Conventions
Names are also important. They’ve to descriptive and they have to be consistent.
This way we can predict what things mean and won’t have to look as hard to find out their true meaning.
Capitalizing Constructors and Classes
Constructor and class names should start with a capital.
For instance, we write:
function FooConstructor() {...}
and:
class Foo {...}
All words should start with capital as we can see.
Conclusion
We should stick to some common conventions for formatting JavaScript code.
This includes capitalizing first letters of words in constructor and class names.
Also, we should add spaces and indentation to make reading code easier.
In addition, we should always have curly braces for blocks whether they’re required or not.
Also, we should have braces in the right location so that they won’t be interpreted in ways that we don’t expect.