C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
| < Day Day Up > |
|
Identifiers are names given to various objects in a program. You have already seen several identifiers declared and used in this chapter; they were given short names like i, a, b, and represented simple data types. Identifiers are also used to name functions, labels, and other user defined data types. The C++ reserved keywords, listed in the Keywords section of this chapter, are examples of identifiers you cannot use to name your objects because they are reserved by the compiler.
Identifiers are formed using letters and digits, however, an identifier must start with a letter or underscore “_” character. The following are valid identifiers:
count _count I_count_to_10 _9to5 getCount() printScreen() Label1
Identifier Naming Conventions
In chapter 1 I discussed the importance of adopting a naming convention and sticking with it. I also proposed a simple naming convention you could use in your programs. However, other naming conventions exist.
Hungarian Notation
Some naming conventions are famous, such as Hungarian notation, formulated by Dr. Charles Simonyi of Microsoft. The concept of Hungarian notation goes something like this: Variable names are prefixed with an abbreviation indicating the variable’s type or class. Table 5-11 lists possible type prefixes.
Prefix | Description |
---|---|
c | signed character |
uc | unsigned character |
i | integer |
ui | unsigned integer |
si | short integer |
li | long integer |
n | an integer number where the actual size is irrelevant |
f | float |
d | double |
s | string of characters |
sz | string of characters, terminated by a null character |
b | an integer or character being used as a boolean value |
by | single byte |
ct | an integer being used as a counter or tally |
p | pointer to a structure or general void pointer |
pfs | file stream pointer |
pfn | pointer to a function |
px | pointer to a variable of class x, e.g. pc, pf, pSubmarine |
v | void type |
To derive identifiers combine the prefixes with names that describe the use of the identifier. Capitalize the first letter of each word in the identifier name. Here are a few examples:
pf_grade_average — pointer to float type
li_number_of_cars — long integer type
You could also apply this naming scheme to functions. The purpose of prefixing the function name would be to indicate what type, if any, the function returned. Here’s a couple examples:
vprintScreen() — returns nothing
igetCount() — returns integer
Again, the important thing about naming conventions is not necessarily which one you choose, but that you use it consistently.
| < Day Day Up > |
|