CSC/ECE 517 Fall 2010/ch5 5b jz: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 38: Line 38:


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.
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.
==References==
{{reflist}}

Revision as of 03:58, 2 November 2010

Variable Naming Conventions

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.<ref>1</ref> 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).

Importance of Good Variable Names

As any programmer who has modified another's code knows, the difference between good and bad variable names can be hours of frustration attempting to make sense of someone else's work. A good set of variable naming conventions allows a programmer to focus on the logic and flow of the code, not on the meaning of strange abbreviations and what 'temp', 'temp1', and 'temp2' are really used for.

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.

References