2. Variable Names
|
|
There are some restrictions on the names of variables and symbolic constants. |
|
2.1 Naming Rules |
|
Variable names can contain letters, digits, and ‘_’. The underscore ‘_’ is sometimes useful for improving the readability of long variable names. |
Variable names should start with letters. The under score ‘_’ counts as a letter. However, don’t begin variable names with underscore, since library routines often use such names. |
Keywords (e.g., for,while etc.) cannot be used as variable names |
Variable names are case sensitive. int x; int X declares two different variables. |
Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants. |
At least the first 31 characters of an internal name are signification. |
For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. |
For external names, the standard guarantees uniqueness only for 6 characters and a single case., ‘abcdefg’ is identical to ‘abcdefh’. |
|
2.2 Name Convention
There are three kinds of naming conventions: underscore notation, cammelCase, and Hungarian notation.
|
|
underscore notation |
– Delimit separate words with the underscore as the subtraction infix.
– ex. “two words” is represented as “two_words”.
– This convention is commonly used in C. |
cammelCase |
– Indicate word boundaries using medial capitalization. The first letter of a camel-case compound may or may not be capitalized.
– ex. “two words” is represented as “twoWords”.
– This convention is commonly used in Java, C#, Visual Basic, and some language (such as Mesa, Pascal, Modula, Java, Google SOC's Python recommendations, and Microsoft's .NET). |
Hungarian notation |
– The name of a variable or function indicates its type or intended use.
– Examples |
bBusy, fBusy : boolean (b for bool, f for flag)
chInitial : char
cApples : count of items
dwLightYears : double word (systems)
nSize, iSize : integer,
uSize, lSize: unsigned integer, long
wSize, dwSize: WORD(unsigned short), DWORD(unsigned long)
dbPi : double
pFoo : pointer
aulColors : array of unsigned long (systems)
szLastName : zero-terminated string
strName: CString variable
m_XXXX: member variable |
|
Is the following naming right? |
– int money$owed ( )
– int total_count ( )
– int score2 ( )
– int 2ndscore ( )
– int long ( ) |