mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 19:24:36 +03:00
Prior to C++11, only static const data members of integral or enumeration type could have initializers in the class definition. C++11 extends that to floating point types (float, double) as well. However, in order for such members to be initialized in the class defintion, such members must either be constexpr or non-static.
37 lines
1.5 KiB
Markdown
37 lines
1.5 KiB
Markdown
# Considering Maintainability
|
|
|
|
|
|
## Avoid Compiler Macros
|
|
|
|
Compiler definitions and macros are replaced by the preprocessor before the compiler is ever run. This can make debugging very difficult because the debugger doesn't know where the source came from.
|
|
|
|
```cpp
|
|
// Bad Idea
|
|
#define PI 3.14159;
|
|
|
|
// Good Idea
|
|
namespace my_project {
|
|
class Constants {
|
|
public:
|
|
// if the above macro would be expanded, then the following line would be:
|
|
// static const double 3.14159 = 3.14159;
|
|
// which leads to a compile-time error. Sometimes such errors are hard to understand.
|
|
static constexpr double PI = 3.14159;
|
|
};
|
|
}
|
|
```
|
|
|
|
## Consider Avoiding Boolean Parameters
|
|
|
|
They do not provide any additional meaning while reading the code. You can either create a separate function that has a more meaningful name, or pass an enumeration that makes the meaning more clear.
|
|
|
|
See http://mortoray.com/2015/06/15/get-rid-of-those-boolean-function-parameters/ for more information.
|
|
|
|
## Avoid Raw Loops
|
|
|
|
Know and understand the existing C++ standard algorithms and put them to use. See [C++ Seasoning](https://www.youtube.com/watch?v=qH6sSOr-yk8) for more details.
|
|
|
|
## Properly Utilize 'override' and 'final'
|
|
|
|
These keywords make it clear to other developers how virtual functions are being utilized, can catch potential errors if the signature of a virtual function changes, and can possibly [hint to the compiler](http://stackoverflow.com/questions/7538820/how-does-the-compiler-benefit-from-cs-new-final-keyword) of optimizations that can be performed.
|