statements

type

  • Local types that are specific to one file are generally declared inside the corresponding file.
  • Classes must be divided into public, protected, and private sections. Each section must be explicitly identified. Unused parts should be reserved.
  • Type conversions must be explicit. Do not rely on implicit type conversions.

variable

  • Variables should be initialized where they are declared.
  • Variables should not have double meanings.
  • Use of global variables should be minimized.
  • Class variables should not be declared public.
  • Related variables of the same type are usually declared in a single statement.
  • C++ pointer and reference symbols should be close to variable names, not type names.
  • The const keyword should precede the type name.
  • Except for Boolean variables and Pointers, you should not test for an implicit 0 value.
  • Variables should be declared in the smallest possible scope.

cycle

  • Only statements for the control loop must be contained in the for() construct.
  • Loop variables should be initialized in front of the loop body.
  • Do -while loops are generally avoided.
  • Break and continue should be avoided within loops.
  • You should use while(true) for an infinite loop

conditions

  • Very complex conditional expressions must be avoided. By introducing temporary Boolean variables instead.
  • For an if statement, the normal branch should be placed in the if section and the exception section in the else section.
  • Conditions should be placed on a separate line.
  • You must avoid including execution statements in the condition section.

miscellaneous

  • Mysterious numbers should be avoided in your code. Any number other than 0 and 1 is a mystery number and should be replaced by a named constant.
  • Methods are recommended to access constants.
  • The function must always explicitly list the return value.
  • Check the validity of all parameter inputs to the function; And the error return code of the called function should be carefully and comprehensively handled.
  • Goto should not be used.
  • Try to limit the size of functions to 200 lines without comments or blank lines.
  • Reduce recursive calls to functions themselves or between functions;
  • “0” should be used instead of “NULL”.