Introduction to C++ Coding Style
How to Program Beautifully?
Zhang Fangkui
ACM Honoured Class, SJTU
Sept. 29, 2013
Zhang Fangkui Google C++ Style Guide
Contents I
Header Files
Inline Functions
Function Parameter Ordering
Names and Order of Includes
Scoping
Local Variables
Static and Global Variables
Zhang Fangkui Google C++ Style Guide
Contents II
Classes
Doing Work in Constructors
Default Constructors
Access Control
Declaration Order
Write Short Functions
Formatting
Line Length
Horizontal Whitespace
Vertical Whitespace
Indentation
Zhang Fangkui Google C++ Style Guide
Inline Functions
Tip:Define functions inline only when they are small, say, 10
lines or less.
Definition:You can declare functions in a way that allows the
compiler to expand them inline rather than calling them
through the usual function call mechanism.
You will gain a deeper understanding working on the C
Compiler Project.
Zhang Fangkui Google C++ Style Guide
Function Parameter Ordering
Tip:When defining a function, parameter order is: inputs,
then outputs.
Zhang Fangkui Google C++ Style Guide
Function Parameter Ordering
Tip:When defining a function, parameter order is: inputs,
then outputs.
This is not a hard-and-fast rule. Parameters that are both
input and output (often classes/structs) muddy the waters,
and, as always, consistency with related functions may require
you to bend the rule.
Zhang Fangkui Google C++ Style Guide
Names and Order of Includes
Use standard order for readability and to avoid hidden
dependencies: C library, C++ library, other libraries’ .h, your
project’s .h.
Within each section it is nice to order the includes
alphabetically.
Zhang Fangkui Google C++ Style Guide
Names and Order Includes
#i n c l u d e ” f o o / p u b l i c / f o o s e r v e r . h”
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <u n i s t d . h>
#i n c l u d e <hash map>
#i n c l u d e <v e c t o r >
#i n c l u d e ” b a s e / b a s i c t y p e s . h”
#i n c l u d e ” b a s e / c o m m a n d l i n e f l a g s . h”
#i n c l u d e ” f o o / p u b l i c / b a r . h”
For example, the includes in
google-awesome-project/src/foo/internal/fooserver.cc
might look like above.
Zhang Fangkui Google C++ Style Guide
Local Variables
Tip: Place a function.s variables in the narrowest scope
possible, and initialize variables in the declaration.
C++ allows you to declare variables anywhere in a function.
We encourage you to declare them in as local a scope as
possible.
int i ;
i = f (); // Bad . . .
int j = g (); // Good !
Zhang Fangkui Google C++ Style Guide
Local Variables
Warning:
if the variable is an object, its constructor is invoked every
time it enters scope and is created, and its destructor is
invoked every time it goes out of scope.
f o r ( i n t i = 0 ; i < 1 0 0 0 0 0 0 ; ++i ) {
Foo f ;
f . DoSomething ( i ) ;
}
Foo f ;
f o r ( i n t i = 0 ; i < 1 0 0 0 0 0 0 ; ++i ) {
f . DoSomething ( i ) ;
}
Zhang Fangkui Google C++ Style Guide
Static and Global Variables
Tip: Static or global variables of class type are forbidden:
they cause hard-to-find bugs due to indeterminate order of
construction and destruction.
However, such variables are allowed if they are constexpr: they
have no dynamic initialization or destruction.
Zhang Fangkui Google C++ Style Guide
Doing Work in Constructors
Tip: Do only trivial initialization in a constructor. If at all
possible, use an Init() method for non-trivial initialization.
Zhang Fangkui Google C++ Style Guide
Default Constructors
Tip: You must define a default constructor if your class
defines member variables and has no other constructors.
Otherwise the compiler will do it for you, badly.
If you have no other constructors and do not define a default
constructor, the compiler will generate one for you. This
compiler generated constructor may not initialize your object
sensibly.
If your class inherits from an existing class but you add no
new member variables, you are not required to have a default
constructor.
Zhang Fangkui Google C++ Style Guide
Access Control
Tip: Make all data members private, and provide access to
them through accessor functions as needed. Typically a
variable would be called foo and the accessor function foo().
You may also want a mutator function set foo().
The definitions of accessors are usually inlined in the header
file.
Zhang Fangkui Google C++ Style Guide
Declaration Order
Tip: Use the specified order of declarations within a class:
public: before private:, methods before data members
(variables), etc.
public: section, then protected: section, then private: section.
If any of these sections are empty, omit them.
Within each section, the order should be
1 Typedefs and Enums
2 Constants
3 Constructors
4 Destructor
5 Methods, including static methods
6 Data Members, including static data members
Zhang Fangkui Google C++ Style Guide
Write Short Functions
Tip: Prefer small and focused functions.
If a function exceeds about 40 lines, think about whether it
can be broken up without harming the structure of the
program.
Long function results in bugs that are hard to find.
Zhang Fangkui Google C++ Style Guide
Line Length
Tip: Each line of text in your code should be at most 80
characters long.
Exception: if a comment line contains an example command
or a literal URL longer than 80 characters, that line may be
longer than 80 characters for ease of cut and past.
Exception: an #include statement with a long path may
exceed 80 columns. Try to avoid situations where this
becomes necessary.
Exception: you needn’t be concerned about header guards
that exceed the maximum length.
Zhang Fangkui Google C++ Style Guide
Horizontal and Vertical Whitespace
Tip: Use of horizontal whitespace depends on location. Never
put trailing whitespace at the end of a line.
Tip: Minimize use of vertical whitespace.
See some examples.
Zhang Fangkui Google C++ Style Guide
Indentation
See some examples.
Zhang Fangkui Google C++ Style Guide
Reference and Further Reading
Google Style Guide
http://google-styleguide.googlecode.com
A tiny Software(Actually a python script file)
http://google-styleguide.googlecode.com/svn/trunk/cpplint
Zhang Fangkui Google C++ Style Guide