Professional Documents
Culture Documents
Table of Contents
1. Purpose.............................................................................................. 5
2. Scope ................................................................................................. 5
3. Definitions And Acronyms .................................................................. 5
4. Guidelines .......................................................................................... 5
4.1. Coding Guidelines 5
4.1.1. Introduction .......................................................................................... 5
4.1.2. Indentation ........................................................................................... 6
4.1.3. Functions .............................................................................................. 6
4.1.4. Use of Spaces........................................................................................ 7
4.1.5. Variable Declaration ............................................................................... 8
4.1.6. Class Declaration ................................................................................... 8
4.1.7. Expressions ......................................................................................... 10
4.1.8. The Loop statements ............................................................................ 10
4.1.9. Use of goto,continue,break .................................................................... 11
4.1.10. The switch statement............................................................................ 12
4.1.11. The return statement.......................................................................... 13
4.1.12. Initialize all Variables ............................................................................ 13
4.1.13. Init Idiom for Initializing Objects ............................................................ 13
4.1.14. A Line Should Not Exceed 78 Characters.................................................. 15
4.2. Naming Conventions 15
4.2.1. Naming Macros .................................................................................... 15
4.2.2. Naming of Classes ................................................................................ 16
4.2.3. Variable Naming .................................................................................. 16
4.2.4. Hungarian Notation Prefixes................................................................... 17
4.2.5. Naming Methods .................................................................................. 18
4.2.6. Naming Structures and Type Definitions .................................................. 18
4.2.7. Global Constants .................................................................................. 19
4.2.8. Global Variables ................................................................................... 19
4.2.9. Reference Variables and Functions Returning References ........................... 19
4.2.10. Pointer Variables .................................................................................. 20
4.3. Documentation 20
4.3.1. Comments .......................................................................................... 20
4.3.2. Readability of Comments....................................................................... 21
4.3.3. Comments for a Block........................................................................... 21
4.3.4. Inline Comments.................................................................................. 21
4.3.5. Header for an implementation file........................................................... 22
4.3.6. Function Header in the Interface File ....................................................... 22
4.3.7. Function Header in the Implementation File ............................................. 22
4.3.8. Function Documentation in the Documentation File ................................... 23
4.3.9. Preprocessor Directives ......................................................................... 24
4.3.10. Directory Documentation....................................................................... 24
4.4. Program structure 24
4.4.1. Size of Files........................................................................................ 24
4.4.2. Size of Functions ............................................................................... 25
4.4.3. Format of an ASCII Documentation File .................................................. 26
4.4.4. Extern Declarations ............................................................................. 28
4.4.5. Multiple Levels of Control Structures ....................................................... 28
4.4.6. Static and Extern ................................................................................. 28
4.4.7. Error Handling, Dates and Reports .......................................................... 28
4.4.8. Naming Class Files ............................................................................... 28
ADITI/QMS/STDC++/1.0 Page 2 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
ADITI/QMS/STDC++/1.0 Page 3 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
ADITI/QMS/STDC++/1.0 Page 4 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
1. Purpose
The objective of this case study is to provide a set of standards for 'C/C++'
programming to produce software that is:
Maintainable
Reliable
Portable
Consistent
Re-usable
2. Scope
The Scope of this procedure is to set out guidelines for use in software
development and the extent of use of this guidelines is to the discretion of the
project manager. C++ Programming Standards is a set of standards to be used by
programmers while developing application/system software in 'C/C++' language.
This case Study can also act as a starting point for creating and tailoring 'C++'
programming standards specific to any project.
'C' being a subset of 'C++', this document also contains guidelines and standards
valid for C programming.
4. Guidelines
4.1. Coding Guidelines
4.1.1. Introduction
The rules specified in this section are meant to:
ADITI/QMS/STDC++/1.0 Page 5 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
4.1.2. Indentation
One level of indentation will be 4 columns. This means that text on
the indented line will start on the fifth column, relative to the
starting column of previous indentation level.
Example:
4.1.3. Functions
The following rules will ensure that function blocks are visually
demarcated in a scan of the listing of any program:
ADITI/QMS/STDC++/1.0 Page 6 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
char* CGetReportName (
char * pchReport // comment
)
{
char * pchRep; // comment
int iLineCount; // comment
int .....; // comment
while (.........................)
{
if (....................)
{
.....
}
else
{
......
}
....
.....
}
return pchRep;
}
(i).The operator '->' : Do not leave any space before and after
the operator.
(ii).The operator '.' : Do not leave any space before and after the
operator.
ADITI/QMS/STDC++/1.0 Page 7 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Examples :
szFriend = pszNames[1] ;
szBestFriend = pszNames [(iStart + iOffset)] ;
iSum = ( (iFirst + iSecond) / iThird ) * iFourth ;
oCancelButton.Press () ;
iResult = sqrt( (5 + 7) * 100 ) ;
#define MAX_SIZE 50
int szName[MAX_SIZE] ;
e). Leave single spaces before and after a semicolon (;). If the
semicolon ends a statement, no space is required after the
semicolon.
a). Do not declare more than one variable in a line with a comment
on the same line.
b). Align variable names vertically, as far as possible.
c). Declare all related variables together.
Example :
ADITI/QMS/STDC++/1.0 Page 8 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
...
};
ADITI/QMS/STDC++/1.0 Page 9 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
4.1.7. Expressions
Example:
Example :
for ( iValue = LOW_LIMIT ; iValue <= HIGH_LIMIT ;
iValue++ )
{
cout << .... ;
}
ADITI/QMS/STDC++/1.0 Page 10 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
If all the three expressions do not fit into the same line, put one
in each line, indenting the second and the third to align with the
start of the first expression.
Examples :
for ( <expression-1> ;
<expression-2> ;
<expression-3> )
{
...........
...........
}
for (...)
{
while (...)
{
...
if (disaster)
goto error;
}
}
...
error:
ADITI/QMS/STDC++/1.0 Page 11 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
while (TRUE)
{
...
// A lot of code
...
if (/* some condition */) {
continue;
}
...
// A lot of code
...
if ( i++ > STOP_VALUE) break;
The opening and closing braces for the switch should come
vertically aligned with the switch.
The cases should come indented by one level, relative to the
switch. The opening and closing braces of case should come
vertically aligned with case.
The default case should always be present and trigger an error
if it should not be reached, yet is reached.
Example :
switch ( iCode )
{
case MAX_CUR :
{
...
break;
}
case MIN_CUR :
{
...
ADITI/QMS/STDC++/1.0 Page 12 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
break;
}
case NO_OPT :
{
...
break;
}
....
....
default :
{
......
......
}
}
}
ADITI/QMS/STDC++/1.0 Page 13 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
class Test
{
public:
Test()
{
Init(); // Call to common object initializer
}
Test(int val)
{
Init(); // Call to common object initializer
mVal= val;
}
private:
int mVal;
String* mpName;
void Init()
{
mVal = 0;
mpName= 0;
}
}
ADITI/QMS/STDC++/1.0 Page 14 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Example:
Good:
#define MAX_LINE_LEN 80
#define DOIT(x) ( (x) + 1 )
#ifdef SPECIAL
#define STRCPY (t,s) MyStrCpy (t , s)
#else
#define STRCPY (t,s) strcpy (t , s)
#endif
...
STRCPY ( pszTarget , pszSource ) ; // Macro used for string
copy
Bad:
#define Max_line_len 80
#define DOIT(x) ( (x) + 1 )
#ifdef SPECIAL
#define strcpy (t,s) MystrCpy (t , s)
// attempt to replace standard strcpy
#endif
...
strcpy ( pszTarget , pszSource);
ADITI/QMS/STDC++/1.0 Page 15 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Example
namespace Customer
{
class CNameOneTwo
class Cname
}
ADITI/QMS/STDC++/1.0 Page 16 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
ADITI/QMS/STDC++/1.0 Page 17 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Example:
Good:
OpenFile() ;
CloseFile() ;
DoFunction() ;
poMyObject->MoveRight( 10 ) ;
Bad:
ADITI/QMS/STDC++/1.0 Page 18 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
{
USHORT usFieldOne;
BYTE byFieldTwo;
} MYSTRUCT // Type name similar to structure
name
typedef MYSTRUCT *PMYSTRUCT;
// Hungarian prefix "P" on
// pointer type
Example
Logger gLog;
Logger* gpLog;
class Test
{
public:
void DoSomething(StatusInfo& rStatus);
StatusInfo& rStatus();
const StatusInfo& Status() const;
ADITI/QMS/STDC++/1.0 Page 19 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
private:
StatusInfo& mrStatus;
}
4.3. Documentation
4.3.1. Comments
Comments are an essential, mandatory part of every program.
Every source file must have a block comment at the beginning
explaining what the file is for and these block comments must
precede every function and/or logical block of code. Line
comments must be used to clarify any line of code that could be
ambiguous. If there is any doubt whether another person will
understand what the code is doing, explain it in a comment!
ADITI/QMS/STDC++/1.0 Page 20 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Recommended :
if (iCurs = = 0) // If at start of list
Not Recommended :
if (iCurs = = 0) // if the cursor is zero
Recommended : Tells what this API call does for the program
DOSEnterCritSec(); // Set Crit so IDM_DONE isn't processed .
// ... until this thread has terminated
Not Recommended :
short sReturnCode = 0; // return code variable
Do not restate the obvious
ADITI/QMS/STDC++/1.0 Page 21 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
// *******************************************************************
//
// History :
// A list of changes made to this file
//
// Purpose :
// The description of the use or purpose of this
// module (from a user standpoint)
//
// Methods :
// List names of the methods implemented in this file.
// Arguments and return values need not be mentioned.
//
// ******************************************************************
Begin each function declaration in the header file with the a block
of comments as follows (the function comment block should
precede the function name). There should be only one function
header corresponding to all the overloaded versions of a function,
and the prototypes for all the overloaded versions should be
given immediately after this header.
// ***************************************************************
// method : Name of the function
// ***************************************************************
Prototypes for all the overloaded versions of this function
ADITI/QMS/STDC++/1.0 Page 22 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
// ***************************************************************
// method : Name of the function
// **********************************************************//
*****************************************************************
//
// implementation : MODULE NAME
//
// *****************************************************************
//
// COPYRIGHT STRING
//
// PROJECT : PROJECT NAME
// MODULE : Module name
// VERSION : Version number
// AUTHOR : Name of author
// DATE : Date of initial release
// SCCS : SCCS string
// SYSTEM : Compiler / OS
// FILE : Name of this file (with relative path)
// *****************************************************************
//
// History:
// A list of changes made to this file
//
// implementation :
*****
// return value :
// A description of the return value of this function.
//
// purpose :
// A description of the use of this function.
//
// possible errors :
// List the limitations, possible errors in use, and the
// exceptions.
//
// Implementation :
// Describe the implementation logic of this function
// ***************************************************************
ADITI/QMS/STDC++/1.0 Page 23 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
DESCRIPTION :
A description of the use or purpose of this function
RETURN VALUE :
A description of what is returned by this function. Here,
we explain not just the return type, but the actual
significance of the return value.
ERRORS :
A description of the possible errors with this function, restrictions in
usage, etc.
#ifdef ORACLE
....
....
#endif // ORACLE
ADITI/QMS/STDC++/1.0 Page 24 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
files, removing the lower level commonly used functions into other
files.
// ************************************************************
//
// implementation : MODULE NAME
//
// ************************************************************
//
// COPYRIGHT STRING
//
// PROJECT : PROJECT NAME
// MODULE : Module name
// VERSION : Version number
// AUTHOR : Name of author
// DATE : Date of initial release
// SCCS : SCCS string
// SYSTEM : Compiler / OS
// FILE : Name of this file (with relative path)
// ************************************************************
//
// History:
// A list of changes made to this file
//
// Implementation :
// A description of the implementation logic for this
// module (class)
//
// methods :
// List names of the methods (functions) implemented
// in this module.
// Arguments and return values need not be mentioned
here.
//
// Friend functions :
// A list of all the friend functions of this class
//
// ************************************************************
//
// interface
ADITI/QMS/STDC++/1.0 Page 25 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
// *********
All the includes for this source file
// global constants
// ****************
All the global constants for this source file
// global types
// ************
All the global types for this source file
// global macros
// *************
All the global macros for this source file
// private methods
// ***************
All the private member functions are implemented here
// protected methods
// *****************
All the protected member functions are implemented here
// exported methods
// ****************
All the exported are member functions implemented here
General Description:
A general description of the functionality of this
module
General Topics:
Restrictions:
******************************************************************
//
// documentation : MODULE NAME
ADITI/QMS/STDC++/1.0 Page 26 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
//
// ******************************************************************
//
// COPYRIGHT STRING
//
// PROJECT : PROJECT NAME
// MODULE : Module name
// VERSION : Version number
// AUTHOR : Name of author
// DATE : Date of initial release
// SCCS : SCCS string
// SYSTEM : Compiler / OS
// FILE : Name of this file with relative path
// ******************************************************************
Module Name :
Class Name :
Interface File :
Error File :
Implementation File :
Library :
Maturity Index :
Base Classes :
Imported Modules :
Imported Classes :
Friend Classes :
Friend Functions :
this module
Attribute List:
-Protected
All the protected data members
-Public
All the public data members
Service list:
- Protected:
Name of all the protected member functions
- Public:
Names of all the public member functions
Description of Services:
ADITI/QMS/STDC++/1.0 Page 27 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
- Protected :
Describe all the protected member functions.
The description should include the names of the
member functions, an explanation of the purpose,
arguments, and return values.
-public :
Describe all the protected member functions.
The description should include the names of the
member functions, an explanation of the purpose,
arguments, and return values.
Example ClassName.h
ADITI/QMS/STDC++/1.0 Page 28 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
4.5.6. I / O Functions
Calls to I/O functions should always be checked for error returns.
ADITI/QMS/STDC++/1.0 Page 29 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
#define LEFT_MARGIN 5
#define RIGHT_MARGIN (LEFT_MARGIN + 65)
Default Constructor
Virtual Destructor
Copy Constructor
ADITI/QMS/STDC++/1.0 Page 30 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Assignment Operator
4.5.12. Justification
class Planet
{
public:
// The following is the default constructor if
// no arguments are supplied:
//
Planet(int radius= 5);
4.5.13. Use the Same Form in Corresponding Calls to new and delete
If you use new with [ ] for creating an array, use [ ] with delete
for properly deleting the array. If you do not use [ ] with new do
not use [ ] with delete.
Example :
ADITI/QMS/STDC++/1.0 Page 31 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
There are two basic strategies for naming: root that name at
some naming authority, like the company name and
division name; try and make names globally independent.
ADITI/QMS/STDC++/1.0 Page 32 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
#ifndef ClassName_h
#define ClassName_h
#endif // ClassName_h
When the include file is not for a class then the file name
should be used as the guard name.
ADITI/QMS/STDC++/1.0 Page 33 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
ADITI/QMS/STDC++/1.0 Page 34 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Example:
Good:
Bad:
ADITI/QMS/STDC++/1.0 Page 35 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Example:
int
XYZMyFunction ( void )
{
#define BUFSIZE 128
{
#define MYADD (a,b) ( ( a ) + (b) + 1 )
...
#undef MYADD // make MYADD invisible, simulating
// local scope
}
return ( 0 ) ;
#undef BUFSIZE // make BUFSIZE invisible,
simulating
// function scope
}
#define NULL 0
#define TRUE 1
#define FALSE 0
Bad :
#define EQUAL ==
#define NOTEQUAL !=
#define GREATERTHAN >
ADITI/QMS/STDC++/1.0 Page 36 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Very Bad :
#define begin {
#define end }
#define repeat do
#define until while
Note :
Example:
Good :
#define ROUNDINGFACTOR 3
int iSum = iXOne + iXTwo;
int iResult = (iSum * iSum);
iRounded Result = ( iResult + ROUNDINGFACTOR ) &
~ROUNDINGFACTOR;
Bad :
Example :
Good :
if ( iX ! = 0 )
{
.... ;
}
fCanIProceed = (iCursor > 0 ) ;
Bad :
ADITI/QMS/STDC++/1.0 Page 37 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
if ( iX )
{
.... ;
}
fCanIProceed = iCursor ;
ADITI/QMS/STDC++/1.0 Page 38 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Good:
ADITI/QMS/STDC++/1.0 Page 39 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Bad:
4.8. PORTABILITY
4.8.1. Case Sensitivity
Do not assume that the linker is case sensitive. Define variables
and functions to be unique in a case insensitive environment.
ADITI/QMS/STDC++/1.0 Page 40 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
4.8.8. Recursion
If recursion is used, ensure that there is a terminating statement,
and also ensure that there is convergence of the recursion. Be
careful on the effects of recursion on local static variables and
global variables. Recursion may be compact and elegant but not
necessarily more efficient, and hence could be rewritten in a non-
recursive manner, if found suitable.
Example:
If assignment (=) is satisfactory, do not go for memcpy () .
ADITI/QMS/STDC++/1.0 Page 41 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
This will ensure that your programs will successfully compile with
any severity level. Moreover it brings to your notice, all possible
errors and warnings.
Public data members and member functions are accessible for all
classes and functions.
A thumb rule is that most of the data members of any class will
normally be private, and, most of the classes will contain public
member functions.
ADITI/QMS/STDC++/1.0 Page 42 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
ADITI/QMS/STDC++/1.0 Page 43 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
If we mix printf and cout in the same program, we are bound to get
problems. All the output by printf will appear together and all the
output by cout will appear together.
ADITI/QMS/STDC++/1.0 Page 44 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Friend classes and functions can access private data members and
member functions of a class. The use of friend may make things
confusing. So the use of friend classes and functions is not
encouraged.
void
TSTDoSomething (
char * pchData ) ; // This version takes a pointer to char
void
TSTDoSomething (
int iData ) ; // This version takes an int
5. Best Practices
5.1. Use #if Not #ifdef
Use #if MACRO not #ifdef MACRO. Someone might write code like:
#ifdef DEBUG
temporary_debugger_break();
#endif
Someone else might compile the code with turned-of debug info like:
cc -c lurker.cpp -DDEBUG=0
ADITI/QMS/STDC++/1.0 Page 45 of 46
Aditi Technologies Pvt. Ltd. C, C++ Coding Standards
Alway use #if, if you have to use the preprocessor. This works fine, and
does the right thing, even if DEBUG is not defined at all (!)
#if DEBUG
temporary_debugger_break();
#endif
If you really need to test whether a symbol is defined or not, test it with the
defined() construct, which allows you to add more things later to the
conditional without editing text that's already in the program:
#if !defined(USER_NAME)
#define USER_NAME "john smith"
#endif
Justification
Reduce the number of lines
Improve the performance
Used to form macros for checking conditions
Example
ADITI/QMS/STDC++/1.0 Page 46 of 46