A list of naming conventions based on this excelent article:
https://betterprogramming.pub/useful-tips-for-naming-your-variables-8139cc8d44b5
const somethingCount // itemsCount (preferable)
const numberOfSomething // numberOfItems
No need to follow this convention for names that already represent integer numbers, such as "age" or "year".
const somethingAmount // moneyAmount (preferable)
const amountOfSomething // amountOfMoney
No need to follow this convention for names that already represent floating-point numbers, such as "price", "width", "height", "weight", "temperature", etc...
Add the unit variable when it is not self-evident. For example:
const tooltipShowDelayInMillis // instead of tooltipShowDelay
const temperatureInCelsius // instead of temperature
const isSomething // isMenuOpen
const hasSomething // hasChildren
const somethingStr // yearStr
No need to follow this convention for names that already represent strings, such as "name", "title", "label", etc...
const somethingObj // nameObj (name is a string, but nameObj is an object)
In general, you won't need to follow this convention, as most names already suggest whether they are objects or not. Only when it is not clear, we'd add the Obj
suffix.
Use the plural form, like errors
, tasks
, clients
etc.
Use undefined
or a "default value" to tell the Code Reader which parameters are "optional".
// `undefined` tells us that `isOpen` is not required
const toggleMenu = (isOpen = undefined) => {
this.isOpen = isOpen ?? !this.isOpen
}
const printError = (text, color = 'red') => {
// ...
}