23 Dec, 2022 - About 2 minutes
Guideline on Naming Variables
Intro
“There are only two hard things in computer science: cache invalidation and naming things.” — Phil Karlson
Follow S-I-D
A name must be Short, Intuitive and Descriptive.
/* Bad */ |
Suggested:
/* Good */ |
Avoid Contraction
Do not use contractions. They contribute to nothing but decreased readability of the code. Finding a short, descriptive name may be hard, but contraction is not an excuse for not doing so. For instance
/* Bad */ |
Avoid Context Duplication
Always remove the context from a name if that doesn’t decrease its readability.
class MenuItem { |
Should Reflect expected result
/* Bad */ |
Consider Singular/Plurals
Like a prefix, variable names can be made singular or plural depending on whether they hold a single value or multiple values.
/* Bad */ |
Use meaningful and pronounceable variable names
const yyyymmdstr = moment().format("YYYY/MM/DD"); // simply awful |
The original article is more complete and has other suggestion regarding naming good practices would suggest you check it.