Style Guide
Google programming style has been widely adopted in software development.
Summary (for C++)
Header Files
Be self-contained
Avoid forward declaration
The format of the symbol name in
#define
guard:<PROJECT>_<PATH>_<FILE>_H_
Names and order of includes
Related header (the header that the current
.cc
file plans to implement or test)C system headers
C++ standard library headers
Other libraries' .h files
Your project's .h files
Conditional includes
Scoping
With few exceptions, place code in a namespace.
Namespaces should have unique names based on the project name, and possibly its path.
Do not use using-directives (e.g., using namespace foo). -- This pollutes the namespace!
Do not use inline namespaces.
Do not use namespace aliases at namespace scope in header files.
Class
Use a
struct
only for passive objects that carry data; everything else is aclass
.Use a
struct
instead of a pair or a tuple whenever the elements can have meaningful names.All inheritance should be
public
.Overload operators judiciously. Do not use user-defined literals.
Define operators only on your own types.
Make classes' data members
private
, unless they are constants.
Functions
Non-optional input parameters should usually be values or
const
references.Use overloaded functions (including constructors) only if a reader can easily get it.
Default arguments are allowed on non-virtual functions when the default is guaranteed to always have the same value.
Use trailing return types only where using the ordinary syntax (leading return types) is impractical or much less readable.
Google-Specific Magic
Prefer to have single, fixed owners for dynamically allocated objects.
Prefer to transfer ownership with smart pointers.
Use cpplint.py to detect style errors.
Other C++ Features
We do not use C++ exceptions.
Avoid using run-time type information (RTTI). (by use of
typeid
ordynamic_cast
.)Use C++-style casts like
static_cast<float>(double_value)
. Do not use cast formats like(int)x
unless the cast is tovoid
. You may use cast formats likeT(x)
only whenT
is a class type.Use the prefix form (
++i
) of the increment and decrement operators unless you need postfix semantics. (There can be a performance difference in certain cases.)We strongly recommend using
const
in APIs wherever it is meaningful and accurate.You should not use the unsigned integer types such as
uint32_t
, unless there is a valid reason such as representing a bit pattern rather than a number. When in doubt, use a larger type.Avoid defining macros, especially in headers; prefer inline functions, enums, and const variables.
Use
nullptr
for pointers, and'\0'
for chars (and not the0
literal).Prefer
sizeof(varname)
tosizeof(type)
.Use type deduction only if it makes the code clearer to readers who aren't familiar with the project.
Use lambda expressions where appropriate. Prefer explicit captures when the lambda will escape the current scope.
Naming
Use names that describe the purpose or intent of the object. Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader.
Filenames should be all lowercase and can include underscores (
_
) or dashes (-
).Type names start with a capital letter and have a capital letter for each new word, with no underscores:
MyExcitingClass
,MyExcitingEnum
.The names of variables (including function parameters) and data members are all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance:
a_local_variable
,a_struct_data_member
,a_class_data_member_
.Variables declared
constexpr
orconst
, and whose value is fixed for the duration of the program, are named with a leading "k" followed by mixed case. Underscores can be used as separators in the rare cases where capitalization cannot be used for separation.Regular functions have mixed case; accessors and mutators may be named like variables.
Namespace names are all lower-case, with words separated by underscores.
Enumerators (for both scoped and unscoped enums) should be named like constants, not like macros. That is, use
kEnumName
notENUM_NAME
.Exceptions: If you are naming something that is analogous to an existing C or C++ entity then you can follow the existing naming convention scheme.
Comments
Use either the
//
or/* */
syntax, as long as you are consistent.Start each file with license boilerplate.
Every non-obvious class or struct declaration should have an accompanying comment that describes what it is for and how it should be used.
Declaration comments describe use of the function (when it is non-obvious); comments at the definition of a function describe operation.
In general the actual name of the variable should be descriptive enough to give a good idea of what the variable is used for. In certain cases, more comments are required.
Note: I personally follows the Doxygen style for comments. Also,
@
is preferred over\
for Doxygen commands.
Formatting
Each line of text in your code should be at most 80 characters long. (controversial)
Non-ASCII characters should be rare, and must use UTF-8 formatting.
Use only spaces, and indent 2 spaces at a time.
See others by examples below.
Last updated