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.
Unary Operators ++ and --
++ and -- both do assignment and return the value.
Their placement can change the meaning of the code.
So, we should consider using alternatives.
For instance, instead of writing:
let i = 10;
i++;
We write:
let i = 10;
i += 1;
Use of process.env
We may use configurations instead of setting environment variables.
They’re easy to replace.
For instance, instead of writing:
if (process.env.NODE_ENV === "development") {
//...
}
We write:
const config = require("./config");
if (config.env === "development") {
//...
}
No process.exit()
process.exit() ends the program immediately.
This means that we don’t have a chance to handle errors gracefully.
So instead of writing:
if (error) {
process.exit(1);
}
Instead, we write:
if (error) {
throw new Error("error happened");
}
No Use of __proto__
__proto__ is a hidden property of an object which has its prototype.
We should use Object.setPrototypeOf and Object.getPrototypeOf to set and get the prototype of an object respectively.
For instance, instead of writing:
const a = obj.__proto__;
obj.__proto__ = b;
We write:
const a = Object.getPrototypeOf(obj);
Object.setPrototypeOf(obj, b);
No Use of Object.prototypes Built-in Properties Directly
We shouldn’t use Objetyc.prptotype built-in properties directly since they may be overridden by anyone.
For instance, foo.hasOwnProperty can be overridden by our own method.
Instead of writing;
const hasBazProperty = foo.hasOwnProperty("baz");
We write:
const hasBazProperty = Object.prototype.hasOwnProperty.call(foo, "baz");
No Variable Redeclaration
We shouldn’t redeclare variables, even though we can with var .
For instance, we shouldn’t write:
var a = 3;
var a = 123;
Instead, we write:
var a = 3;
a = 123;
Multiple Spaces in Regular Expression Literals
We shouldn’t make our regex literals more complex with extra spaces.
They’re hard to read, and we can replace them with curly braces.
For instance, instead of writing:
const re = /foo bar/;
We write:
const re = /foo {5}bar/;
No Assignment in return Statement
We shouldn’t assign values ina returnm statement.
It’s probably a mistake if we do.
We probably want to do a comparison.
For instance, instead of writing:
function doWork() {
return foo = bar + 2;
}
We write:
function doWork() {
return foo === bar + 2;
}
No Unnecessary return await
We don’t need to use return await together.
Using await with return just add an extra task to the microtask queue.
async functions always return a promise, so we can just return the promise directly instead of awaiting it before doing so.
For instance, instead of writing:
async function foo() {
return await bar();
}
We write:
async function foo() {
return bar();
}
No Script URLs
Script URLs are like eval . They can run JavaScript code that’s in a string.
So instead of writing:
location.href = "javascript:void(0)";
we remove that.
No Self Assignment
We shouldn’t do self-assignment in our code since they’re useless.
For instance, instead of writing:
foo = foo;
or:
[bar, baz] = [bar, abc];
Instead, we write:
foo = bar;
[a, b] = [b, a];
No Self Compare
Self-comparisons are useless, so we shouldn’t write to them.
For instead of writing:
if (x === x) {
x = 20;
}
We write:
if (x === y) {
x = 20;
}
No Use of the Comma Operator
We shouldn’t use the comma operator since they always return the last item in the list.
For instance, instead of writing:
const a = (3, 5);
We just write:
const a = 5;
No Returning Values from Setters
We shouldn’t return values from setters since they can’t be used.
For instance, we shouldn’t write:
conat foo = {
set b(value) {
this.val = value;
return value;
}
};
class Foo {
set b(value) {
this.val = value * 2;
return this.val;
}
}
Instead, we write:
conat foo = {
set b(value) {
this.val = value;
}
};
class Foo {
set b(value) {
this.val = value * 2;
}
}
Conclusion
We may consider using config files instead of environment variables.
Useless expressions and statements like self compare, returning things in setters, etc. should be removed.