CSC/ECE 517 Fall 2010/ch4 4c t1: Difference between revisions
No edit summary |
|||
Line 82: | Line 82: | ||
==References== | ==References== | ||
<span id="1foot">[[#1body|1.]]</span> http://www.cprogramming.com/tutorial/style_naming_conventions.html | <span id="1foot">[[#1body|1.]]</span> http://www.cprogramming.com/tutorial/style_naming_conventions.html | ||
<span id="2foot">[[#2body|2.]]</span>http://java.sun.com/docs/codeconv/CodeConventions.pdf |
Revision as of 01:01, 19 October 2010
Naming Convention in Object-Oriented Languages
Naming convention in object-oriented programming refers to the naming rules that the programmer adopts for software elements, such as classes, methods, and variables. Naming convention may refer to the way in which compound words are capitalized, how descriptive the names are, as well as the length of the names among other attributes. The name selected for any variable, class or method within a program should convey the meaning and use of the object and should generally promote the readability of the code. There are standardized naming conventions that are used in the industry (e.g., camelCase), as well as language-specific naming conventions (e.g., Java, C++).
The advantages of using a consistent naming convention are most apparent when a programmer is working as part of a team. Use of a naming convention allows the different developers within the team to read and understand each others code with more ease since all of the programmers are using a matching set of rules. It also ensures that name collisions are less likely when programmers are working on parts of the program that rely on modules or classes from other developers.
When individuals are developing code, they too will experience benefits of using a consistent naming convention. A developer will find it easier to move between projects or pick up old projects they developed if they have been employing a consistent naming convention across all of their work. It is also important for a programmer to remember that code they are developing as part of an individual project may be incorporated as part of a larger team project in the future, so using a standardized naming convention will ensure that code is easier to hand off to other developers in the future.
History of Naming Conventions
Before the introduction of modern development environments and compilers, naming conventions were developed to serve a different purpose than how they are used today. Many standards were developed to indicate the variable type, scope, or purpose or to limit the length of the variable name because of limitations of the development tools.
Positional notation was adopted as a naming convention for very short names where the position of the character in the name indicates the attribute that it specifies. For example, with an eight character name the**************
Hungarian notation (more specifically “Apps Hungarian”) was developed by Charles Simonyi in the 1970’s while he was working for Xerox PARC. In Apps Hungarian, a prefix is added to the variable name to indicate the purpose of the variable. For example, tbRocks might be used to indicate a table of rock types in a geology program, with “tb” indicating that it is a table. Or frmTestEntry could be used to indicate a user form that would be used to administer a test.
Modern development tools have afforded code developers more flexibility in naming objects in their programs. Inspection tools that are built into the development environment allow attributes such as type and usage to be tracked based on the implementation in the language. The naming conventions for most modern object-oriented languages focus on ...
Standardized Naming Conventions for Object-Oriented Languages
C-Style
For the “C” languages, such as C, C++, and C#, there is a recommended naming convention, C-Style. Constants should be in all caps; #define MAX_HEIGHT, Functions, typedef, variable names, structs, unions and enum tags should all be in lowercase; typedef hello, int myheight. When using a naming scheme, avoid using similar looking names, such as myWindow and MyWindow or my_window and mywindow. Having similarly named methods or variables might be confusing when another coder is trying to debug the code. Using a leading and trailing underscore are reserved for system purposes, __STDC__ (preprocessor symbol) [1]
Java
The Java naming convention allows for an easy flowing, logical naming scheme. It allows a programmer to easily understand the nature of the class, method or variables being called. For class names, nouns are suggested with each first letter in the string capitalized. For example: “Person”, “ClassRoster”, “MyExampleClass”.
Methods, in most cases, should be verbs describing the action of the method. The first letter is lowercase followed by the first letter of the internal word in the string uppercased. For example: “getStudentName()”, “throwBallFar()”, “shootGun()”.
Variables should have a meaningful but short name. The first letter is lowercased followed by the first letter of the internal word in the string uppercased. For example: “int windowSize”, “string firstName”. Temporary variables generally are 1 character long. For integers, i, j, k, m, n are usually used and “c, d, e” are used for charaters. Example: for(int i = 0; i < 20; i++){ i = i + 1 // i lives only within the for loop}
For class constants, the entire string is capitalized with words separated by an underscore “_”. For example: “static final int WINDOW_SIZE = 640;”, “static final string WINDOW_NAME = “Charlie”;” It is recommended to NOT use underscores in variable names, although it is allowable by the compiler.
camelCase
Camel case refers to the use of capitalization to add clarity to compound names assigned in code. It evolved as a common standard because of the need to avoid commonly available delimiters, such as hyphens (-) or underscores (_), that were used as keywords in the syntax of languages.
Naming Convention Attributes
Classes
When naming a class, the name of the class should reflect the object that the class is describing. If the programmer decides to name a class “class1”, no indication is given of the object that is being represented by the class.
class class1 { }
If the developer upgrades the class name to “appleclass” the name gives more insight into the purpose, but includes useless information (“class”) that hampers readability.
class appleclass { }
Consider a change of the name to simply “Apple”. The purpose of the class is very clear and readability is improved significantly from where the programmer started.
class Apple { }
Methods
Method names should clearly indicate the what action is performed when calling the method. Take for example the method “methodb”, which tells the reader nothing about the purpose of the method.
int methodb() { }
If the name is upgraded to “rangemethod” it adds some additional info to the readability since it is now clear that it has something to do with a range, but it still lacks clarity.
int rangemethod() { }
If the name is refined to “getRange”, the reader can now distinguish that the method will return the range of the particular class.
int getRange() { }
Variables
Consider the case of a programmer creating a counter variable. If the programmer chooses to name the variable “a”, the name gives the reader of the code no idea of the programmers intent for that variable.
a += 1; print a + ‘ users’;
If the programmer chooses to upgrade the name to “thiscounter”, they have given more information (we now know we are dealing with a counter) but we still don’t know how this counter is used.
thiscounter += 1; print a + ‘ thiscounter’;
Now consider that the programmer upgrades the variable name to “userCounter”. It gives a clear indication that the counter is counting users, so we know how it is used. Notice that the capitalization has been updated to make it easier to distinguish the different parts of the compound name.
userCounter += 1; print a + ‘ userCounter’;
Conclusion
Here
References
1. http://www.cprogramming.com/tutorial/style_naming_conventions.html 2.http://java.sun.com/docs/codeconv/CodeConventions.pdf