Descriptive Variable Names vs Short Ones: What Studies Say
Short answer: Use descriptive names for anything that lives longer than a few lines, and save single letters for tiny loop counters like i. One study of professional programmers found bugs about 19% faster when variables were written as full words.
What the study found
Hofmeister, Siegmund, and Holt (2017) tested 72 professional C# developers. Each person looked at short code snippets and tried to find a bug. The same snippet appeared in three versions: single-letter names, abbreviations, and full words.
Full words won. Developers found semantic bugs (code that runs but does the wrong thing) about 19% faster with words than with letters or abbreviations. The effect size was small to medium (dz = 0.32). Letters and abbreviations did not differ from each other. So cnt was no better than c.
That last point matters for class. Shortening a name to save typing does not seem to help the reader. Only a real word did.
What this study cannot tell you
Be careful with the claim. These were working professionals, not intro students. All of them were C# developers, and the snippets were short, simple, and about 15 lines. The authors also note that many of the people who started the study were removed by their exclusion rules, so the final group may not represent all developers. Speed at finding a planted bug is also only an approximation of "understanding" code.
Other work points the same cautious way. Beniamini et al. (2017) looked at 1,000 popular GitHub projects and found that i is the most common single-letter name. In their controlled experiments, people were not significantly worse at modifying code that used single letters. The authors concluded that single letters can be fine in some cases, though respondents in their survey still preferred longer names.
So the gap is modest, and it shows up for finding bugs. But it costs you almost nothing to write a clear name, and your grader and your future self will both be reading your code cold.
Why descriptive names help
When you write code, you hold the whole plan in your head. t feels obvious because you know what it is. A week later, or on a different person's screen, that meaning is gone.
A word carries its meaning with it. When the name says totalPrice, you can spot the bug in totalPrice = price + tax without tracing back to find out what t was. That is the kind of semantic bug the study measured.
Names also act as comments that cannot go out of date. They give a grader a way to see your intent, which can matter when your code is slightly wrong. A reader who can follow the logic can often see what you were trying to do.
How to name variables in class assignments
- Name the thing, not its type. Prefer
studentScoresoverlist1orarr. Say what the data means. - Write whole words. Prefer
averageoveravgora. The study found abbreviations did no better than letters, so skip them unless your course or language uses a standard one. - Allow short names in tiny scopes. A loop counter
i(andjfor a nested loop) is normal when the loop is a few lines and the index has no meaning beyond position. If the index means something, name it:row,col,studentIndex. - Match the idea in math code. Using
xandyfor coordinates, ornfor a count, is common and readable when your assignment is itself about math. Check your course style guide. - Make booleans read like questions.
isEmpty,hasWon, andfoundmakeifstatements read like sentences.
Try it tonight: Open your last assignment and find the three variables with the shortest names. Rename each one to a word that says what it holds, then reread the code and see if it reads more easily.
A quick check you can run
Read one function out loud, using only the variable names. If the sentence makes sense, the names are doing their job. If you say "t plus c times x" and cannot tell what it means, the reader cannot either. This takes under a minute and works on any assignment, in any language your class uses.
Common mistakes
- Over-long names.
theNumberOfStudentsInTheClassroomis not better thanstudentCount. Aim for clear, not long. - Number suffixes.
list1,list2, andtemp2tell the reader nothing. Name how they differ. - Reusing one name for two jobs. A variable called
resultthat first holds a sum and later a string is hard to follow. - Renaming at the last minute. Rename as you write, or while you still remember what each name means. If you are prepping for an exam, how to study for a CS exam covers what else to practice.
- Skipping the plan. Good names are easier when you know what the code should do. Try solving a test case by hand before coding, then name your variables after the values you used by hand.
Sources
- Hofmeister, J., Siegmund, J., & Holt, D. V. (2017). Shorter identifier names take longer to comprehend. In 2017 IEEE 24th International Conference on Software Analysis, Evolution and Reengineering (SANER). https://doi.org/10.1109/SANER.2017.7884623
- Beniamini, G., Gingichashvili, S., Klein Orbach, A., & Feitelson, D. G. (2017). Study of single-letter variable names in code. Hebrew University of Jerusalem. https://www.cs.huji.ac.il/w~feit/papers/SingleLetter17ICPC.pdf
