A variable name is a representation for a piece (or pieces) of data. It should accurately describe the underlying data.
Variable names should be meaningful.
Seeing something called
flag tells one it’s probably a Boolean, but doesn’t describe the intention whatsoever.
You don’t know what the purpose of it is.
A good variable name reduces ambiguity.
It allows the developer to understand what the data represents.
flag is rarely used correctly.
stylizedFontsEnabled tell a much more detailed story about what is represented.
Sometimes it’s hard to come up with a descriptive name. Naming things is hard. However, it is a worthy time investment, and it should be taken seriously. It’s becoming increasingly common that a software developer will leave their job and go somewhere else. This means a new codebase, a new opportunity, and potentially a new nightmare. If you’ve ever had to sift through someone else’s code that didn’t make sense, then you may be able to empathize. Looking at code is a logic puzzle and you should make it easy for the next person, or for your future self when something breaks.
Variable names may change over time as the context in which it operates changes.
As a very simple example, imagine a form where a user must enter their name.
Initially, you’d probably call the variable
Over time, that form may change and accept a first name and a last name.
name is not a good variable name because it’s not descriptive as to which piece of information it should hold.
fullName would be better suited to describe the scenario.
Clean Code does a great dive into this topic.