Line, SavingsAccount
2. Variable names must be in mixed case starting with lower case.
line, savingsAccount
3. Named constants (including enumeration values) must be all uppercase using underscore to separate words. MAX_ITERATIONS, COLOR_RED, PI
4. Names representing methods or functions must be verbs and written in mixed case starting with lower case. getName(), computeTotalWidth()
5. Names representing namespaces should be all lowercase.
model::analyzer, io::iomanager, common::math::geometry
6. Names representing template types should be a single uppercase letter.
template
7. Abbreviations and acronyms must not be uppercase when used as name [4].
exportHtmlSource(); // NOT: exportHTMLSource();
8. Global variables should always be referred to using the :: operator.
::mainWindow.open(), ::applicationContext.getName()
9. Private class variables should have underscore suffix.
class SomeClass { private: int length_; }
10. Generic variables should have the same name as their type.
void setTopic(Topic* topic) // NOT: void setTopic(Topic* value)
11. Plural form should be used on names representing a collection of objects.
vector
12. The prefix n should be used for variables representing a number of objects.
nPoints, nLines
The notation is taken from mathematics where it is an established convention for indicating a number of objects.
13. The suffix No should be used for variables representing an entity number.
tableNo, employeeNo
14. The prefix is should be used for boolean variables and methods.
isSet, isVisible, isFinished, isFound, isOpen
There are a few alternatives to the is prefix that fit better in some situations. These are the has, can and should prefixes:
bool hasLicense();
bool canEvaluate();
bool shouldSort();
15. Abbreviations in names should be avoided.
computeAverage(); // NOT: compAvg();
There are two types of words to consider. First are the common words listed in a language dictionary. These must never be abbreviated. Never write:
cmd instead of command
cp instead of copy
pt instead of point
comp instead of compute
init instead of initialize
etc.
Then there are domain specific phrases that are more naturally known through their abbreviations/acronym. These phrases should be kept abbreviated. Never write:
HypertextMarkupLanguage instead of html
CentralProcessingUnit instead of cpu
PriceEarningRatio instead of pe
etc.
16. C++ pointers and references should have their reference symbol next to the type rather than to the name. float* x; // NOT: float *x; int& y; // NOT: int &y;
More references
No comments:
Post a Comment