Skip to content

Instantly share code, notes, and snippets.

@gchumillas
Last active April 23, 2021 08:19
Show Gist options
  • Save gchumillas/aa797e804211d4ae07d6e9089446b3fb to your computer and use it in GitHub Desktop.
Save gchumillas/aa797e804211d4ae07d6e9089446b3fb to your computer and use it in GitHub Desktop.

A list of naming conventions based on this excelent article:
https://betterprogramming.pub/useful-tips-for-naming-your-variables-8139cc8d44b5

Naming numbers

Integer numbers

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".

Floating-point numbers

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...

Tips

Add the unit variable when it is not self-evident. For example:

const tooltipShowDelayInMillis // instead of tooltipShowDelay
const temperatureInCelsius     // instead of temperature

Naming booleans

const isSomething  // isMenuOpen
const hasSomething // hasChildren

Naming string

const somethingStr // yearStr

No need to follow this convention for names that already represent strings, such as "name", "title", "label", etc...

Naming objects

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.

Naming arrays, lists, and sets

Use the plural form, like errors, tasks, clients etc.

Naming optional parameters

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') => {
  // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment