Skip to main content

Section 2.5 Choosing Variable Names

Programmers generally choose names for their variables that are meaningful to the human readers of the program—they help the programmer document, or remember, what the variable is used for.
For example, a variable named age gives you a good idea of what the variable’s purpose is. A name like a, on the other hand, isn’t very meaningful. Does it mean age, average, account, or what?
When you are writing programs in this course, you should choose meaningful names for your variables. (But don’t go overboard: age_in_years_as_of_last_birthday is, well, a bit too much.)
There are exceptions to this guideline: if you are writing a program that implements a well known mathematical formula, then variable names like x and y might be fine. If there are abbreviations that are commonly used, then a short name like qty is as meaningful as quantity.
Sometimes, if a variable is used in an example and doesn’t have an inherent meaning, you’ll see variables like a or b.

Warning 2.5.1.

Beginners sometimes confuse “meaningful to the human readers” with “meaningful to the computer”. They’ll wrongly think that because they’ve called some variable average or pi, it will somehow automagically calculate an average, or automagically associate the variable pi with the value 3.14159. No! The computer doesn’t attach semantic meaning to your variable names.