Beruflich Dokumente
Kultur Dokumente
Code: SW-GU-003
Version: 1.0
Table of Contents
1. Introduction.......................................................................................................................................................
1.1 Purpose......................................................................................................................................................
1.2 Scope..........................................................................................................................................................
1.3 Definitions, Acronyms and Abbreviations.................................................................................................
1.4 References..................................................................................................................................................
2. Overview...........................................................................................................................................................
3. Comment Convention........................................................................................................................................
Rule 1: Always use documentation comment immediately before declaration..............................................
Rule 2: Do not use C-Style comment.............................................................................................................
Rule 3: Always use comment with //............................................................................................................
Rule 4: Do not leave commented out software in a file................................................................................
Rule 5: Do not include anything except description in comment.................................................................
Rule 6: Do not put comment at the end of line.............................................................................................
4. Naming Convention.........................................................................................................................................
Rule 1: Always capitalize the first letter and letters as word separators of name only................................
Rule 2: Do not use acronyms in names unless the acronym is defined in user document...........................
Rule 3: Always use verb for first word and use verb opposites for opposite actions...................................
Rule 4: Always include a prefix on global and namespace names...............................................................
Rule 5: Always include a prefix on member name.......................................................................................
Rule 6: Always include a prefix on local name............................................................................................
Rule 7: Always 1st letter is lowercase and only letters as word separators is uppercase.............................
Rule 8: Do not include any underscore in name except after variables prefix............................................
Rule 9: Always make all letters of name uppercase.....................................................................................
Rule 10: Always make a combination of plane words by using underscore ( _ ).........................................
Rule 11: Do not pluralize enumerated type name.........................................................................................
Rule 12: Never use number as word.............................................................................................................
Rule 13: Always make sure that argument names and its purposes are the same........................................
Rule 14: Always name header and implement files the same as class name................................................
5. Format Convention..........................................................................................................................................
Rule 1: Always indent new scope with two additional spaces.....................................................................
Rule 2: Do not allow line to exceed 80 characters.......................................................................................
Rule 3: Always use space instead of tab.......................................................................................................
Rule 4: Do not code more than one statement per line.................................................................................
Rule 5: Always add one more empty line between each section..................................................................
Rule 6: Always make level alignment of every code section.......................................................................
Rule 7: Always put pair of braces around statement on new line even single line of code..........................
6. Constant Convention.......................................................................................................................................
Rule 1: Do not use #define to create the constant........................................................................................
Rule 2: Do not use #define to create method-like macros............................................................................
Rule 3: Always declare the constant when possible.....................................................................................
Rule 4: Always define constant for literal except 0, 1 and true, false..........................................................
7. Operator Convention.......................................................................................................................................
Rule 1: Do not add a white space between unary operator and its operand.................................................
Rule 2: Always add a white space before and after to operators..................................................................
Rule 3: Do not use ? operator.......................................................................................................................
TMA Confidential
Page 2 of 79
Code: SW-GU-003
Version: 1.0
Page 3 of 79
Code: SW-GU-003
Version: 1.0
TMA Confidential
Page 4 of 79
Code: SW-GU-003
Version: 1.0
1. Introduction
1.1 Purpose
This document defines C/C++ Coding Standards. These standards lead to consistency and help software
engineers avoid common problems. A development environment that uses C/C++ standards has the following
advantages:
Developers can view any C/C++ software and quickly figure out whats going on.
Developers new to C/C++ are spared the need to develop a personal style.
Developers make fewer mistakes in consistent environments.
Discussions about the appropriateness of software are reduced at reviews.
These guidelines are expected to be adhered to in all future C/C++ software development for the
project.
Existing code will not be required to be rewritten to adhere to these guidelines, but it is suggested that
reasonable efforts are made to adhere to the new guidelines when working on existing code.
1.2 Scope
This document is written specifically for all project of TMA coding in C/C++ language, so it is meant to be as
generic as possible. Because each project can have its own programming standard, so we can change this
standard to be consistent with customer requirements.
In any case, the standard cannot cover every issue of C++ programming, and cannot always match the
different choices that different experiments/projects have made on certain issues. Therefore the different
experiments/projects should, if necessary, tailor this standard to their specific quality requirements; this could
mean to suppress an item or to add additional items.
Each section of this document contains recommended standards in that area, along with a further list of
guidelines. A standard is required to be followed in all future development. If a programmer must violate a
standard, then the reason for doing so must be strong and clearly documented via code comments. Guidelines
are just that.
Individual programmers are encouraged to program in a style which is consistent with the rest of the
programming team, but differing code styles could be acceptable.
These standards recognize that individual programmers have the right to make judgments about how best to
achieve the goal of code clarity. When working on others code, please respect and continue existing styles
which, although they may differ from your own, meet these guidelines. This in turn leads to what is perhaps
the most important aspect of coding style: consistency. Try as much as possible, to use the same rules
throughout the entire program or module.
1.4 References
[1] Optivity Telephony Manager C++ Coding Standards Kyle Blaney 2002/05/10
[2] Coding Standard (Volume Visual C++) 2002/08/07
TMA Confidential
Page 5 of 79
Code: SW-GU-003
Version: 1.0
TMA Confidential
Page 6 of 79
Code: SW-GU-003
Version: 1.0
2. OVERVIEW
This document is organized to many sections. In every section, the rules are formatted as following form:
Rule ##: Name of rule.
Description
This rule is ONLY applied for specific cases or accepted from SOME.
Justification
Why we use this rule?
Example
Give out lines of sample codes which explain to the rule.
Comment Convention: In this section, you stand a good chance to learn how to comment software meet the
standard.
Naming Convention: Name is called, its said that an intent name is considered carefully before naming. So
why we have not to make a standard name? In this section, you will learn how to name a variable, a method, a
class and so on.
Format Convention: Studies have shown that developers have strong expectations about what software
should look like. With format convention your software will be neat and look professional.
Constant Convention: The purpose of this section is to name constant consistently.
Operator Convention: Just at a glance, this section is to give a form of operators should follow.
Control Flow Convention: In section, you will learn samples of control flow and something you should
avoid.
Expression Convention: Expression is combination of operators, its operands and others. Every operator
has its priority. As developer you have a lot of things to remember. Why you must put this mess thing in your
mind. Following these rules in this section, you can be easy to recognize the processing order of expression.
And avoid some unexpected errors!
Pointer and Memory Management Convention: Memory management is endless matter of C++ language.
How to overcome this matter? You can recognize easily how to do in this section.
Variable and Data Type Convention: Variable is not dropped out. Yes, but you have to utilize its steps as
much as possible. This section is to improve ways of using variable and data type as well.
Method Convention: You can be easy to know that method is the core of OOP. C++ is a strong language
supporting OOP. So method convention is a need of C++ coding standard. In this section, you will learn how to
organize your methods effectively.
Class Design Convention: The purpose of this section is to give out valuable ideas that you can take it
together when designing class.
3. COMMENT CONVENTION
Rules in this section ensure that software is formatted in a consistent comment. Following these rules, it is
easy for fixers to fix potential bugs and for designers to upgrade production. The following are rules talking
about comment.
Rule 1: Always use documentation comment immediately before declaration.
Description
Justification
Example
TMA Confidential
Page 7 of 79
Code: SW-GU-003
Version: 1.0
For class:
/**
* ------------------------------------------------------------* Class Name: <<Name of class>>
* Description: <<Whats intent of class>>
* Copyright: Copyright (c) <<YYYY/MM/DD>>
* Company: <<TMA - Software Solutions Vietnam>>
* @author: <<Who takes care this class>>
* @version <<1.0>>
* Note: <<Put your notes here carefully>>
* ------------------------------------------------------------*/
intpStrInStr(constchar*tarstr,//Searchtargetstring
constchar*srcstr)//Searchstring
{
return 0;
}
Justification
Example
TMA Confidential
Page 8 of 79
Code: SW-GU-003
Version: 1.0
Please look in the other rules in this section for more information
Rule 3: Always use comment with //.
Description
Justification
This rule is to make clear what section of code does and what temporary variables purpose
is.
Example
Please look in the other rules in this section for more information
Rule 4: Do not leave commented out software in a file.
Description
Justification
Commented out software obscures behavior. If developers fail to notice that a block of
software is commented out, they will misunderstand the softwares behavior. In addition, it is
difficult for a file comparison tool to get an accurate representation of the changes made
when software is commented out. To the file comparison tool, it appears that one section of
software was modified and another section was added. This is not what actually happened.
Rather, only one section was modified.
Sometimes, old versions of software are left (commented out) in a file so that a reader can
TMA Confidential
Page 9 of 79
Code: SW-GU-003
Version: 1.0
have an idea of the history of changes to the file. This should not be done because
maintaining a history of changes is a job already performed by a projects software revision
control system(Source Safe, Clear Case, and so on). There is no reason to duplicate the effort
of this tool.
Example
Justification
Comments that indicate when and by whom software was changed are not necessary because
that information is stored by the revision control system.
Example
This code was commented out by Bang Mai on Feb 21, 2002.
CR: 009999
Area: WINCS
This section is to execute background service for Web component.
Apply to all comments EXCEPT intentional fall-through in case statements or other tablebased software, and #include statements.
Justification
Example
TMA Confidential
Page 10 of 79
Code: SW-GU-003
Version: 1.0
Definition of structure:
Use the typedef to define the separate name of structure as below
typedef struct{
// Received
Comment of structure
long lLen;
// xxxxxxxxx
Declaration of Member
long lProcNo;
// xxxxxxxxx
long lParamCnt;
// xxxxxxxxx
long lParamLen;
// xxxxxxxxx
}MULTIPOINT;
Name
m_fMaxByte;
// XXXXXX
CHAR
m_chSend;
// XXXXXX
LONG
GetReceive();
Declaration of Property
// Member function
CVsClientCtrl ();
// XXXXXX
Declaration of Method
// Constructor
~CVsClientCtrl ();
// Destructor
protected:
LONG
CheckReceive(LONG lLen);
private:
}
TMA Confidential
Page 11 of 79
Code: SW-GU-003
Version: 1.0
4. NAMING CONVENTION
Rules in this section ensure that software is formatted in a consistent name. Why we make the consistent
name, because name is the heart of programming and if name is exposed, then you will know its own implied
purpose. The following are rules talking about naming.
Rule 1: Always capitalize the first letter and letters as word separators of name only
Description
Justification
Example
Class name
Aa
(depends on project)
TMA Confidential
Page 12 of 79
Code: SW-GU-003
Version: 1.0
Rule 2: Do not use acronyms in names unless the acronym is defined in user document.
Description
Justification
Using acronyms without definition make software hard to read and understand.
Example
Rule 3: Always use verb for first word and use verb opposites for opposite actions .
Description
Justification
Example
TMA Confidential
Page 13 of 79
Code: SW-GU-003
Version: 1.0
Color GetColor();
void WriteColor(const Color color);//should use verb Get
private:
BOOL LeapYear(Year year); //should begin with verb
}
Example
TMA Confidential
Page 14 of 79
Code: SW-GU-003
Version: 1.0
r for reference
b for BOOL, bool
d for DOUBLE,double
i for BYTE, UINT,int
a for array
l for LONG,long
f for FLOAT,float
bstr for BSTR
TMA Confidential
Page 15 of 79
Code: SW-GU-003
Version: 1.0
r for reference
a for array
Order: [s][p, r, a]
Example: sp, sr, sa
Note: After the prefix(es), a underscore( _ ) is added.
Example
TMA Confidential
Page 16 of 79
Code: SW-GU-003
Version: 1.0
CString name;
CString strName;
DWORD age;
DWORD dwAge;
};
Rule 7: Always 1st letter is lowercase and only letters as word separators is uppercase.
Description
Justification
This rule is to distinguish variable from class, struct, union, enum, and method.
Example
Rule 8: Do not include any underscore in name except after variables prefix.
Description
Justification
This rule exists to distinguish a variables prefix from the first word in the variables name.
Example
TMA Confidential
Page 17 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule is to distinguish struct and union name from class name and constant name from
variable name.
Example
0;
0;
Justification
This rule is to distinguish struct and union name from class name and constant name from
others, especially to make struct, union, and constant easy to see.
Example
TMA Confidential
0;
Page 18 of 79
Code: SW-GU-003
Version: 1.0
0;
Apply to enum.
Justification
Enumerated types define a single concept, so do not pluralize the types variable name
because plural names mean multiple concepts. Built-in types (like int and char) are not
pluralized, so enumerated types should not be different.
Example
Apply to class, struct, union, enum, method, variable, method argument, constant, and
namespace.
Justification
Using numbers as words makes confusing because it is too difficult to understand source
code.
Example
Rule 13: Always make sure that argument names and its purposes are the same.
Description
Justification
This makes it is easier to understand how a function works. A user could be able to tell what
the arguments are and how the arguments are used without looking at the functions
definition.
TMA Confidential
Page 19 of 79
Code: SW-GU-003
Version: 1.0
Rule 14: Always name header and implement files the same as class name.
Description
Justification
Following this rule, it helps to browse class under ClassView and FileView modes
synchronizedly in Visual C++ environment. When using class, its clear and lucid to include
header file as well as to declare object of class.
Example
TMA Confidential
Page 20 of 79
Code: SW-GU-003
Version: 1.0
5. FORMAT CONVENTION
Rules in this section ensure that software is formatted in a consistent manner. Studies have shown that
developers have strong expectations about what software should look like. If there is no standard format for
software, then source code looks untidy. So format convention is born. The following are rules talking about
format.
Rule 1: Always indent new scope with two additional spaces.
Description
Justification
This rule exists for consistency and readability. Two spaces are enough so that new scope is
recognized. With two spaces, there is more room on the line with 80 characters.
Example
TMA Confidential
Page 21 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule makes easy to read a hard copy of software and easy to view in code review
meeting.
Example
If (A)
{
. // the length of line should be less than 80 chars
}
Justification
Tab does not have the same interpretation in all cases; space does. For example, if software
uses tabs and is formatted to look good in one text editor, there is no
guarantee that the software will look good in a different text editor.
Note: With VC++ tool, we can configure this function through TOOL/OPTIONS/TAB, then
check Insert spaces.
Example
Justification
Example
TMA Confidential
Page 22 of 79
Code: SW-GU-003
Version: 1.0
Rule 5: Always add one more empty line between each section.
Description
Justification
This make us easy to distinguish every sections and to read when reviewing, fixing and so
on.
Example
TMA Confidential
Page 23 of 79
Code: SW-GU-003
Version: 1.0
void Steal();
}
Justification
Example
Rule 7: Always put pair of braces around statement on new line even single line of code.
Description
TMA Confidential
Page 24 of 79
Code: SW-GU-003
Version: 1.0
Justification
If braces are not placed where they are optional, then they must be added. Because the
software may become more complicated in the future. This is error-prone and can lead to
difficult problems while developing or fixing. It is less error-prone to always include the
braces.
Example
TMA Confidential
Page 25 of 79
Code: SW-GU-003
Version: 1.0
6. CONSTANT CONVENTION
Please refer to rule #12 in section Naming Convention for more information.
Rule 1: Do not use #define to create the constant.
Description
Justification
Apply to constant.
(Refer to Effective C/C++)
When a constant is created using #define, the constants name is replaced by the
preprocessor; it is never seen by the compiler. This leads to confusing compiler error
messages that refer to the value instead of the name and causes problems for a symbolic
debugger. In addition, you can not use the & operator to get the address of
a constant created by using #define.
Define a constant using const does not have any of the above problems.
Example
Apply to constant.
Method-like macros have all the disadvantages described in the justification of Rule Do not
use #define to create constant.
An additional disadvantage of function-like macros is the danger involved with passing in
expressions that have side effects. Methods(possibly created by using templates) can be just
as flexible as macros and have none of the disadvantages of macros.
TMA Confidential
Page 26 of 79
Code: SW-GU-003
Version: 1.0
After maxNumFruit is initialized, numApples has been incremented twice (once when
evaluating the condition and once when returning a value). This is probably not what was
intended.
The following comply:
template <class T> const T& max( const T& value1, const T& value2 )
{
T maxValue = 0;
if( value1 < value2 )
{
maxValue = value2;
}
else
{
maxValue = value1;
}
return maxValue;
}
Apply to local variable, method parameter, class function, class member variable.
Justification
Making items constant avoids inadvertent changing of something that was not meant to be
changed. This makes software more readable and may help the compiler as well.
Example
TMA Confidential
Page 27 of 79
Code: SW-GU-003
Version: 1.0
!! Consider a class that stores information about a book. Each book has a title, an author, and
a publisher. For any given book, this information does not change, so
the members should be constant.
The following do not comply:
class Book
{
public:
Book(CString &r_title, CString &r_author, CString &r_publisher);
private:
CString m_title; //should be constant
CString m_author; //should be constant
CString m_publisher; //should be constant
};
GetValue does not modify any members of the class. Therefore, this function can be made
constant.
TMA Confidential
Page 28 of 79
Code: SW-GU-003
Version: 1.0
Using const in this way promises the compiler that the function does not modify any of the
class members.
Rule 4: Always define constant for literal except 0, 1 and true, false.
Description
Justification
Example
It is impossible to know what this software does without comments. If the software is
written with named constants, it is much easier to read.
The following complies:
const unsigned int BOTTOM_VALUE = 7;
const unsigned int TOP_VALUE = 83;
for (int i = BOTTOM_VALUE; i <= TOP_VALUE; i++)
{
// do something
}
7. OPERATOR CONVENTION
TMA Confidential
Page 29 of 79
Code: SW-GU-003
Version: 1.0
Rule 1: Do not add a white space between unary operator and its operand.
Description
Justification
Example
Unary operators
Apply to assignment operator, logical operator, arithmetic operator, bitwise operator, and
relational and equality operator.
Justification
Example
Assignment operators
Logical operators
Arithmetic operators
Bitwise operators
Relational and equality operators
Shift operators
TMA Confidential
Page 30 of 79
Code: SW-GU-003
Version: 1.0
Justification
The ? operator is a short form if statement that can be very difficult to read. A compiler
creates identical software for an if statement as it does for the ? operator. Therefore, use an if
statement instead.
Example
TMA Confidential
Page 31 of 79
Code: SW-GU-003
Version: 1.0
Justification
When you write a for loop, it is highly confusing and error-prone to change the loop variable
within the loop body rather than inside the expression executed after each iteration.
Example
Justification
Example
TMA Confidential
Page 32 of 79
Code: SW-GU-003
Version: 1.0
{
case 1:
// do this;
break;
case 2:
// do that;
break;
}
Justification
Example
if else statement:
if (A == B)
{
// do something here
}
if (A == B)
{
// do this
}
else
{
// do that
}
for statement:
for (int i = 0; i < MAX_ITEM; i++)
{
// do something here
}
for (int i = 0, int j = MAX_LENGTH - 1; i >= j; i++, j--)
{
// do something here
}
while statement:
while (A == B)
{
// do something here
}
do-while statement:
do
TMA Confidential
Page 33 of 79
Code: SW-GU-003
Version: 1.0
{
// do something here
}
while (A == B);
switch-case statement:
switch (A)
{
case 1:
{
// block of commands;
break;
}
case 2:
// single command;
break;
default:
// do other things;
}
try-catch statement:
try
{
// Write the header file
file.Write((LPSTR)&r_bmpfileHeader, sizeof(BITMAPFILEHEADER));
//Write the DIB header and the bits
file.WriteHuge(lp_bi, dw_dibSize);
}
catch (CFileException* e)
{
::GlobalUnlock((HGLOBAL), hDib);
throw;
}
Rule 4: Do not use goto keyword to replace control flow statements or other purposes.
Description
Justification
Example
TMA Confidential
Page 34 of 79
Code: SW-GU-003
Version: 1.0
{
goto TheLable;
}
9. EXPRESSION CONVENTION
Rules in this section ensure that expression in software is clear and neat. If you browse any fragment of code,
then you can be easy to recognize the processing order of expression. Following these rules, unexpected error
will not occur as error-prone. The following are rules talking about expression.
Please refer to rules in section Format, Variable and Operator Conventions for more information.
Rule 1: Always avoid embedding the assignment within another expression.
Description
TMA Confidential
Page 35 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule exists to make clear source code. Its easy for developer, reviewer, and fixer to
recognize what fragment of code does.
Example
Rule 2: Should put the constant, TRUE/FALSE, .. on the left of comparison expression.
Description
Justification
This rule helps developer, reviewer, and fixer to debug software easily.
Example
TMA Confidential
Page 36 of 79
Code: SW-GU-003
Version: 1.0
Justification
Following this rule, software will look neat. And no error-prone occurs if possible. Because
integer variable will be changed by float variable somehow in the future.
Example
TMA Confidential
Page 37 of 79
Code: SW-GU-003
Version: 1.0
Justification
Example
TMA Confidential
Page 38 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule is to make clear order processing of expression. If the rule is used, then software
look neat and easy to compute manually as needed.
Example
Justification
When you use new and delete instead of malloc and free, you are able to take advantage of
the class librarys memory-management debugging enhancements, which can be useful in
detecting memory leaks. When you build your program with the Release version of MFC,
the standard versions of the new and delete operators provide an efficient way to allocate
and deallocate memory.
There is a note important for us to take a look. The following information is extracted from
Memory Management Topics.
! Mixing the new and delete operators with the resizable memory-allocation functions on the
same memory block will result in corrupted memory in the Debug version of MFC. You
should not use realloc on a memory block allocated with new. Likewise, you should not
allocate a memory block with the new operator and delete it with free, or use the delete
operator on a block of memory allocated with malloc.
Example
TMA Confidential
Page 39 of 79
Code: SW-GU-003
Version: 1.0
else
{
cout << "Memory space allocated for path name\n" ;
delete []path;
cout << "Memory deleted\n";
}
Rule 2: Always use new and delete operators consistently and accurately.
Description
Justification
This rule exists because failure to release all free store memory that you allocate with
operator new is called memory leak. Memory leak is difficult to find, so the best way to
avoid them and go the following.
! When you allocate an array of objects with operator new, use the [] notation with operator
delete to release the array. Programs with loops that use operator new to allocate objects
one at a time should have corresponding loops to release each object with operator delete
without [].
Example
TMA Confidential
Page 40 of 79
Code: SW-GU-003
Version: 1.0
int *p_numItems[MAX_ITEMS];
// Allocate space for a list of items
for (int i = 0; i < MAX_ITEMS; i++)
{
p_numItems[i] = new int(0);
}
//Delete allocated pointer.
for (int i = 0; i < MAX_ITEMS; i++)
{
delete p_numItems[i];
}
Justification
Example
Apply to pointer.
(Standard C)
Standard C++ type conversions allow 0 to be used as a constant of any pointer type. NULL
is macro which defines either as (void*) 0 or as 0 in standard C. If this definition remains in
C++, problems may rises. Because if NULL is defined as void*, then it can not be assigned
an arbitrary pointer without explicit type convention. For this reason, always compare a
TMA Confidential
Page 41 of 79
Code: SW-GU-003
Version: 1.0
pointer with 0 in order to meet standard conversion. This leads to fewer error-prone than
using any conceivable NULL macro.
Example
Justification
Example
TMA Confidential
Page 42 of 79
Code: SW-GU-003
Version: 1.0
Rule 6: Should use exception handling instead of status value and error code.
Description
Justification
For error reporting, exception handling is a more powerful technique than returning status
values and error codes. It allows to separate code that handles errors from the ordinary flow
of control. Because an exception is an object, an arbitrary amount of error information can
be stored in an exception object; the more information that is available, the greater the
chance that the correct decision is made for how to handle the error. In certain cases,
exception handling can be localized to one function along a call chain; this implies that less
code needs to be written, and it is more legible.
Example
TMA Confidential
Page 43 of 79
Code: SW-GU-003
Version: 1.0
Justification
Example
Apply to structure.
Justification
Structures are only used to keep related information together. If a structure has userdefined functions or non-public members, it has more complicated behavior. Should be
a class. By the default, members of structure is public.
Example
TMA Confidential
Page 44 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule decreases a variables life span, which increases software readability. In addition, a
variables constructor is executed upon definition. If the variable is not used, executing the
constructor is unnecessary.
Example
TMA Confidential
Page 45 of 79
Code: SW-GU-003
Version: 1.0
Note: Because retValue variable is used only in the nested if statement. So delaying to
declare this variable is for certain.
The following do not comply:
#include <Vertex.h>
BOOL Rectangle::Rotate()
{
Vertex vertex;
if (FALSE == IsValid() )
{
return FALSE;
}
// use vertex here
return TRUE;
}
Note: If method IsValid() somehow returns FALSE, then it is unnecessary to destruct vertex
variable when this method returns.
Rule 4: Do not use the same variable name in outer and inner scopes.
Description
Justification
Following this rule software is neat otherwise the code would be very hard to understand;
and it would certainly be a major error-prone condition.
Example
with x
with x
TMA Confidential
Page 46 of 79
Code: SW-GU-003
Version: 1.0
int x = 1;
if (a == b)
{
int y;
//do this with y
}
else
{
//do this with x
}
//do others with x
Justification
Example
Please refer to rules #3, #4 in section Class Convention for more information.
Justification
Consistently ordering method arguments in this way makes it easier to read and understand
method.
Example
TMA Confidential
Page 47 of 79
Code: SW-GU-003
Version: 1.0
Apply to method.
Justification
Example
Justification
Example
Dont apply this rule in case we need to check simple error condition at the beginning of the
method.
Justification
If this rule is applied, method implementation is clear and easy to understand which state is
met. Otherwise, it is too hard to understand how this method works and which things need
to be done before exiting the method.
Example
TMA Confidential
Page 48 of 79
Code: SW-GU-003
Version: 1.0
Note: Why the code above is not complied? The answer is it is too hard to know how this
method works while using a lot of return statements. But the most important thing is that
unexpected problem occurs when method DeleteRecord returns false value, because the
database is still locked.
Rule 5: Always specify return type explicitly even though default type is int.
Description
Apply to method.
Justification
Specifying a methods return type helps understanding and takes the decision away from the
compiler.
Example
TMA Confidential
Page 49 of 79
Code: SW-GU-003
Version: 1.0
Apply to method.
Justification
This is a dangerous practice because non-static local variables go out of scope as soon as the
functions execution ends. The memory used for such variables can then be reused any time
after.
Example
Justification
Calling static class method from another class without using the scope operator makes it
appear that an object is subject to change. Using the scope operator makes it obvious that no
object is changing.
Example
TMA Confidential
Page 50 of 79
Code: SW-GU-003
Version: 1.0
class A
{
public:
static void SetMaxAge(int maxAge);
void PrintMaxAge();
};
//File A.cpp
void A::SetMaxAge(int maxAge)
{
s_maxAge = maxAge;
}
void PrintMaxAge()
{
cout << s_maxAge << endl;
}
Note: In C++, when modifying a data member in a class declaration, the static keyword
specifies that one copy of the member is shared by all the instances of the class.
Rule 8: Do not use inline method when they are not really needed
Description
Apply to inline method only if performance is your absolute top priority and a function is
very simple.
Justification
Example
TMA Confidential
Page 51 of 79
Code: SW-GU-003
Version: 1.0
{
public:
inline BOOL IsDirty();
private:
BOOL mb_dirty;
}
//File ApplicationProfile IL.h
inline BOOL ApplicationProfile::IsDirty()
{
return mb_dirty;
}
Justification
Studies have shown that humans have trouble comprehending more than seven discrete
pieces of information at once. Therefore, method that accepts more than seven arguments is
difficult to understand. If a methods requires more than seven
arguments, then this can indicate poor design. Consider revisiting your design or grouping
related arguments into a structure.
Example
TMA Confidential
STUDENT_MARK
maths;
physics;
chemistry;
biologic;
english;
gym;
philosophy;
Page 52 of 79
Code: SW-GU-003
Version: 1.0
};
void InputStudentMark(UINT studentId, STUDENT_MARK &r_studentMark);
Justification
Example
Rule 11: Do not add a white space after method name when calling method.
Description
Apply to method.
Justification
Example
Apply to method.
Please refer to rule #3 in this section for more information.
Justification
One of the main advantages of C++ is that it is a strongly-typed language. Using unspecified
arguments automatically loses this advantage.
If the number of arguments is known but the exact type of arguments is unknown, use
TMA Confidential
Page 53 of 79
Code: SW-GU-003
Version: 1.0
overloaded functions or use a pointer to a base class for each argument. If these are not
possible, use a void * for each argument.
If the type of arguments is known but the exact number of arguments is unknown use a
vector (from the standard template library) argument to pass in the values.
Example
ArbitraryClass::ActionPerformed(int
ArbitraryClass::ActionPerformed(int
ArbitraryClass::ActionPerformed(int
ArbitraryClass::ActionPerformed(int
int arg4);
arg1);
arg1, int arg2);
arg1, int arg2, int arg3);
arg1, int arg2, int arg3,
If the function accepts one hundred to five hundred int parameters, this approach becomes
too tedious. In this case, we can use a vector from the standard template library (STL), which
has a built-in size:
The following also comply:
void ArbitraryClass::ActionPerformed(vector<int>& argList)
{
//use argList.size();
}
Justification
This order emphasizes that which one on a class interface is more important to users of the
class.
The public, protected, and private appear no more than once in a class declaration to make it
easier to distinguish each access section.
Example
TMA Confidential
Page 54 of 79
Code: SW-GU-003
Version: 1.0
};
Justification
Example
TMA Confidential
++ ();
-- ();
+ (Fraction fraction);
- (Fraction fraction);
< (Fraction fraction);
<= (Fraction fraction);
> (Fraction fraction);
>= (Fraction fraction);
== (Fraction fraction);
!= (Fraction fraction);
+= (Fraction fraction);
-= (Fraction fraction);
*= (Fraction fraction);
/= (Fraction fraction);
Page 55 of 79
Code: SW-GU-003
Version: 1.0
//member methods
void GetNumerator(int numerator);
int GetNumerator();
void GetDenominator(int denominator);
int GetDenominator();
//check valid denominator (!= 0)
bool IsValidDenominator();
private:
int mi_numerator;
int mi_denominator;
}
Apply to class.
Justification
The protected section of a class is the information that is inherited by derived classes. If a
class is final class, it does not make sense to have a protected section.
Example
TMA Confidential
Page 56 of 79
Code: SW-GU-003
Version: 1.0
Apply to class.
Justification
Public member variables declaration violates encapsulation principles. Add a pair of methods
to meet encapsulation principle.
Example
TMA Confidential
Page 57 of 79
Code: SW-GU-003
Version: 1.0
Justification
Some members (non-static constant members, reference members, and members with no
default constructor) must be initialized in an initialization list. For other members,
initializing in an initialization list is preferable for the following reasons:
It is consistent with the members that must be initialized in an initialization list.
It becomes obvious that initialization is being done.
It is more efficient to do so.
Example
Rule 6: Always ensure the order of variables in declaration and in init list are the same
Description
Apply to variable.
Justification
If the order of variables in initialization list is the same as the order of variables in
declaration, it is very easy for us to maintain source later. Otherwise the initialization list
obscures the constructors behavior.
Example
//File Rectangle.h
class Rectangle
{
public:
Rectangle();
private:
static const int sm_initialHeightAndWidthInInches = 10;
TMA Confidential
Page 58 of 79
Code: SW-GU-003
Version: 1.0
int m_widthInInches;
int m_heightInInches;
};
The following comply:
//File Rectangle.cpp
Rectangle::Rectangle():
m_widthInInches(sm_initialHeightAndWidthInInches),
m_heightInInches(sm_initialHeightAndWidthInInches)
{
}
The following do not comply:
//File Rectangle.cpp
Rectangle::Rectangle():
m_heightInInches(sm_initialHeightAndWidthInInches),
m_widthInInches(sm_initialHeightAndWidthInInches)
{
}
Justification
A class with virtual functions and a non-virtual destructor can easily create memory leaks
when a base object pointer is initialized with an instance of a derived class. For instance,
when a derived class is destroyed, the destructor for the derived class
does not get called unless the destructor of the base class is virtual.
Example
TMA Confidential
Page 59 of 79
Code: SW-GU-003
Version: 1.0
};
class DerivedClass: public BaseClass
{
public:
DerivedClass();
~DerivedClass();
private:
Shape *mp_shape;
};
BaseClass::BaseClass()
{
mp_color = new Color(red);
};
BaseClass::~BaseClass()
{
delete mp_color;
};
DerivedClass::DerivedClass()
{
mp_shape = new Shape( circle );
};
DerivedClass::~DerivedClass()
{
delete mp_shape;
};
void ArbitraryClass::PerformAction()
{
BaseClass *p_derivedClass = new DerivedClass;
delete p_derivedClass;
}
In this example, the BaseClass and DerivedClass constructors are called when the derived
object is created. However, only the Base destructor is called when the derived object is
deleted.The memory pointed to by mp_shape is never freed. So this causes memory
leak.
Rule 8: Mark virtual for method in derived class when method is virtual in base class
Description
Justification
The virtual keyword is needed only in the base class's declaration of the function; any
subsequent declarations in derived classes are virtual by default. But you have to mark
virtual for method in derived class to make method obvious.
Example
TMA Confidential
Page 60 of 79
Code: SW-GU-003
Version: 1.0
Apply to constructors.
Justification
If you do not supply any constructors, the compiler attempts to generate a default
constructor. Constructors written by humans are better than constructors generated by
compiler, because they are visible and make construction explicitly.
Example
Always apply to classes with pointer data members. Otherwise, if classes without pointer
data members and we do not want to support member-wise of copying and initializing a
object, we declare copy constructor and assignment operator as private attribute when
appreciated.
Justification
Classes with pointer data members that allocate and deallocate memory with constructors
and destructors require copy constructor and assignment operator - overloading.
TMA Confidential
Page 61 of 79
Code: SW-GU-003
Version: 1.0
Note: The compiler uses a copy constructor for objects when you
Initialize one object to another object of the same name.
Pass object by value to method.
Return object by value from method.
If the copy constructor and assignment operator are made private, the compiler ensures that
member-wise copying does not occur in case we do not support member-wise of copying
and initializing a object.
Example
class Tree
{
public:
Tree();
Tree(int hh, int ww);
~Tree();
int GetX();
int GetY();
private:
int m_h;
int m_w;
}
TMA Confidential
Page 62 of 79
Code: SW-GU-003
Version: 1.0
forest1.doSomething();
}
During PerformAnotherAction()s execution, forest1 is copied into forest2. This means that
each instances mp_tree points to exactly the same memory location. When
PerformAnotherAction() finishes execution, forest2 go out of scope, which causes their
destructors to get called forest2 is deleted. Back to function PerformAction(), forest1 was
also deleted, because forest1 and forest2 point to the same memory location. So calling
forest1.doSomething() causes invalid memory access error happens.
To correct this problem - initializations, we must implement the copy constructor.
If we replace above method by the follow, this error occurs again:
void ArbitraryClass:: PerformAnotherAction(Forest forest1)
{
Forest forest2;
Forest2 = forest1 //assignment operator
}
TMA Confidential
Page 63 of 79
Code: SW-GU-003
Version: 1.0
Justification
Example
Justification
This rule is to improve flexibility. That means you can not move included files if using path
in #include statement. Instead, specify the compilers include path.
Example
Rule 3: Should not put methods implementation in header file even inline method.
Description
Justification
This rule exists to separate methods definition from methods implement which allow
developers, who use the method, to focus on methods interface without worrying about
methods implementation.
TMA Confidential
Page 64 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule exists to separate inline methods definition from inline methods implement which
allow developers, who use the method, to focus on methods interface without worrying
about methods implementation
Example
TMA Confidential
Page 65 of 79
Code: SW-GU-003
Version: 1.0
TMA Confidential
Page 66 of 79
Code: SW-GU-003
Version: 1.0
Justification
This rule is to improve compile time when coding and to increase dependencies of every
class.
Example
Forward declarations can be used when the compiler needs to know about the mere
existence of a class, rather than the class size. In the above example, the compiler does not
need to know the size of class Tree when compiling Forest.h because p_tree is a pointer to a
Tree. Pointers are a fixed size regardless of what they point to. The compiler only needs to
know that class Tree exists.
TMA Confidential
Page 67 of 79
Code: SW-GU-003
Version: 1.0
Note: One more thing you must know that forward declaration cant be replaced by #include
in case of circle-calling. Look into the following example:
//File A.h
#include B.h
class A
{
public:
void Method1();
void Method2();
};
//File A.cpp
int A::Method1()
{
//Do something here
}
//In this method, we refer to class B
int A::Method2()
{
//Call Method3 of class B
//Do something here
}
In header file B.h, if we use #include A.h instead of class A, compile error will occur. So
forward declaration is a must in this case.
//File B.h
class A; //This is forward declaration
class B
{
public:
void Method3();
void Method4();
//.
};
//File B.cpp
#include A.h
#include B.h
int B::Method3()
{
//Do something here
}
//In this method, we refer to class A
int B::Method4()
{
//Call Method1() of class A
//Do something here
}
TMA Confidential
Page 68 of 79
Code: SW-GU-003
Version: 1.0
Rule 6: Dont define non-static variable in header file except constant variable.
Description
Justification
If a non-static variable definition exists in a header file and the header file is included in
two .cpp files, the variable is defined twice, which is an error.
Example
Rule 7: Do not define an aggregate variable or member in a header file even constant.
Description
Justification
If aggregate variable is defined in header file, compile error will occur when the header file
is included in more than one implementation file.
In C++, an aggregate is one of the following:
an array
a class or structure with none of the following:
user-defined constructors
private non-static data members
protected non-static data members
non-static reference members
non-static constant members
base classes
virtual functions
Example
TMA Confidential
Page 69 of 79
Code: SW-GU-003
Version: 1.0
Rule 8: Always separate each class from specific header and implementation files.
Description
Justification
This makes easier to read your source code files. This also improves the version control of
the files; for example the file containing a stable class declaration can be committed and not
changed anymore.
Example
TMA Confidential
Page 70 of 79
Code: SW-GU-003
Version: 1.0
B::~B()
{
}
Note: Refer to rule #14 of Naming Convention section for more information.
TMA Confidential
Page 71 of 79
Code: SW-GU-003
Version: 1.0
Example
Follow this checklist as we design classes, because an extensive set of guidelines is helpful.
They do not exactly fit the requirements of all objects, but we should think about, even if
they do not apply to all objects when designing classes.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Initialization
Copy constructor
Default constructor
Assignment operator
Destructor
Conversions
Relational operators
Arithmetic operators
Input and output
Subscription operator[] and iteration operators()
Non-member or member methods implementation
Please refer to book Navigating C++ and Object-Oriented Design, page 331 for more
information.
Justification
If we use pointer in program, opportunities for resource leaks increase in number. Because
we are beforehand with this problem, so we had better overcome it. This rule is to help
preventing resource leaks by using destructor.
Please refer to book More Effective C++ - 35 New Way Improve Your Programs and
Designs, page 45 for more information.
TMA Confidential
Page 72 of 79
Code: SW-GU-003
Version: 1.0
Scenario:
There are three classes in hierarchy tree as following:
LittleAnimal
Puppy
Kitty
LittleAnimal is an abstract base class. Puppy and Kitty are concrete derived classes. A virtual
method, ProcessAction(), handles the necessary specific processing. Look below samplecode:
class LittleAnimal
{
public:
virtual void ProcessAction() = 0;
//.
};
class Puppy : public LittleAnimal
{
public:
virtual void ProcessAction();
//.
};
class Kitty : public LittleAnimal
{
public:
virtual void ProcessAction();
//.
}
void ProcessMultipleAction(istream &dataSource)
{
while (dataSource)
{
LittleAnimal *p_littleAnimal = readNextAnimal(dataSource);
p_littleAnimal->ProcessAction();
delete p_littleAnimal;
}
}
TMA Confidential
Page 73 of 79
Code: SW-GU-003
Version: 1.0
Justification
If you use pointer in your program, opportunities for resource leaks increase in number. If
we are beforehand with, then it had better overcome it. This rule is to help preventing
resource leaks in constructors.
Example
Please refer to book More Effective C++ - 35 New Ways Improve Your Programs and
Designs, page 10 for more information.
Scenario:
In software development, you might come up with design like this:
class Image
{
public:
Image(const string& imageDataFileName);
//.
};
class AudioClip
{
public:
AudioClip(const string& audioDataFileName);
//.
};
class PhoneNumber
{
//holding phone number
};
class BookEntry
{
public:
BookEntry(const string &name, const string address = ,
const string &imageFileName,
const string &audioClipFileName);
~BookEntry();
//.
Private:
string m_theName;
string m_theAddress;
Image *mp_theImage;
AudioClip *mp_theAudioClip;
//.
};
BookEntry::BookEntry(const string &r_name,
const string &r_address = ,
const string &r_imageFileName,
const string &r_audioClipFileName)
:
m_theName(r_name),
m_theAddress(r_address),
mp_theImage(0),
mp_theAudioClip(0)
{
TMA Confidential
Page 74 of 79
Code: SW-GU-003
Version: 1.0
if ( != r_imageFileName)
{
mp_theImage = new Image(r_imageFileName);
}
if ( != r_audioClipFileName)
{
mp_theAudioClip = new AudioClip(r_audioClipFileName);
}
}
BookEntry::~BookEntry()
{
delete mp_theIamge;
delete mp_theAudioClip;
}
What happens if operator new is unable to allocate enough memory for an AudioClip Object
- new AudioClip(r_audioClipFileName). Who deletes the object which mp_theImage already
points to? NO BODY because the destructor wont be called if the object isnt fully
constructed.
Solution 1: Using try-catch to overcome the resource leak in constructor.
BookEntry::BookEntry(const string &r_name,
const string &r_address = ,
const string &r_imageFileName,
const string &r_audioClipFileName)
:
m_theName(r_name),
m_theAddress(r_address),
mp_theImage(0),
mp_theAudioClip(0)
{
try
{
if ( != r_imageFileName)
{
mp_theImage = new Image(r_imageFileName);
}
if ( != r_audioClipFileName)
{
mp_theAudioClip = new AudioClip(r_audioClipFileName);
}
}
catch()
{
delete mp_theImage;
delete mp_theAudioClip;
}
}
This is OK, but dont make sense if mp_theImage and mp_heAudioClip are constant
pointers.
If you try to put these constant pointers in initialization list, then you cant implement trycatch to protect resource leak when exception is thrown. Nevertheless, you can make two
private static member methods that return initialized pointers.
This is perfect but
+ youve been laboring to overcome the problem.
+ you have a bad headache when maintenance because constructor is dispersed across
TMA Confidential
Page 75 of 79
Code: SW-GU-003
Version: 1.0
several methods.
Solution 2: Using auto_ptr instead of raw pointers.
class BookEntry
{
public:
BookEntry(const string &r_name,
const string &r_address = ,
const string &r_imageFileName,
const string &r_audioClipFileName);
~BookEntry();
//.
Private:
string m_theName;
string m_theAddress;
const auto_ptr<Image> mp_theImage;
const auto_ptr<AudioClip> mp_theAudioClip;
};
BookEntry::BookEntry(const string &r_name,
const string &r_address = ,
const string &imageFileName,
const string &audioClipFileName)
:
m_theName(r_name),
m_theAddress(r_address),
mp_theImage( != r_imageFileName ?
new Image(r_imageFileName), 0),
mp_theAudioClip( != r_audioFileName ?
new AudioClip(r_audioClipFileName):0)
{
}
BookEntry::~BookEntry()
{
}
TMA Confidential
Page 76 of 79
Code: SW-GU-003
Version: 1.0
double
dynamic_cast
else
enum
explicit
export
extern
false
float
for
friend
goto
if
inline
int
long
mutable
namespace
new
not
not_eq
operator
or
or_eq
private
protected
public
register
reinterpret_cast
return
short
signed
sizeof
static
static_cast
struct
switch
template
this
throw
true
try
typedef
typeid
typename
union
unsigned
using
virtual
void
votatile
wchar_t
while
xor
xor_eq
Meaning
Set
Get
Set
Get
Verb
Verb
Create
Create
Verb
Destroy
Destroy
Verb
TMA Confidential
N
o.
11
1
2
1
3
1
Words &
phrases
Info
Active
Meaning
Noun
Adjective
First
Information
As the present
object
First
Next
Next
Adjective
Adjective
Page 77 of 79
Code: SW-GU-003
Version: 1.0
Name
Name
Noun
Count, Cnt
Count
Noun
Max
Max
Noun
Min
Min
Noun
QueryDef
Query
Noun
1
0
Table, Tbl
Table
Noun
4
1
5
1
6
1
7
1
8
1
9
2
0
New
New
Adjective
All
All
Adjective
By
By
Preposition
To
To
Preposition
From
From
Preposition
Meaning
Global var
Constant
member variable
static member variable
ANSCIIZ string( NULL-end string)
Sample
GsErrorCode
cValue
SZ
Pre-fix
G
c
m_
S
sz
PSHORT
PUSHORT
PLONG
PULONG
PCH
PUC
PSZ
ps
pus
pl
pul
pch
puc
psz
psErrorCode
pusLineNo
plSeekPointer
pulFunctionNo
pchName
pucColor
pszString
CHAR
UCHAR
c
ch
uc
SHORT
USHORT
s
us
LONG
ULONG
ul
FLOAT
DOUBLE
N/A
n64
u64
f
dbl
N/A
TMA Confidential
SsubLineNo
szString
chMotor
ucColor
sErrorCode
usLineNo
lSeekPointer
ulFunctionNo
fVariable
LogDate
Page 78 of 79
Code: SW-GU-003
Version: 1.0
LogDate.usDay
TMA Confidential
Page 79 of 79