- Make names descriptive, so they explain the purpose of variables and functions.
- Don't use literals in expressions (HARDCODE). Instead use constants explaining this values
- BAD EXAMPLE:
if (numberOfDays >= 7) { return basePrice - 50; }
- GOOD EXAMPLE:
const LONG_TERM = 7; const LONG_TERM_DISCOUNT = 50; if (numberOfDays >= LONG_TERM) { return basePrice - LONG_TERM_DISCOUNT; }
- Prefer
const
overlet
where possible, to avoid unintentional changes. - Prefer
if
withreturn
overif else
to simplify later conditions. - DON'T add
else
afterif
withreturn
- the code after it won't be executed anyway.- BAD EXAMPLE:
if (condition) { return x; } else { return y; }
- GOOD EXAMPLE:
if (condition) { return x; } return y;