CSC/ECE 517 Fall 2010/ch5 5b jz

From Expertiza_Wiki
Jump to navigation Jump to search

Variable Naming Conventions

Importance of Good Variable Names

Variable Naming History

In the early days of computing, software was mainly used for complex mathematical calculations. Most programming languages of the time resembled algebra, with short (often only one or two character) variable names. As the complexity of these algorithms grew over time, it was no longer feasible to use such non-descriptive variable names. To facilitate the needs of those programmers, new languages were developed that supported richer variable naming schemes.

While the first BASIC interpreter limited variables to a single letter and optional digit (A, B2, B3, etc.), other languages gave programmers additional room for creating more meaningful variable names. FORTRAN for instance allowed a programmer to use up to 6 characters for variable names, which was quite a leap from previous languages. Eventually, C was introduced which allowed a programmer to use an arbitrary amount of characters for a variable name, thus giving a programmer ultimate control over their variable naming scheme (for better or for worse).

General Guidelines

Use a Common Naming Scheme

Hungarian Notation

Under Scores

Camel Case

Stay Short and to the Point

Be Descriptive

Explain Abbreviations with a Comment

The last two points would seem to contradict each other, but having short and to the point names does not necessarily mean that they are less descriptive. In fact, many variable names can be shortened with the use of abbreviations to make them not only descriptive, but succinct as well. Just remember to explain any abbreviations that you have made in a comment that accompanies the declaration of the variable.

Don't Be Generic

Variables such as 'temp' or 'counter' may seem self-explanatory when first writing a block of code, but they quickly lose their meanings when re-reading that same code out of context. Avoid using generic names such as these.

Use Units in Names

Avoid Negative Logic

Always use positive logic when naming variables, especially in the case of boolean values. Using a name such as 'isNotEnabled' instead of 'isEnabled' only serves to confuse yourself and any future readers of the code.