To make code easy to read and maintain, we should follow some best practices. In this article, we’ll look at some best practices we should follow to make everyone’s lives easier.
No Unused Labels
We shouldn’t have unused labels in our code.
For instance, instead of writing:
B: {
foo();
}
We write:
foo();
No Unnecessary Computed Property Keys in Objects and Classes
If we’re going to put in strings into the brackets, then we should use other alternatives.
For instance, instead of writing:
const foo = {["a"]: "b"};
We write:
const foo = {"a": "b"};
or
const foo = { a: "b" };
No Unnecessary Concatenation of Strings
We shouldn’t concatenate strings if we don’t need to do them.
For instance, instead of writing:
const foo = "foo" + "bar";
We write:
const foo = "foobar";
No Unnecessary Constructor
We shouldn’t write unnecessary constructors in our code. JavaScript will add them in some cases automatically.
For instance, we can write:
class A {
constructor() {}
}
or
class B extends A {
constructor(value) {
super(value);
}
}
Instead, we write:
class A {
constructor() {
doWork();
}
}
or:
class B extends A {
constructor() {
super();
super('foo');
}
}
Unnecessary Escape Usage
We shouldn’t escape non-special characters in strings, template literals, and regexes.
For instance, we shouldn’t write:
let foo = "hola";
Instead, we write:
let foo = "hola";
No Renaming import, export, and Destructured Assignments to the Same Name
We shouldn’t rename imports, exports, and restructured assignments to the same name.
For instance, we shouldn’t write:
import {
baz as baz
} from "bar";
export {
baz as baz
};
const {
foo: foo
} = bar;
Instead, we write:
import { baz } from "bar";
export { baz};
let { foo } = bar;
No Redundant return Statements
We shouldn’t have redundant return
statements in our code.
For instance, the following have useless return
statements:
function foo() {
return;
}
function foo() {
doWork();
return;
}
Instead, we write:
function foo() {
}
function foo() {
doWork();
}
Use let
or const
instead of var
let
or const
are block-scoped, so they remove lots of confusion with var
.
For instance, instead of writing:
var x = "y";
var CONFIG = {};
We write:
let x = "y";
const CONFIG = {};
Don’t Use the void Operator
The void
operatior always return undefined
, so there’s no point in using it.
For instance, instead of writing:
function baz() {
return void 0;
}
We write:
function baz() {
return undefined;
}
No Warning Comments
Instead of adding comments for warnings, we should fix the traps indicated in the comments.
Instead of writing:
// TODO: fix this
// FIXME: bad code
We do the things in the comment.
No Whitespace Before Properties
We shouldn’t have whitespace before properties.
So we don’t write:
foo [bar]
foo. bar
Instead, we write:
foo[bar]
foo.bar
No with
Statements
with
statements are confusing because we may confuse the variable inside with what’s outside. So we shouldn’t use it, and it’s not allowed with strict mode.
For instance, instead of writing:
with(dimensions) {
area = width * height;
}
We write:
const area = ({width, height}) => width * height;
Location of Single-Line Statements
If we have single-line statements, then they should be always in the same location so that we won’t be confused by their location.
For instance, instead of writing:
if (foo)
bar();
baz();
We write:
if (foo)
bar();
baz();
So we won’t be confused with their location.
Enforce Consistent Line Breaks Inside Braces
We should have consistent line breaks inside braces. Diffing is easier if they’re consistent.
For instance, we should write:
let b = {
foo: 1
};
and stick with it.
Consistent Spacing Inside Braces
Like with line breaks, we should have consistent spacing inside braces.
For instance, we can write:
const obj = { foo: "bar" };
and stick with it.
Conclusion
We should keep our spacing consistent for easy digging. Also, we shouldn’t do unnecessary operations or have useless expressions. Useless constructors should also be removed. with
statements should never be used.