< previous page page_436 next page >

Page 436
Put a space after commas and semicolons, not before.
Parentheses should not have spaces on either side.
Keywords, such as if, should be set off by a space: if (a == b).
The body of a comment should be set off from the // with a space.
Place the pointer or reference indicator next to the type name, not the variable name. Do this:
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
char* foo;
int& theInt;
rather than this:
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
char *foo;
int &theInt;
Do not declare more than one variable on the same line.
Identifier Names
Here are some guidelines for working with identifiers.
Identifier names should be long enough to be descriptive.
Avoid cryptic abbreviations.
Take the time and energy to spell things out.
Short names (i, p, x, and so on) should only be used where their brevity makes the code more readable and where the usage is so obvious that a descriptive name is not needed.
The length of a variable's name should be proportional to its scope.
Make sure 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 letters and underscores to separate the logical words of names, such as SOURCE_FILE_TEMPLATE. Note, however, that these are rare in C++. Consider using constants and templates in most cases.

 
< previous page page_436 next page >

If you like this book, buy it!