I have already published an article named "Coding standards in C language". Some concpets in C++ are same in C. So i have not repeated those here.
In this article i have explained some guidelines especially for C++ .
1. File Structure
Rule 1 (Required)
Implementation files in C++ always have the file name extension ".cpp".
Rule 2 ( Required )
An include file should not contain more than one class definition.
2. Naming convention
Rule 3 ( Advisory )
Variables representing GUI components should be suffixed by the component type name. Eg. submitButton, mainWindow etc.
Rule 4( Required )
All private member variables names should be preceded by an underscore
All MFC Class Wizard member variables names should be preceded by ”m_”
Local Variable -> variableName
Private Variable -> _variableName
MFC Class Wizard Variable -> m_variableName
3. Control flow
Rule 5 ( Required )
Follow each non-empty case statement block in a switch statement with a break statement.
Rule 6 ( Required )
Do not alter a control variable in the body of a for ,do or while statement.
Rule 7 ( Required )
Do not use 'goto'.
Rule 8 ( Advisory )
For functions with non-void return type, ensure all paths have a return statement that contains an expression of the return type.
Rule 9( Required )
Avoid the use of continue.
Rule 10 ( Required )
The break statement shall not be used (except to terminate the cases of a switch statement).
Rule 11 ( Required )
A switch statement must always contain a default branch which handles unexpected cases.
Rule 12 ( Advisory )
A switch expression should not represent a Boolean value.
Rule 13( Advisory )
Only expressions concerned with loop control should appear within a for statement.
4.Classes
Rule 14 ( Required )
The public, protected, and private sections of a class are to be declared in that order
Rule 15 ( Required )
No member functions are to be defined within the class definition.
Rule 16 ( Required )
Class data members should be explicitly declared to be private by using the private keyword
Rule 17 ( Advisory )
Every class should have a constructor, a destructor and a copy constructor defined
Rule 18 ( Required )
Organize class member functions in the source code in the same order they are declared in the class interface
5.Functions
Rule 19 ( Required )
Always provide the return type of a function explicitly
Rule 20 ( Required )
Pass arguments of class types by reference or pointer.
Rule 21 ( Required )
Specify the name of each function parameter in both the function declaration and the function definition. Use the same names in the function declaration
and definition.
Rule 22 ( Advisory )
Use pass-by-reference in preference to pass by value or pass by pointer.
Rule 23 ( Required )
Functions shall always be declared at file scope.
Rule 24 ( Required )
Functions shall always have prototype declarations and the prototype shall be visible at both the function definition and call.
Rule 25 ( Required )
Identifiers shall either be given for all the parameters in a function prototype declaration,or for none.
Rule 26 ( Required )
Functions with no parameters shall be declared with parameter type void.
Rule 27( Advisory )
A function should have a single point of exit.
Rule 28( Advisory )
If a function returns error information, then that error information should be tested.
6.Variables and Constants
Rule 29( Required )
Initialize all objects at definition. Never use an object before it has been given a value.
Rule 30 ( Advisory )
If possible, always use initialization instead of assignment.
Rule 31 ( Required )
Constants are to be defined using const or enum; never using #define.
Rule 32 ( Advisory )
Use suffixes L, U, F,and UL for all constants of type 'long', 'unsigned int' ,’Float’and 'unsigned long'.
Rule 33 ( Required )
There shall be no unreachable code. Examples of unreachable code are given below:
- Code after a goto or return.
- Code in a switch body, before the first label.
- Code after an infinite loop (a loop with a constant controlling expression that evaluates to true).
- Code after a function call of a function that is known not to return.
- Code after break in a switch clause.
- Code after an if statement that is always taken where the end of the dependent statement is unreachable.
- Code after an if statement where the ends of both dependent statements are unreachable.
- Code after a switch statement where the ends of all clauses are unreachable.
7. Memory Allocation
Rule 34 ( Required )
Do not use malloc, realloc or free.
Rule 35 ( Required )
If memory allocation is done for an Array using new operator. Be careful that you delete the whole array .
8. Thread Handling
Rule 36 ( Required )
If global variables can be accessed by more than one thread, code altering the global variable should be enclosed using a synchronization mechanism
such as a mutex. Code accessing the variable should be enclosed with the same mechanism.
Rule 37 ( Required )
Always design a Thread Safe Class
Whenever threads are used proper synchronization should be done.Since one thread could be updating the contents of a structure while another thread is reading the contents of
the same structure. It is unknown what data the reading thread will receive: the old data, the newly written data, or possibly a mixture of both. To avoid accessing incorrect data
always use Critical Section (Lock and Unlock functions)
9. Synchronization
Rule 38 (Required)
It is important to release the locks in the same order they were acquired to avoid deadlock situations.
10. Optimization Techniques
Pass Class Parameters by Reference
Passing an object by value requires that the entire object be copied (copy ctor), whereas passing by reference does not invoke a copy constructor, For strings, passing
by reference is almost 30 times faster! For the bitmap class, it's thousands of times faster. Passing a complex object by reference is almost 40% faster than passing by
value. Only ints and smaller objects should be passed by value, because it's cheaper to copy them than to take the dereferencing hit within the function.
Postpone Variable Declaration as Long as Possible
For maximum efficiency, declare variables in the minimum scope necessary, and then only immediately before they're used.The only time where it may make sense to declare an
object outside of the scope where it's used is in the case of loops. An object declared at the top of a loop is constructed each time through the loop.If the object naturally changes
every time through the loop, declare it within the loop. If the object is constant throughout the loop, declare it outside the loop.
// Declare Outside (b is true half the time)
T x;
if (b)
x = t;
// Declare Inside (b is true half the time)
if (b)
T x = t;
Prefer Initialization over Assignment
Initializing an object invokes the object's copy constructor. Defining and then assigning an object invokes both the default constructor and then the assignment operator.
Reduce the number of parameters
Function calls with large number of parameters may be expensive due to large number of parameter pushes on stack on each call. For the same reason, avoid passing
complete structures as parameters.
Don't define a return value if not used
The called function does not "know" if the return value is being used. So, it will always pass the return value. This return value passing may be avoided by not defining
a return value which is not being used.