5 Rules You Should Follow When Naming Variables

Improve your code readability with these tips

XOR
JavaScript in Plain English
2 min readDec 16, 2020

--

Photo by Mihai Surdu on Unsplash

In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation.

Naming things is hard!

This writing is tried to focus on some rules and protocol for naming a variable which will improve code readability.

Although these suggestions can be applied to any programming language, we will use JavaScript to illustrate them in practice.

1. Follow S-I-D

A name must be Short, Intuitive and Descriptive.

/* Bad */
const a = 5 // "a" could mean anything
const isPaginatable = (postsCount > 10) // "Paginatable" sounds extremely unnatural
const shouldPaginatize = (postsCount > 10) // Made up verbs are so much fun!

Suggested:

/* Good */
const postsCount = 5
const hasPagination = (postsCount > 10)
const shouldDisplayPagination = (postsCount > 10) // alternatively

2. Avoid Contraction

Do not use contractions. They contribute to nothing but decreased readability of the code…

--

--