CSC/ECE 517 Fall 2010/ch5 5b jz: Difference between revisions
Line 32: | Line 32: | ||
====Hungarian Notation==== | ====Hungarian Notation==== | ||
Hungarian notation is similar to Camel Case, but it adds they type of the variable to the front of the name. This style is | Hungarian notation is similar to Camel Case, but it adds they type of the variable to the front of the name. This style is helpful when there are a large number of variables in use that may have similar names, but different or ambiguous types. | ||
// String (str) for the student's name. | // String (str) for the student's name. |
Revision as of 04:32, 2 November 2010
Variable Naming Conventions
"In computer programming, a variable is a symbolic name given to some known or unknown quantity or value, for the purpose of allowing the name to be used independently of the value it represents. A variable name in computer source code is associated with a data storage location and thus also its contents, which generally change during the course of program execution." [1]
Variable Naming History
In the early days of computing, software was generally 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. [2] 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.
Importance of Strategic 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.
This article presents a list of guidelines and examples that a new programmer should take notice of when naming variables, classes, packages, etc. Because modern programming languages place almost no restrictions or regulations on variable names, it is more important than ever to be disciplined in your approach to naming variables.
General Guidelines
Common Naming Patterns
Under Scores
Camel Case
Camel case is a way of denoting compound variable names, and is commonly used in a wide range of languages (Java, C, C++, etc). It is characterized by capitalizing the first letter of each new word in a compound name. This method of naming variables would not normally be mixed with underscores for consistency.
studentName classRank gradePointAverage isEnrolled
Hungarian Notation
Hungarian notation is similar to Camel Case, but it adds they type of the variable to the front of the name. This style is helpful when there are a large number of variables in use that may have similar names, but different or ambiguous types.
// String (str) for the student's name. strStudentName
// Integer (int) for class rank. intClassRank
// Float (f) for grade point average. fGradePointAverage
// Boolean (b) for enrollment status. bIsEnrolled
The examples above show the usefulness of Hungarian Notation, as it would otherwise be difficult to determine the variable type by simply looking at the names. As Integrated Development Environments (IDEs) become more powerful and user-friendly, the advantages of Hungarian Notation become less prominent (due to the automated variable type display in most modern IDEs).
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.