C++ Coding Standards
C++ Coding Standards
C++ Coding Standards
GlobalSCAPE, Inc.
Last Update: 09 June 2000 (GTH)
Table of Contents
TABLE OF CONTENTS ......................................................................................................................... 1
INTRODUCTION..................................................................................................................................... 1
SOURCE FILES ....................................................................................................................................... 1
STRUCTURE AND NAMING CONVENTIONS ................................................................................................ 1
FILE NAMING ........................................................................................................................................... 1
COMMENTS............................................................................................................................................. 2
FILE COMMENTS ...................................................................................................................................... 2
CLASS, METHOD, AND FUNCTION COMMENTS ........................................................................................ 3
INCLUDE FILES...................................................................................................................................... 5
ASSIGNING NAMES............................................................................................................................... 6
STYLE ....................................................................................................................................................... 7
CLASSES .................................................................................................................................................. 7
FUNCTIONS .............................................................................................................................................. 7
COMPOUND STATEMENTS ........................................................................................................................ 8
FLOW CONTROL STRUCTURES ................................................................................................................. 8
QUALITY FACTORS.............................................................................................................................. 8
DEBUGGING ............................................................................................................................................. 8
CODE COMPLEXITY.................................................................................................................................. 9
CODING PRACTICES ............................................................................................................................ 1
REFERENCES.......................................................................................................................................... 2
Introduction
This document represents the coding standards established at GlobalSCAPE, Inc. Standardization of
coding practices yields higher productivity through increased understanding, better organization, and
higher re-use potential of source code. It is not the intent of coding standardization to inhibit
programmer creativity; rather, coding is standardized only to the extent that the creativity and
production of a programmer is done in an organized, logical, understandable, and repeatable manner.
In short, any software developed at GlobalSCAPE, Inc. should be
• Correct
• Easy to Maintain
In order to achieve these goals, the source that produces the software should:
• Have a consistent style,
• Be easy to read and understand,
• Be free of common type errors,
• Be maintainable by different programmers,
• (Be portable on other architectures)
Refer to the section entitled References at the end of this document for a collection of excellent material
discussing guidelines for programming in C / C++.
This document is based heavily on the coding standards recommended by the Ellemtel-rules found in the
references.
Source Files
Rule 1 Include files always have the file name extension “.h”
Rule 2 Implementation files always have the file name extension “.cpp”
File Naming
Always give a file name that is descriptive and unique for the entire project.
Rule 3 Use the same capitalization techniques used in source code for file names.
Comments
It is imperative for understanding source code that it be well documented. Proper choice of variable
names, method names, class names, etc. provides for self documenting code and reduces the need for
comments within the code.
File Comments
File comments are placed at the beginning of each source file in order to describe, in a high level
fashion, the purpose of the file as well as to track any modifications / revisions made to the file.
return Index;
}
Include Files
The easiest way to avoid multiple includes of files is by using an #ifndef/#define block in the beginning
of the file and an #endif at the end of the file
True portable code is independent of the underlying operating system. For this reason, relative search
paths should be avoided when including files. The processing of such search paths depends on the
compiler and platform. Instead, search paths should be provided in `make' files as options for the
compiler (which is equivalent to including them in the project workspace settings in Visual C++).
Inner classes can be used to declare classes that are member variables of
another class ONLY under highly specialized situations in which industry
Exceptions accepted and proven practices warrant their use. Due to potential reduced
to Rule 12: reusability and possible misunderstanding of purpose, this practice should
be avoided in the majority of situations.
Examples of situations that might warrant inner classes would be iterators and aggregating com
interfaces to implement inner and outer objects.
Every C++ course teaches the difference between the include directives for user-prepared and for library
include files. If the file name is bracketed between "<" and ">", the preprocessor will not search for the
file in the default directory. This reduces the risk of unintended name collisions between user-prepared
and library include files.
Assigning Names
Rule 18 Do not use identifiers which begin with one or two underscores (`_' or `__').
Do not use type names that differ only by the use of uppercase and
Rule 19 lowercase letters.
It is recommended identifiers not be extremely long, to reduce the risk for name collisions when using
tools that truncate long identifiers. At the same time, identifiers should be expressive enough to convey
meaning without requiring too much effort in language deciphering (for example, avoid abbreviations.
This is especially important when dealing with developers of different national origins with different
native languages).
The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to
the ANSI-C standard.
Underscores (`_') are often used in names of library functions (such as " _main" and "_exit"). In order to
avoid collisions, do not begin an identifier with an underscore.
Recommendation: Industry Standard naming conventions, such as the well-established Hungarian
Notation, provide an excellent set of conventions that conform to the above rules. It is recommended
that such naming conventions be used as the basis for development while, at the same time, flexibility
beyond such conventions (while still adhering to the above rules) be allowed.
Recall that the ultimate goal of the above rules (#14-#19) is to provide for a consistent mechanism by
which identifiers are mnemonic in characterizing their type and purpose.
See the section below entitled Coding Practices for further details.
Style
Classes
The public, protected, and private sections of a class are to be declared in
Rule 20 that order (the public section is declared before the protected section which
is declared before the private section).
No member functions are to be defined within the class definition except
for accessor methods (the “get” and “set” functions that manipulate non-
Rule 21 public member variables)
By placing the public section first, everything that is of interest to a user is gathered in the beginning of
the class definition. The protected section may be of interest to designers when considering inheriting
from the class. The private section contains details that should have the least general interest.
A member function that is defined within a class definition automatically becomes inline. Class
definitions are less compact and more difficult to read when they include definitions of member
functions. It is easier for an inline member function to become an ordinary member function if the
definition of the inline function is placed outside of the class definition.
Functions
When declaring functions, the leading parenthesis and the first argument (if
any) are to be written on the same line as the function name. If space
permits, other arguments and the closing parenthesis may also be written on
Rule 22 the same line as the function name. Otherwise, each additional argument is
to be written on a separate line (with the closing parenthesis directly after
the last argument).
Example 18 Right and wrong ways of declaring formal arguments for a function (in function definition)
// Right:
int MyComplicatedFunction( unsigned UnsignedValue,
int IntValue,
char* CharPointerValue,
int* IntPointerValue,
myClass* MyClassPointerValue,
unsigned* UnsignedPointerValue );
// Wrong:
int MyComplicatedFunction( unsigned UnsignedValue, int IntValue,
char* CharPointerValue, int* IntPointerValue, myClass* MyClassPointerValue,
unsigned* UnsignedPointerValue );
Compound Statements
Braces ("{}") which enclose a block are to be placed in the same column,
Rule 24 on separate lines directly before and after the block
“The placement of braces seems to have been the subject of the greatest debate concerning the
appearance of both C and C++ code. We recommend the style which, in our opinion, gives the most
readable code. Other styles may well provide more compact code.” [Ellemtel]
Quality Factors
The following rules are designed to ensure the highest level of quality in all software development.
Quality derives from rigorous enforcement of sound design and coding principles as well as intelligent
design of software that is easily tested and debugged.
Debugging
Functions that make assumptions about variables or member data will
Rule 28 validate those assumptions with an ASSERT macro.
Every non-trivial function shall include a TRACE macro denoting the entry
Rule 29 point to that function. In addition, complex portions of code shall include
the TRACE macro in order to track the flow of the function and program.
Every function shall include, at the beginning of the function definition,
code that will log pertinent information. This code shall be delimited by
preprocessor conditionals so that various compilations do not necessarily
Rule 30 include all such logging.
All exception and error handling shall always include logging regardless of
compilation status (e.g., release versus Beta).
Do not use the preprocessor directive #define to obtain more efficient code;
Rule 31 instead, use templated and/or inline functions.
Constants are to be defined using const or enum; never using #define nor
Rule 32 arbitrary literals.
Liberal use of the ASSERT macro imposes no burden on a release build yet provides a robust
mechanism for debugging an application during development and testing.
Likewise, the TRACE macro provides useful information during initial development but is not included
in release builds.
Both mechanism above require only slight additional code / effort on the developer’s part while yielding
significant gains in the testing and debugging of code.
Inline functions have the advantage of often being faster to execute than ordinary functions. The
disadvantage in their use is that the implementation becomes more exposed, since the definition of an
inline function must be placed in an include file for the class, while the definition of an ordinary
function may be placed in its own separate file.
All constants shall be named rather than arbitrary literal values. This allows both for greater
understanding as well as easier maintenance (through centralized definitions). The preprocessor
performs a textual substitution for macros in the source code that is then compiled. This has a number of
negative consequences. For example, if a constant has been defined using #define, the name of the
constant is not recognized in many debuggers. If the constant is represented by an expression, this
expression may be evaluated differently for different instantiations, depending on the scope of the name.
In addition, macros are, at times, incorrectly written.
Code Complexity
Code shall be written to accomplish, at most, a single logically distinct
Rule 32 function per line.
No more than three levels of nested logic shall be used within a single
Rule 33 function. Additional functions shall be used instead, when required.
No more than 9 control flow keywords (e.g., if, else, while, switch) shall be
Rule 34 used in a single function. Additional functions shall be used instead, when
required.
The complexity of an algorithm need not be accompanied by difficult to understand source code.
Source code maintenance and future usability demands that source code be easy to understand; to that
end, the complexity of source code shall be minimized by the above rules.
Coding Practices
The following is a collection of recommended coding practices. These items fall outside the scope of
formalized standards, yet are considerations that should be taken into account when attempting to write
“good” code at GlobalSCAPE, Inc.