Cpp Coding Guide
User Manual:
Open the PDF directly: View PDF
.
Page Count: 14
| Download | |
| Open PDF In Browser | View PDF |
Member of the P ICANOL GROUP
µ C & DSP T EAM
C++ Coding Guide
Jens Jonckheere
July 12, 2018
We are authors. And one thing about authors is that they have readers. Indeed, authors are
responsible for communicating well with their readers. The next time you write a line of code,
remember you are an author, writing for readers who will judge your effort.
– Robert C. Martin
Contents
Foreword
iv
1
Coding Style
1
1.1
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.1.1
Fundamental Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.1.2
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.1.3
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2
Clean Code
3
2.1
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.1.1
Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.1.2
Horizontal Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.1.3
Vertical Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.1.4
Vertical Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.2.1
Small, Small, Small! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.3.1
Small, Small, Small! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.3.2
Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.2
2.3
3
Essential C ++ Knowledge
5
3.1
List Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3.2
Value Initialization of Fundamental Types . . . . . . . . . . . . . . . . . . . . .
5
3.3
Object Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3.3.1
Default Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3.3.2
Direct Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.3.3
Copy Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3.4
List Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
ii
4
3.3.5
Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3.6
Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3.7
test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
FreeRTOS
8
Bibliography
9
iii
Foreword
The different coding styles of programmers can lead to confusion and time-loss when other teammembers need to understand the code, for example to add features or to debug. This document
presents a common coding style for our team, so that code written by colleagues also looks familiar
to us. This document also gives some tips on writing clean code and gives an overview of some
essential C++ knowledge.
iv
Chapter 1
Coding Style
1.1
1.1.1
Initialization
Fundamental Types
An object of fundamental type can be initialized in many different ways.
1
2
3
4
5
uint8
uint8
uint8
uint8
uint8
t
t
t
t
t
uninitialized var ;
zeroed var1 (0) ;
zeroed var2 = 0;
zeroed var3 {0};
zeroed var4 = {0};
//
//
//
//
//
default intialization
direct initialization
copy i n i t i a l i z a t i o n => p r e f e r r e d
d i r e c t −l i s t −i n i t i a l i z a t i o n
copy− l i s t − i n i t i a l i z a t i o n
Listing 1.1: Initialization of fundamental type objects
This style guide prefers copy initialization for fundamental type objects because it also works in
C, it’s what we are accustomed to.
Note that for fundamental types default initialization actually means uninitialized.
By leaving the parentheses empty, objects are value initialized. For fundamental types this means
that all bits are made zero.
1
2
3
uint8 t zeroed var5 () ;
u i n t 8 t zeroed var6 {};
u i n t 8 t zeroed var7 = {};
Listing 1.2: Value initialization of fundamental type objects
1.1.2
Arrays
Arrays can be initialized in different ways.
1
2
3
uint8 t uninitialized array [3];
u i n t 8 t array1 [3]{0 , 1 , 2};
u i n t 8 t a r r a y 2 [ 3 ] = {0 , 1 , 2};
// default initialization
/ / d i r e c t −l i s t −i n i t i a l i z a t i o n
/ / copy− l i s t − i n i t i a l i z a t i o n => p r e f e r r e d
Listing 1.3: Initialization of arrays
1
Default initialization of an array results in default initialization of every element.
This style guide prefers copy-list-initialization for arrays over direct-list-initialization because it
also works in C.
Note that the individual array elements are initialized by copy-initialization from the initializers
specified in the braced-init-list.
If the number of initializer clauses is less than the number of elements, or the initializer list is
completely empty, the remaining elements are value-initialized.
1
2
uint8 t zeroed array1 [3]{};
u i n t 8 t zeroed array2 [3] = {};
Listing 1.4: Value initialization of arrays
The preferred way of zeroing an array is shown in listing 1.5 because this also works in C.
1
u i n t 8 t zeroed array3 [3] = {0};
Listing 1.5: Zeroing arrays
1.1.3
Classes
2
Chapter 2
Clean Code
Figure 2.1: The only valid measurement of code quality: WTFs / minute [2]
3
2.1
Variables
2.1.1
Names
2.1.2
Horizontal Alignment
2.1.3
Vertical Alignment
2.1.4
Vertical Whitespace
2.2
2.2.1
2.3
Functions
Small, Small, Small!
Classes
2.3.1
Small, Small, Small!
2.3.2
Cohesion
4
Chapter 3
Essential C ++ Knowledge
3.1
List Initialization
Prefer list initialization over direct initialization for class types.
List initialization has some advantages:
• it can be used for class types and also for fundamental types and arrays, so it can be seen as a
uniform initializer;
• it avoids the most vexing parse.
1
2
3
4
int
int
int
int
number ;
number { } ;
array [100];
array [100]{};
// uninitialized
/ / same a s ” i n t number = 0”
// uninitialized
/ / same a s ” i n t a r r a y [ 1 0 0 ] = {0}”
Listing 3.1: Value initialization syntax
3.2
Value Initialization of Fundamental Types
In C++, local variables and class members of fundamental types (int, float, int*, ...) do not get
initialized by default. Value initialization can be used to zero-initialize fundamental types.
1
2
3
4
int
int
int
int
number ;
number { } ;
array [100];
array [100]{};
// uninitialized
/ / same a s ” i n t number = 0”
// uninitialized
/ / same a s ” i n t a r r a y [ 1 0 0 ] = {0}”
Listing 3.2: Value initialization syntax
3.3
3.3.1
Object Initialization
Default Initialization
This is the initialization performed when a variable is constructed with no initializer.
5
1
2
T object ;
T∗ o b j e c t p t r = new T ;
/ / c o n s t r u c t e d on s t a c k
/ / c o n s t r u c t e d on h e a p
Listing 3.3: Default initialization syntax
The effects of default initialization are:
• if T is a [non-POD (until C++11)] class type, the constructors are considered and subjected to
overload resolution against the empty argument list. The constructor selected (which is one
of the default constructors) is called to provide the initial value for the new object;
• if T is an array type, every element of the array is default-initialized;
• otherwise, nothing is done: the objects with automatic storage duration (and their subobjects)
are initialized to indeterminate values.
Notes
If no user-defined constructors of any kind are provided for a class type (struct, class, or union),
the compiler will always define an empty default constructor as an inline public member of its
class.
Default initialization of non-class variables with automatic and dynamic storage duration produces
objects with indeterminate values (static and thread-local objects get zero initialized).
If T is a const-qualified type, it must be a class type with a user-provided default constructor.
References cannot be default-initialized.
3.3.2
Direct Initialization
Initializes an object from an explicit set of constructor arguments.
1
2
3
4
5
6
7
T o b j e c t ( a r g 1 , a r g 2 , . . . ) ; / / c o n s t r u c t e d on s t a c k
T∗ o b j e c t p t r = new T ( a r g 1 , a r g 2 , . . . ) ; / / c o n s t r u c t e d on h e a p
T ( o t h e r ) / / p r v a l u e t e m p o r a r y by f u n c t i o n a l c a s t
T( arg1 , arg2 , . . . ) / / p r v a l u e temporary with a p a r e n t h e s i z e d e x p r e s s i o n l i s t
Class : : Class () :
member ( a r g s , . . . ) / / b a s e o r a non− s t a t i c member i n i t i n i n i t i a l i z e r l i s t
{...
Listing 3.4: Direct initialization syntax
The effects of direct initialization are:
• if T is a class type, the constructors of T are examined and the best match is selected by
overload resolution. The constructor is then called to initialize the object;
• otherwise, if T is a non-class type but the source type is a class type, the conversion functions
of the source type and its base classes, if any, are examined and the best match is selected by
overload resolution. The selected user-defined conversion is then used to convert the initializer
expression into the object being initialized;
• otherwise, standard conversions are used, if necessary, to convert the value of other to the
cv-unqualified version of T, and the initial value of the object being initialized is the (possibly
converted) value.
Notes
6
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and non-explicit user-defined conversion functions, while directinitialization considers all constructors and all user-defined conversion functions.
In case of ambiguity between a variable declaration using the direct-initialization syntax (with
round parentheses) and a function declaration, the compiler always chooses function declaration.
This disambiguation rule is sometimes counter-intuitive and has been called the most vexing parse.
3.3.3
Copy Initialization
3.3.4
List Initialization
3.3.4.1
Aggregate Initialization
3.3.5
Stack
3.3.6
Heap
3.3.7
test
7
Chapter 4
FreeRTOS
• Prefer inter-task queues over mutexes.
8
Bibliography
[1] cppreference.com. https://en.cppreference.com/w/, 2018.
[2] Robert C. Martin. Clean Code. Prentice Hall, 2009.
9
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 14 Page Mode : UseOutlines Author : Title : Subject : Creator : LaTeX with hyperref package Producer : pdfTeX-1.40.19 Create Date : 2018:07:12 21:18:52+02:00 Modify Date : 2018:07:12 21:18:52+02:00 Trapped : False PTEX Fullbanner : This is MiKTeX-pdfTeX 2.9.6642 (1.40.19)EXIF Metadata provided by EXIF.tools