Cpp Coding Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 14

Member of the PICANOL GROUP
µC & DSP TEAM
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 FundamentalTypes............................. 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 VerticalAlignment............................. 4
2.1.4 VerticalWhitespace ............................ 4
2.2 Functions ...................................... 4
2.2.1 Small, Small, Small! ............................. 4
2.3 Classes ....................................... 4
2.3.1 Small, Small, Small! ............................. 4
2.3.2 Cohesion .................................. 4
3 Essential C ++ Knowledge 5
3.1 ListInitialization .................................. 5
3.2 Value Initialization of Fundamental Types . . . . . . . . . . . . . . . . . . . . . 5
3.3 ObjectInitialization................................. 5
3.3.1 Default Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.2 DirectInitialization............................. 6
3.3.3 CopyInitialization ............................. 7
3.3.4 ListInitialization.............................. 7
ii
3.3.5 Stack .................................... 7
3.3.6 Heap .................................... 7
3.3.7 test ..................................... 7
4 FreeRTOS 8
Bibliography 9
iii
Foreword
The different coding styles of programmers can lead to confusion and time-loss when other team-
members 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 Initialization
1.1.1 Fundamental Types
An object of fundamental type can be initialized in many different ways.
1uint8 t uninitialized var ; / / d e f a u l t i n t i a l i z a t i o n
2u i n t 8 t z e r o e d v a r 1 ( 0 ) ; // direct initialization
3u i n t 8 t z e r o e d v a r 2 = 0 ; // copy initialization =>preferred
4u i n t 8 t z e r o e d v a r 3 {0};/ / d i r e c t l i s t initialization
5u i n t 8 t z e r o e d v a r 4 = {0};/ / copyl i s t initialization
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.
1u i n t 8 t z e r o e d v a r 5 ( ) ;
2u i n t 8 t z e r o e d v a r 6 { } ;
3u i n t 8 t z e r o e d v a r 7 = { } ;
Listing 1.2: Value initialization of fundamental type objects
1.1.2 Arrays
Arrays can be initialized in different ways.
1u i n t 8 t u n i n i t i a l i z e d a r r a y [ 3 ] ; // default initialization
2u i n t 8 t a r r a y 1 [ 3 ] {0 , 1 , 2 };/ / d i r e c t listinitialization
3u i n t 8 t a r r a y 2 [ 3 ] = {0 , 1 , 2 };/ / copylistinitialization =>preferred
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.
1u i n t 8 t z e r o e d a r r a y 1 [ 3 ] { } ;
2u i n t 8 t z e r o e d a r r a y 2 [ 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.
1u i n t 8 t z e r o e d a r r a y 3 [ 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 Functions
2.2.1 Small, Small, Small!
2.3 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.
1int number ; // uninitialized
2int number { } ;/ / same a s i n t number = 0
3int a r r a y [ 1 0 0 ] ; // uninitialized
4int a r r a y [ 1 0 0 ] { } ;/ / 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.
1int number ; // uninitialized
2int number { } ;/ / same a s i n t number = 0
3int a r r a y [ 1 0 0 ] ; // uninitialized
4int a r r a y [ 1 0 0 ] { } ;/ / same a s i n t a r r a y [ 1 0 0 ] = {0}
Listing 3.2: Value initialization syntax
3.3 Object Initialization
3.3.1 Default Initialization
This is the initialization performed when a variable is constructed with no initializer.
5
1T object ; / / c o n s t r u c t e d on s t a c k
2To b j e c t p t r = new T ; / / 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.
1T o b j e c t ( ar g 1 , arg2 , . . . ) ; / / c o n s t r u c t e d on s t a c k
2To b j e c t p t r = new T ( a r g 1 , arg2 , . . . ) ; / / c o n s t r u c t e d on h e a p
3T ( 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
4T ( arg1 , arg 2 , . . . ) / / p r v a l u e t e m p o r a r y w i t h 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
5C l a s s : : C l a s s ( ) :
6member ( a r g s , . . . ) / / b a s e o r a nons 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
7{...
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 consid-
ers non-explicit constructors and non-explicit user-defined conversion functions, while direct-
initialization 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

Navigation menu