01
.1 Inline Functions
Inline functions (inline function) should be less than 10 lines
Description: Inline functions have the characteristics of regular functions, but differ in how function calls are handled. When a regular function is called, control is transferred to the called function and then returns to the calling function; whereas, in an inline function, the calling expression is replaced with the inline function body. Inline functions are suitable only for small functions with 1 to 10 lines. For larger functions with many statements, the overhead of calling and returning from a function is relatively insignificant, and there is no need to implement inline functions. Most compilers will abandon the inline approach and use the normal method to call the function.
If an inline function contains complex control structures, such as loops, branches (switch), try-catch, etc., most compilers will treat that function as a regular function.Virtual functions and recursive functions cannot be used as inline functions.
02 Use Inline Functions Instead of Function MacrosDescription: C++ also supports macro functionality, but macros have inherent flaws (e.g., they cannot perform type checking on parameters). Therefore, wherever inline functions can be used, macros should not be used.Example:
// Good example: template <class TYPE_T> inline TYPE_T& max(TYPE_T& x, TYPE_T& y) { return (x>y)?x:y;} // Bad example: #define MAX(x,y)((x)>(y)?(x):(y))
Exception: Some general and mature applications, such as encapsulation of new and delete, can retain the use of macros.03
Implement Inline Functions in Separate Files
Description: Ensure clear interfaces. If users and maintainers see declarations containing a large number of inline implementations, it will interfere with their thinking and reduce the readability and maintainability of the declarations. Therefore, except for the simplest member access functions, the implementations of other more complex inline functions should be placed in separate header files. The definitions of complex inline functions should be placed in header files with the suffix -inl.h, included at the end of the declaration header file.
Example:
#ifndef CPP_RULE_H #define CPP_RULE_H class CppRule { public: inline inlineFunction(); }; #include "cpp_rule-inl.h" #endif //CPP_RULE_H
//cpp_rule-inl.h#ifndef CPP_RULE_INL_H#define CPP_RULE_INL_Hinline CppRule::inlineFunction() { // Inline function implementation} #endif //CPP_RULE_INL_H
04
2 Function Parameters
Prefer using const references instead of pointers for input parameters
Description: References are safer than pointers because they are guaranteed to be non-null and will not point to other targets; references do not require checking for illegal NULL pointers. If the product is developed on an older platform, it is preferable to follow the existing platform’s handling methods. Choosing const avoids modification of parameters, making it clear to the code reader that the parameter will not be modified, greatly enhancing code readability.05 Recommendation to Eliminate Unused Function ParametersDescription: Check for unused function parameters and confirm whether they are needed. If not, directly remove the parameter name. It is quite common for some parameters to be unreferenced when implementing interfaces. The compiler will detect unused parameters and generate a warning; some components may even consider this an error. To avoid this situation, comment out unused parameters using /* parameter name */ syntax.Example:
// Good example: Remove the localityHint parameter name, keeping it in the comment for reference and understanding of the parameter's meaningpointer allocate(size_type numObjects, const void * /* localityHint */ = 0) { return static_cast<pointer>(mallocShared(numObjects * sizeof(T))); }
06
Minimize the Use of Default Parameters
Description: Using default values for parameters only facilitates function usage without providing new functionality, but it reduces the understandability of the function. Default parameters make it difficult to present all parameters when copying and pasting previous function call code, and when default parameters are not applicable to new code, it may lead to significant issues.07
3 Function Pointers
Minimize the Use of Function PointersDescription: Many codes still use function pointers to implement some functional extensions (such as encapsulation), but function pointers are difficult to understand and maintain. It is recommended to use the basic usage of inheritance and derivation in C++ and to use function pointers sparingly.
Follow Me
Click the card below to follow me
↓↓↓

If you like it, please click
Collect
Like
View
+1
❤❤❤