- The length of a variable’s name should be proportional to its scope.
- Be certain identifiers look and sound different from one another to minimize
confusion. - Function (or method) names are usually verbs or verb-noun phrases:Search(),
Reset(),FindParagraph(),ShowCursor(). Variable names are usually abstract
nouns, possibly with an additional noun:count,state,windSpeed,windowHeight.
Boolean variables should be named appropriately:windowIconized,fileIsOpen.
Spelling and Capitalization of Names............................................................
Spelling and capitalization should not be overlooked when creating your own style.
Some tips for these areas include the following:
- Use all uppercase and underscore to separate the logical words of #definednames,
such as SOURCE_FILE_TEMPLATE. Note, however, that these are rare in C++.
Consider using constants and templates in most cases. - All other identifiers should use mixed case—no underscores. Function names,
methods, class, typedef, and struct names should begin with a capitalized letter.
Elements such as data members or locals should begin with a lowercase letter. - Enumerated constants should begin with a few lowercase letters as an abbreviation
for the enum. For example,
enum TextStyle
{
tsPlain,
tsBold,
tsItalic,
tsUnderscore,
};
Comments ......................................................................................................
Comments can make it much easier to understand a program. Sometimes, you will not
work on a program for several days or even months. In that time, you can forget what
certain code does or why it has been included. Problems in understanding code can also
occur when someone else reads your code. Comments that are applied in a consistent,
well-thought-out style can be well worth the effort. Several tips to remember concerning
comments include the following:
- Wherever possible, use C++ single-line //comments rather than the / /style.
Reserve the multiline style (/ /) for commenting out blocks of code that might
include C++ single-line comments.
782 Day 21