Coding Guidelines

Coding Guidelines

Consistency is key

Most projects are not built or maintained by a single person. Instead, there is a collection of people involved, who all have their own personal preferences. If everyone would use their personal style, maintainability would suffer.

With coding guidelines enforced, the code will be easier to understand. With understandability comes readability, changeability, extensibility and maintainability.

Variable names

Variable names generally shouldn't be abbreviated.

// Good
function saveUser(user) {
    localStorage.set('user', user);
}

// Bad, it's hard to reason about abbreviations in blocks as they grow.
function saveUser(u) {
    localStorage.set('user', u);
}

String Interpolation

When embedding expressions, always use the template literals syntax.

// Good
const name = 'Luigi';

console.log(`Hello, $(name)!`); // Result: Hello, Luigi!

// Bad
const one = 1;
const two = 2;

console.log("The sum of one and two is " + (one + two)); // Result: The sum of one and two is 3

Comparisons

Always use a triple equal to do variable comparisons. If you're unsure of the type, cast it first.

// Good
const one = 1;
const another = "1";

if (one === parseInt(another)) {
    // ...
}

// Bad
const one = 1;
const another = "1";

if (one == another) {
    // ...
}