Be careful about how you name identifiers. The name should be long enough to be descriptive, and it is worth the extra effort to spell names out. Consider limited use of Hungarian notation. For example, prefix pointers with p
(pSomePointer
) and references with r
. Don't get carried away, however, as Hungarian notation breaks down pretty quickly with user-defined types.
Use a consistent prefix for member variables. I've used a single letter (e.g. dAge
is a data member, while age
is just a local variable). I've also used my (myAge
) and its (itsAge
). Microsoft uses m_ as its member variable prefix (m_age
);
The length of a variable's name should be proportional to its scope. Within a for
loop, it is fine to have a variable named j
, but if the variable is going to live for any time at all it should have a more descriptive name.
Note, the ANSI standard has changed the scoping rule for for
loops. Variables declared in the header used to be scoped to the outer loop:
int SomeFunction()
{
for ( int i = 0; i < Max_Int; i ++ )
{
}
for (i = 0; i < Max_Int; i ++ )
{
}
}
With the change to the standard, the variable i
is now scoped to the for
loop itself, thus this will no longer compile (if you are using an ANSI compatible compiler) as i
is not declared for the second for loop.
Avoid having two variables whose names differ only by capitalization. While the compiler won't be confused, you certainly will be. Adopt a capitalization strategy — variables should begin with a lower case letter, and methods begin with an upper case letter.
Consider making variable names abstract nouns (theCount
, windSpeed
, windowPosition
), while methods should be verb/noun phrases like Find()
, ShowButton()
, and MoveWindow()
. We used to call this kill dwarf notation, from the old interactive computer games where you specified a verb (move
, lift
, kill
) and a noun (scroll
, wand
, dwarf
).