Conventions

What conventions do we have?

There are quite a few conventions in Javascript-land. One of them is naming things:

const person = {
    name: "Angelique",
    age: 88,
    city: "Amsterdam",
    hasTicket: true
}

const boat = {
    captain: "Henk",
    passengers: [person, otherPerson, someOtherPerson]
}

const isOld = person => person.age > 80; 
const hasPassengers = boat => boat.passengers.length > 0;
const shouldBoard = person => person.hasTicket;

In the previous example you see that we write variables with a boolean value in a specific fashion. If a property meets a specific predicate, we tend to prefix it with is. If an array or object possesses a certain value, we can prefix it with has. If we decide if we want to take an action or not, we can prefix it with should. These are not set in stone, use what seems the most logical!

Another convention are constants. Values that are not supposed to change:

const PI = 3.14159; // Use ALL-CAPS for constants
const VAT_PERCENTAGE = 21.00; 

Store constants in one location and refer to them from that location by importing them. This prevents having to duplicate. Imagine if you are a webshop, and you have the VAT inlined in every location. Imagine that the VAT-percentage is changed to 22. The horror! You would have to look in the codebase to find every 21 that is used as a VAT-percentage, and replace it.

Also, try to avoid magic numbers. That are numbers that miss the semantics to understand them. The number 18 on itself doesn't mean much without context. But if we call it MINIMUM_AGE, then we know exactly what it means.

ESLint also covers several conventions. Configure it for all your projects!

Last updated

Was this helpful?