How to Name Variables: A Developer's Guide to Clean, Readable Code
6 min read
Variable naming is the most frequent design decision a programmer makes. Good names make code self-documenting; bad names force readers to decode your logic.
Core Principles
- Reveal intent:
elapsedTimeInDaysnotd - Avoid disinformation: Don't call a list
accountListif it's actually a set - Use pronounceable names:
generationTimestampnotgenymdhms - Use searchable names:
MAX_STUDENTS_PER_CLASSnot7
Naming Patterns by Type
| Type | Pattern | Examples |
|---|---|---|
| Boolean | is/has/can/should + adjective | isActive, hasPermission |
| Counter | noun + Count/Total | retryCount, totalItems |
| Collection | Plural noun | users, selectedItems |
| Function | verb + noun | calculateTotal, fetchUser |
| Event handler | handle/on + event | handleClick, onSubmit |
Anti-Patterns
data,info,temp,stuff— too vaguea,b,x— acceptable only in lambdas and math formulasflag— flag for what? UseisEnabledinstead- Hungarian notation (
strName,intAge) — outdated with modern IDEs
Format Your Names
Use the camelCase converter for JavaScript variables, snake_case converter for Python, or the case converter hub.