R3BROOT
R3B analysis software
|
To build a robust and readable software, programmers have to follow good coding standards and consistent conventions. The coding standards, either relating specific styles or following the C++ language rules, are created after decades of experience from various industries. The coding styles, probably different from companies to companies though, must be consistent throughout the whole project. The code related to NeuLAND follows strictly the C++ Core Guidelines and latest industrial standards. Reasons why a certain rule exists or a style is preferred are also given in the following sections.
Always prefer C++ ways over the C ways (There is no such thing as "C/C++"!). This includes:
static_cast
or dynamic_cast
instead of C cast.constexpr
instead of C macro.#pragma once
instead of include guard.There are only two ways (both with curly brackets) to declare and initialize a variable:
(preferred) Use auto
:
Type in the front:
Reason: Using curly brackets {}
makes sure the expression is an initialization instead of a function declaration. For example, int val();
is a declaration for a function called val
while int val {};
is a variable initialization.
Always use auto
for the variable initialization and function declaration:
Reason: Using auto (even for a simple integer) to initialize a variable guarantees the variable always get initialized. For example, int i;
fails to initialize the variable but still compiles while auto i = int;
never compiles.