Académique Documents
Professionnel Documents
Culture Documents
2015
Purpose
The purpose of this document is to define one style of programming in
C++. The guidelines presented here are not final, but should serve as a basis for
continued work with C++. This collection of guidelines should be seen as a
dynamic document; suggestions for improvements are encouraged.
Programs that are developed according to these guidelines should be:
Correct
Easy to maintain.
2015
In order to obtain insight into how to effectively deal with the most
difficult aspects of C++, the examples of code that are provided should be
carefully studied. C++ is a difficult language in which there may be a very
fine line between a feature and a bug. This places a large responsibility upon
the programmer. C++ allows a programmer to write compact and, in some
sense, unreadable code.
Page 2 of 17
2015
Contents
Coding Guidelines for C++ ........................................................................................... 1
Purpose .............................................................................................................................. 1
Source Code In Files............................................................................................... 5
Naming of Files.................................................................................................... 5
Code Structure ..................................................................................................... 5
Include Files ......................................................................................................... 5
Comments ............................................................................................................. 6
Style ........................................................................................................................... 7
General .................................................................................................................. 7
Assigning Names ................................................................................................ 7
o In General ............................................................................................................. 7
o Class names .......................................................................................................... 8
o Variable names .................................................................................................... 8
o Other names ......................................................................................................... 8
o Prefixes .................................................................................................................. 8
o Classes ................................................................................................................... 9
o Functions ............................................................................................................... 9
Classes..................................................................................................................... 10
Access Right ....................................................................................................... 10
Inline Functions ................................................................................................. 10
Friends ................................................................................................................. 10
Const Member Functions ................................................................................. 10
Constructors and Destructors .......................................................................... 10
Assignment Operators ...................................................................................... 11
Operator Overloading....................................................................................... 11
Member Function Return Types..................................................................... 11
Inheritance .......................................................................................................... 11
Class Templates ................................................................................................. 12
Functions ................................................................................................................ 12
General ................................................................................................................ 12
Function Arguments ......................................................................................... 12
Function Overloading ....................................................................................... 13
Formal Arguments............................................................................................. 13
Page 3 of 17
2015
Page 4 of 17
2015
General
o Every time you deviate from these standards, it must be clearly
documented.
o Optimize the code only if you know that you have a performance
problem. Think twice before you begin.
2015
o The include file should contain all declarations for classes, structures,
and functions.
o An include file should never contain more than one class definition.
o Every include file should have a guard block (such as the "#pragma
once" compiler directive) to prevent multiple inclusion.
o An Include file should contain as few #include directives as possible.
o When using the #include directive, use "file.h" for user-prepared include
files, and <file.h> for library include files.
Comments
o Implementation comments.
Should be compact and easy to find.
Should not duplicate information that is easy to see by looking at the
code.
Should be minimized by choosing proper names for classes, functions
and variables and by properly structuring code.
o Documentation comments.
Every file must contain a beginning comment block that contains the
file's name, author, and a Copyright notice.
All classes must contain a descriptive comment block immediately
before their definition in the header file.
All public members of a class must contain a descriptive
comment block before their definition in the include file.
For public functions this must include descriptions of inputs, outputs
and return values.
Page 6 of 17
2015
Style
General
o Do not use spaces around '.' or '->', nor between unary operators and
operands.
o Indent each nested code block.
o Braces ("{}") that enclose a block are to be placed in the same column, on
separate lines directly before and after the block.
o The flow control primitives if, else, while, for and do should be followed
by a block, even if it is an empty block.
o Only one variable should be declared per line.
o The dereference operator '*' and the address-of operator '&' should be
directly connected with the type names in declarations and definitions.
Assigning Names
o In General
A name should denote meaning so other developers
may easily
2015
For
2015
2015
Classes
Access Right
o Member data in a class should always be private.
o Structs should not be used as they contain only public data.
Inline Functions
o Access and Forwarding functions should be defined inline.
o Constructors and destructors must never be defined inline.
Friends
o Friends of a class should be used to provide additional functions that are
best kept outside of the class.
o Friends are good if used properly. However, the use of many friends
can indicate that the modularity of the system is poor.
Const Member Functions
o A member function that does not affect the state of an object (its instance
variables) is to be declared const.
o If the behavior of an object is dependent on data outside the object, this
data is not to be modified by const member functions.
o Avoid explicit type conversions from a const type to a non-const type.
Constructors and Destructors
o A class that uses "new" to allocate instances managed by the class,
must define a copy constructor.
o All classes that are used as base classes and that have virtual functions,
must define a virtual destructor.
o Avoid the use of global objects in constructors and destructors.
Page 10 of 17
2015
2015
2015
2015
o Avoid the use of numeric values in code; use symbolic values instead.
Variables
o Variables are to be declared with the smallest possible scope.
o Each variable is to be declared in a separate declaration statement.
o Every variable that is declared is to be given a value before it is used.
o If possible, always use initialization instead of assignment.
Floating Point Variables
o Always prefer the floating point type 'double' to that of 'float'.
Containers
o Avoid using C-style arrays for containers. Instead of using a C-style
array use a container class.
Pointers and References
o Pointers to pointers should be avoided whenever possible.
o Use a typedef to simplify program syntax when declaring function
pointers.
Type Conversions
o Avoid using explicit casts where possible.
o Never use C-style explicit type conversions (casts).
o Do not write code that depends on functions that use implicit type
conversions.
o Never convert pointers to objects of a derived class to pointers to objects
of a virtual base class.
o Never convert a const to a non-const.
2015
Memory Allocation
o Do not use malloc, realloc or free.
o Always provide empty brackets ("[]") for delete when deallocating
arrays.
o Avoid global data if at all possible.
o Do not allocate memory and expect that someone else will deallocate it
Page 15 of 17
2015
later.
o Always assign a new value to a pointer that points to deallocated
memory.
Fault Handling
o Check the fault codes that may be received from library functions even if
these functions seem.
o foolproof.
Portability
Sizes of Types
o Do not assume that any two types have the same size.
o Do not assume that a char is signed or unsigned.
o Always set char to unsigned if 8-bit ASCII is used.
Type Conversions
o Do not assume that pointers and integers have the same size.
o Use explicit type conversions for arithmetic using signed and unsigned
values.
Data Representation
o Do not assume that you know how an instance of a data type is
represented in memory.
Order of Execution
o Do not assume that the operands in an expression are evaluated in a
definite order.
o Do not assume that you know how the invocation mechanism for a
function is implemented.
Page 16 of 17
2015
References
[1] Programming in C++, Rules and Recommendations - FN/Mats Henricson and
Erik Nyquist.
[2] Doxygen Manual www.doxygen.org
Page 17 of 17