mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 03:04:36 +03:00
Add a new section on maintainability
This commit is contained in:
22
03-Style.md
22
03-Style.md
@@ -271,31 +271,13 @@ Forgetting to initialize a member is a source of undefined behavior bugs which a
|
||||
|
||||
There is almost never a reason to declare an identifier in the global namespaces. Instead, functions and classes should exist in an appropriately named namespace or in a class inside of a namespace. Identifiers which are placed in the global namespace risk conflicting with identifiers from other libraries (mostly C, which doesn't have namespaces).
|
||||
|
||||
## 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 const double PI = 3.14159;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Use the Correct Integer Type For stdlib Features
|
||||
|
||||
The standard library generally returns `size_t` for anything related to size. What exactly `size_t` is is implementation defined.
|
||||
|
||||
In general, using `auto` will avoid most of these issues, but not all.
|
||||
|
||||
Make sure you stick with the correct integer types and remain consistent with the C++ stdlib. It might not warn on the platform you are currently using, but it probably will when you change platforms.
|
||||
|
||||
## Use .hpp and .cpp for Your File Extensions
|
||||
|
||||
Reference in New Issue
Block a user