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: elapsedTimeInDays not d
  • Avoid disinformation: Don't call a list accountList if it's actually a set
  • Use pronounceable names: generationTimestamp not genymdhms
  • Use searchable names: MAX_STUDENTS_PER_CLASS not 7

Naming Patterns by Type

TypePatternExamples
Booleanis/has/can/should + adjectiveisActive, hasPermission
Counternoun + Count/TotalretryCount, totalItems
CollectionPlural nounusers, selectedItems
Functionverb + nouncalculateTotal, fetchUser
Event handlerhandle/on + eventhandleClick, onSubmit

Anti-Patterns

  • data, info, temp, stuff — too vague
  • a, b, x — acceptable only in lambdas and math formulas
  • flag — flag for what? Use isEnabled instead
  • 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.