Meaningful identifier names
What is an Identifier?
An identifier is the name given to a variable, constant, or subroutine in a program. Every time you declare a variable such as x, name a constant such as MAX, or define a subroutine such as calc, you are choosing an identifier. Identifiers must follow language rules (no spaces, no reserved words), but beyond that the choice is yours - and that choice matters greatly.
Meaningful vs Poor Identifiers
A meaningful identifier describes the purpose of the value or action it represents. It makes code self-documenting - a reader can understand the program without needing separate notes. Poor identifiers such as single letters or vague abbreviations force the reader to guess, increasing the chance of errors during maintenance or collaboration.
The code below works correctly, but the identifiers give no clue about purpose. What does x hold? What does f calculate? What is n?
n = 5
x = 9.81
def f(m):
return m * x
r = f(n)
The same logic with meaningful identifiers. The purpose of every value and subroutine is immediately clear - no guesswork required.
mass_kg = 5
GRAVITY = 9.81 # constant -- UPPER_CASE convention
def calculate_weight(mass_kg):
return mass_kg * GRAVITY
weight_newtons = calculate_weight(mass_kg)
Key Takeaways
- An identifier is the name given to a variable, constant, or subroutine.
- Meaningful identifiers describe the purpose of the value or action, making code easier to read, debug, and maintain.
- Poor identifiers (e.g.
x,a1,temp2) make code hard to understand and increase the risk of errors when the code is modified later. - Good identifiers benefit not just the original programmer, but anyone who reads or maintains the code in future.