Readability Expectations

Variables && How to Name Them

A general rule: You should always name your variable something logical — something that is meaningful within a specific section of code.

Variable Naming Advice:

  • Avoid spelling errors. While this can be tricky since you can’t spell check it does actually make a difference.
    • Consider a program with multiple coders and you make a function called euqals instead of equals
    • If the spelling is bad enough you may be violating the “meaningful name” rule!
  • Don’t abuse case sensitivity.
    • Don’t create two variables that only differ in capitalization (i.e. ‘one’ and ‘One’). This will get confusing.
    • Don’t create a variable that is a capitalized reserved word.
  • Avoid your own abbreviations
    • While you may come up with shorthand that makes sense at the moment it may be something you easily forget
      • If you really want to do this… add a comment explaining what the abbreviation means
    • If someone else is looking at your program this doesn’t follow the “meaningful name” rule
  • Avoid swearing
    • It may seem funny or maybe you are venting frustration when debugging… but it is easy to forget you have inappropriate things in your code.

Only exception:

In my opinion the one exception to the “meaningful name” rule is loop counter variables!

If you are writing a simple loop and all your variable does is count, then its acceptable to use i, j, x, temp, etc.

Common syntax naming rules:

These rules apply to many languages including java and C++:

  • A variable cannot contain whitespace.
  • You cannot use a reserved word as a variable name (i.e. public, float).
  • You cannot start a variable with a number.
  • Many languages restrict the use of special characters in variable names

Be aware: These are my opinions. Other instructors will have their own policies!

Check out more of my readability expectations by language: