Naming
Much of the material in this section is taken from the "C++ Coding Standard" text back in 2002 by Todd Hoff. Copyright (C) 1995-2002 Todd Hoff.
Names are the heart of programming. Only a programmer who understands the system as a whole can create a name that "fits" with the system. If the name is appropriate everything fits together naturally, relationships are clear, meaning is derivable, and reasoning from common human expectations works as expected.
Classes are often nouns. By making function names verbs and following other naming conventions programs can be read more naturally.
Name the class after what it is. If. you can't think of what it is that is a clue you have not thought through the design well enough.
Compound names of over three words are a clue your design may be confusing various entities in your system. Revisit your design. Try a CRC card session to see if your objects have more responsibilities than they should.
Avoid the temptation of bringing the name of the class a class derives from into the derived class's name. A class should stand on its own. It doesn't matter what it derives from. Generic suffices such as "Driver" or "Model" are OK.
Usually every function performs an action, so the name should make clear what it does: checkForErrors() instead of errorCheck(), dumpDataToFile() instead of dataFile().
When confronted with a situation where you could use an all upper case abbreviation instead use an initial upper case letter followed by all lower case letters. No matter what. We use PiiRgbNormalizer instead of PiiRGBNormalizer and PiiFft instead of PiiFFT.
Class Names
Use upper case letters as word separators, lower case for the rest of a word (e.g. PiiOperationCompound, PiiInputSocket).
First character in a name is upper case
Usually, there are no namespaces for classes. Instead, each class name is preceded by "Pii". Namespaces are used in plug-ins (see PiiImage, for example) to wrap functions that do not logically belong to any class. If a class/struct is defined within a namespace, its name must not be prepended with "Pii" (e.g. Pii::IsInteger).
Function Names
Use the same rule as for class names, except for the first letter which must be lower case. This rule makes it easy to distinguish between function calls and constructor calls.
Property getters and setters should be named according to Qt style. That is, a property "fooBar" should have a getter called
fooBar()and a setter calledsetFooBar().
Type Prefixes
Type prefixes should be used to indicate the type of variables, both on the stack and as class attributes. The following is an incomplete list of suitable prefixes:
i-intl-longd-doublef-floatstr- QStringlst- QListvec- QVectormap- QMapmat- PiiMatrixclr- PiiColorp- pointer. Use this for real pointers and for classes that masquerade as pointers, such as PiiSmartPtr.In some occasions, it is useful to be able to easily change a type to another. For example, if there is no need to know that a storage is a QVector instead of QList,
lstcould be used for both.Do not exaggerate. A PiiMatrix<PiiColor<int> >* does not need to be
piclrmatImage.pColorImageis a better name.
Class Attribute Names
All private and protected attribute names must be prepended with an underscore (_), followed by a type prefix.
After the underscore and the type prefix, use the same rules as for class names.
_iChildren- number of children, anint_lstChildren- the children, a QList, whose content type can be anything. We won't go into extremes: template types can be left unspecified._strName- name, a QString_pData- pointer to some data_dSpeed- a double_fSpeed- a float
d, not _d.Function Parameters
The first character should be lower case.
All word beginnings after the first letter should be upper case as with class names.
Function parameters do not need to be prefixed.
Variable Names on the Stack
Follow the same convention as with class attributes, just without the initial underscore.
Type Names
Use the same naming policy as for classes.
#defines and Macro Names
Put #defines and macros in all upper case using '_' as a word separator. Macro parameters should follow the same convention.
Enumerations
For both the type and the enumerated values use the same naming policy as for classes.
Repeat a keyword in all enumerated values. See Pii::PropertyFlag for an example.
Prefer enums for boolean values in function parameters to make the code more readable.
// Instead of this void drive(bool careful); // Prefer this enum DrivingMode { DriveCarefully, DriveCarelessly }; void drive(DrivingMode mode); // See the difference: drive(true); drive(DriveCarefully);
Templates
Generic types with no special meaning are denoted with capital letters. The convention is to name the first type
T, the second oneUand so on, aplhabetically. If the types have similar function, they can also be namedT1,T2etc.If a type has a clear role, the role should be reflected in the type name, which should be formatted according to class name naming conditions.
The names of non-typename template parameters should follow function name naming conditions.
Leave a space after the
templatekeyword.
template <class T> PiiMatrix; template <bool condition, class Then, class Else> struct If;
File Names
Class declaration is always placed into a file whose name equals the class name, and has a .h extension. The same applies to namespaces. Examples: PiiOperation.h, PiiImage.h.
Class definition is placed into a .cc file whose name matches the header file. If the class is a template, the definition is placed into a file that ends with -templates.h. Example: PiiLbp-templates.h. Both .cc and -templates.h may be present.
Exceptions
It is a good idea to make iterators and function objects compatible to stl. In these contexts, function and type names should match stl, whose naming conventions are different. For example, adaptable unary function objects must contain
typedefsforargument_typeandresult_type(notArgumentTypeandResultType).
Add a note
Not a single note added yet. Be the first, add yours.