Avoiding Meaningless and Generic Names
You'll learn how vague, generic variable names obscure your code's intent and how to replace them with meaningful alternatives.
The Problem with Generic Names
Names like data, info, temp, x, i (outside loops), or result tell you nothing about what the variable actually represents. They're placeholders that force readers to mentally decode your code, wasting precious mental energy.
Think of it like labeling kitchen containers. A jar marked "stuff" is useless—you have to open it to know if it contains flour, sugar, or coffee. But "wholeWheatFlour" tells you exactly what's inside at a glance.
Common Offenders and Better Alternatives
data→ What kind of data? TrycustomerRecord,sensorReading, orjsonPayloadinfo→ Same problem. UseuserProfile,errorDetails, orconfigSettingstemp→ Temporary for what purpose? Replace witholdPassword,swapValue, orpreviousIndex- Single letters (outside loops) →
x,y,zare fine for coordinates, but otherwise usedistance,velocity,userName result→ Result of what? TryisValid,customerCount, ortotalPrice
The Exception
Single letters like i, j, k are acceptable for simple loop counters within a tiny scope, because their purpose is immediately clear from context. But even here, index or row and col are often clearer.
Making the Shift
When you write a generic name, pause and ask: "What does this actually represent?" The answer is your better name. If you can't answer clearly, your design might need refinement.
Key Takeaway: Meaningless names like data, temp, and single letters hide intent—replace them with specific names that reveal exactly what the variable represents and how it's used.