Cpp Coding Guide

User Manual:

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

DownloadCpp Coding Guide
Open PDF In BrowserView PDF
Member of the P ICANOL GROUP

µ C & DSP T EAM

C++ Coding Guide

Jens Jonckheere

July 7, 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

iii

1

Coding Style

1

2

Clean Code

2

2.1

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.1.1

Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.1.2

Horizontal Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.1.3

Vertical Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.1.4

Vertical Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.2.1

Small, Small, Small! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.3.1

Small, Small, Small! . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.3.2

Cohesion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2.2
2.3

3

4

Essential C ++ Knowledge

4

3.1

Value Initialization of Fundamental Types . . . . . . . . . . . . . . . . . . . . .

4

3.2

Object Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

3.2.1

Default Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

3.2.2

Direct Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

3.2.3

Copy Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

3.2.4

List Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

3.2.5

Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

3.2.6

Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

FreeRTOS

6

Bibliography

7
ii

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.

iii

Chapter 1

Coding Style

1

Chapter 2

Clean Code

Figure 2.1: The only valid measurement of code quality: WTFs / minute [1]

2

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

3

Chapter 3

Essential C ++ Knowledge
3.1

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.1: Value initialization syntax

3.2
3.2.1

Object Initialization
Default Initialization

This is the initialization performed when a variable is constructed with no initializer.
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.2: 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.
4

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.2.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.3: 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
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.2.3

Copy Initialization

3.2.4

List Initialization

3.2.4.1

Aggregate Initialization

3.2.5

Stack

3.2.6

Heap

5

Chapter 4

FreeRTOS
• Prefer inter-task queues over mutexes.

6

Bibliography
[1] Robert C. Martin. Clean Code. Prentice Hall, 2009.

7



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.5
Linearized                      : No
Page Count                      : 11
Page Mode                       : UseOutlines
Author                          : 
Title                           : 
Subject                         : 
Creator                         : LaTeX with hyperref package
Producer                        : pdfTeX-1.40.19
Create Date                     : 2018:07:07 21:09:31+02:00
Modify Date                     : 2018:07:07 21:09:31+02:00
Trapped                         : False
PTEX Fullbanner                 : This is MiKTeX-pdfTeX 2.9.6642 (1.40.19)
EXIF Metadata provided by EXIF.tools

Navigation menu