CSC/ECE 517 Summer 2008/wiki2 2 rapodraz: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:


Examples of good, well known naming conventions are [http://msdn.microsoft.com/en-us/library/aa260976(VS.60).aspx Hungarian Notation], and the [http://www.symbian.com/developer/techlib/v70sdocs/doc_source/DevGuides/EssentialIdioms/NamingConvs.guide.html Symbian OS naming conventions].
Examples of good, well known naming conventions are [http://msdn.microsoft.com/en-us/library/aa260976(VS.60).aspx Hungarian Notation], and the [http://www.symbian.com/developer/techlib/v70sdocs/doc_source/DevGuides/EssentialIdioms/NamingConvs.guide.html Symbian OS naming conventions].
There are other aspects of naming which may not be specific to a particular naming convention, but are part of general good practice, such a name's length and the use of abbreviations. It is generally preferred to use short names and abbreviations when possible without confusing the meaning of the name. Common examples are using "ptr" for "pointer" or "ct" for "count."  
There are other aspects of naming which may not be specific to a particular naming convention, but are part of general good practice, such a name's length and the use of abbreviations. It is generally preferred to use short names and abbreviations when possible without confusing the meaning of the name. Common examples are using "ptr" for "pointer" or "ct" for "count." Often variable names include a verb, and in this case it is always best to use the active voice. For example, never use a name like "recieverForMessages" when "messageReciever" would work. It would even be possible to abbreviate this to "msgReceiver" to be even more concise. As another example, don't use "countOfItems" when you can use "itemCount." Never use negative forms of a word for variable names, such as state. Otherwise you may run into confusing [http://en.wikipedia.org/wiki/Double_negative double-negative] boolean expressions in code. Be careful that your variable name is not the same as a [http://en.wikipedia.org/wiki/Reserved_word reserved word] in a language. Some compilers will not even allow you to do this, but some will, and it may resolve the name clash in a way your or others do not expect. Do not use variables that different in name by only a few letters, as this may result in accidentally writing incorrect code that compiles without errors.
 
Often variable names include a verb, and in this case it is always best to use the active voice. For example, never use a name like "recieverForMessages" when "messageReciever" would work. It would even be possible to abbreviate this to "msgReceiver" to be even more concise. As another example, don't use "countOfItems" when you can use "itemCount."
 
Never use negative forms of a word for variable names, such as state. Otherwise you may run into confusing [http://en.wikipedia.org/wiki/Double_negative double-negative] boolean expressions in code.


=Naming Choices=
=Naming Choices=
Line 28: Line 24:
http://chrisbensen.blogspot.com/2007/07/variable-names-to-avoid.html<br>
http://chrisbensen.blogspot.com/2007/07/variable-names-to-avoid.html<br>
http://blogs.msdn.com/marcelolr/archive/2005/08/04/Marcelo.aspx
http://blogs.msdn.com/marcelolr/archive/2005/08/04/Marcelo.aspx
http://www.oualline.com/style/c03.html


[[CSC/ECE 517 Summer 2008/wiki2 Assignment|Back to the assignment page]]
[[CSC/ECE 517 Summer 2008/wiki2 Assignment|Back to the assignment page]]

Revision as of 18:51, 23 June 2008

Variable Naming in Programming

Almost all programming languages allow the programmer a great deal of freedom when naming variables in a program's source code. It may seem like this is an obvious advantage, as it gives the programmer a great deal of flexibility. However, this level of freedom may incorrectly reflect the notion that variable naming is unimportant. Since variable identifiers can be named in many different ways, they often are, and the result is a high degree of inconsistency that can be problematic for code readability and maintenance.

Naming Conventions

In most contexts, the term "naming conventions" refers to a set of rules used to name variables depending on its scope, type, etc. Naming conventions are a form of metadata. A good set of naming conventions can be very useful and is often part of good programming practice. Naming conventions may differ among programming languages and projects, but the purpose is the same: to provide information about the variable identifier in the name itself. Some examples of common naming conventions include:

  • Using lowercase for a single word name
  • Using capitalization or underscores to separate multi-word names
  • Using all caps for constants
  • Using variable prefixes to indicate scope or type

Examples of good, well known naming conventions are Hungarian Notation, and the Symbian OS naming conventions. There are other aspects of naming which may not be specific to a particular naming convention, but are part of general good practice, such a name's length and the use of abbreviations. It is generally preferred to use short names and abbreviations when possible without confusing the meaning of the name. Common examples are using "ptr" for "pointer" or "ct" for "count." Often variable names include a verb, and in this case it is always best to use the active voice. For example, never use a name like "recieverForMessages" when "messageReciever" would work. It would even be possible to abbreviate this to "msgReceiver" to be even more concise. As another example, don't use "countOfItems" when you can use "itemCount." Never use negative forms of a word for variable names, such as state. Otherwise you may run into confusing double-negative boolean expressions in code. Be careful that your variable name is not the same as a reserved word in a language. Some compilers will not even allow you to do this, but some will, and it may resolve the name clash in a way your or others do not expect. Do not use variables that different in name by only a few letters, as this may result in accidentally writing incorrect code that compiles without errors.

Naming Choices

Choosing a good variable name means naming a variable in a way that will help the reader understand the program's design and purpose. This is different from naming conventions in that it can not be so strictly defined, and is concerned with individual variables as opposed to all variables.

Using Design Pattern Names

External Links

http://www.symbian.com/developer/techlib/v70sdocs/doc_source/DevGuides/EssentialIdioms/NamingConvs.guide.html
http://www.kamath.com/columns/squareone/so001_whatname1.asp
http://msdn.microsoft.com/en-us/library/aa260976(VS.60).aspx
http://chrisbensen.blogspot.com/2007/07/variable-names-to-avoid.html
http://blogs.msdn.com/marcelolr/archive/2005/08/04/Marcelo.aspx http://www.oualline.com/style/c03.html

Back to the assignment page