@@ -272,7 +272,7 @@ member variables use the lowerCamelCase naming style.
const unsigned int DEFAULT_FILE_SIZE_KB = 200; // Global variable
}
Formatting
Formatting
**********
Line Length
...
...
@@ -942,7 +942,7 @@ Classes
=======
Rule 3.15.1 Class access specifier declarations are in the sequence: public, protected, private. Indent these specifiers to the same level as the class keyword.
@@ -997,4 +997,462 @@ Rule 3.15.2 The constructor initialization list must be on the same line or wrap
someOtherVar_(var + 1)
{
DoSomething();
}
\ No newline at end of file
}
Comments
********
Generally, clear architecture and good naming are recommended to improve code readability, and comments are provided only when necessary.
Comments are used to help readers quickly understand code. Therefore, **comments should be provided for the sake of readers**.
Comments must be concise, clear, and unambiguous, ensuring that information is complete and not redundant.
**Comments are as important as code**.
When writing a comment, you need to step into the reader’s shoes and use comments to express what the reader really needs. Comments are used to express the function and intention of code, rather than
repeating code.
When modifying the code, ensure that the comments are consistent with the modified code. It is not polite to modify only code and keep the old comments, which will undermine the consistency between code and
comments, and may confuse or even mislead readers.
Comments should be made in English.
Comment Style
=============
In C++ code, both ``/* */`` and ``//`` can be used for commenting.
Comments can be classified into different types, such as file header comments, function header comments, and code comments. This is based on their purposes and positions.
Comments of the same type must keep a consistent style.
Note: Example code in this document uses comments in the ``//`` format
only to better describe the rules and recommendations. This does not
mean this comment format is better.
File Header Comments
====================
Rule 4.2.1 File header comments must contain the copyright notice.
Rule 4.4.2 There must be a space between the comment character and the comment content. At least one space is required between the comment and code if the comment is placed to the right of code.
TODO/TBD comments are used to describe required improvements and
supplements. FIXME comments are used to describe defects that need
fixing. They should have a standardized style, which facilitates text
search. For example:
.. code:: cpp
// TODO(<author-name>): XX
// FIXME: XX
Header Files
************
Header File Responsibility
==========================
A header file is an external interface of a module or file. The design
of a header file shows most of the system design. The interface
declaration for most functions is more suitable placed in the header
file, but implementation (except inline functions) cannot be placed in
the header file. Functions, macros, enumerations, and structure
definitions that need to be used in .cpp files cannot be placed in the
header file. The header responsibility should be simple. An overly
complex header file will make dependencies complex and cause long
compilation times.
Recommendation 5.1.1 Each .cpp file should have a .h file with the same name. It should be used to declare the classes and interfaces that need to be exposed externally.
Generally, each .cpp file has a corresponding .h file. This .cpp file is
used to store the function declarations, macro definitions, and class
definitions that are to be exposed. If a .cpp file does not need to open
any interface externally, it should not exist. Exception: **An entry
point (for example, the file where the main function is located), unit
tests, and dynamic libraries**
For example:
.. code:: cpp
// Foo.h
#ifndef FOO_H
#define FOO_H
class Foo {
public:
Foo();
void Fun();
private:
int value_;
};
#endif
.. code:: cpp
// Foo.cpp
#include "Foo.h"
namespace { // Good: The declaration of the internal function is placed in the header of the .cpp file, and has been limited to the unnamed namespace or static scope.
void Bar()
{
}
}
...
void Foo::Fun()
{
Bar();
}
Header File Dependency
======================
Rule 5.2.1 Header file cyclic dependency is forbidden.
An example of cyclic dependency (also known as circular dependency) is: a.h contains b.h, b.h contains c.h, and c.h contains a.h. If any of these header files is modified, all code containing a.h, b.h, and
c.h needs to be recompiled.
For a unidirectional dependency, for example if: a.h contains b.h, b.h contains c.h, and c.h does not contain any header file, modifying a.h does not mean that we need to recompile the source code for b.h or
c.h.
The cyclic dependency of header files reflects an obviously unreasonable architecture design, which can be avoided through optimization.
Rule 5.2.2 Header files should have #define guards to prevent multiple inclusion.
To prevent header files from being included multiple times, all header files should be protected by #define. Do not use #pragma once.
When defining a protection character, comply with the following rules:
The protection character uses a unique name. 2) Do not place code or comments (except for file header comments) before or after the protected
part.
Example: Assume that the timer.h file of the timer module is in the timer/include/timer.h directory. Perform the following operations to safeguard the timer.h file:
.. code:: cpp
#ifndef TIMER_INCLUDE_TIMER_H
#define TIMER_INCLUDE_TIMER_H
...
#endif
Rule 5.2.3 It is prohibited to reference external function interfaces and variables in extern declaration mode.