C++/C Code Review Considerations (excerpt, non-original)

Source: Internet
Author: User
Tags case statement naming convention

C++/C Code Review

File structure

is the name of the header file and the definition file reasonable?
Is the directory structure of the header file and definition file reasonable?
is the copyright and version statement complete?

Important
Does the header file use the IFNDEF/DEFINE/ENDIF preprocessing block?
Does the header file contain only "declarations" without "definition"?

Layout of the program

is the blank line decent?
Is the space within the line of code appropriate?
is long line splitting appropriate?
Do "{" and "}" each take one row and align to the same column?

Important
Does a line of code only do one thing? If you define only one variable, write only one statement.
Important
The If, for, while, and do statements take on a single line, regardless of the number of execution statements, add "{}".
Important
When defining a variable (or parameter), does the modifier * and & Close the variable name?
Is the note clear and necessary?
Important
Does the comment have errors or may cause misunderstandings?

Important
The public of the class structure, protected, does the private order remain consistent in all programs?

Naming rules

is the naming convention consistent with the style of the operating system or development tool being used?
Is the identifier intuitive and can be spelt?
Should the length of identifiers conform to the "Min-length && max-information" principle?
Important
Are the same local variables and all variables present in the program?
are class names, function names, variables and arguments, and constants written in a format that follows certain rules?
are static variables, global variables, and class member variables prefixed?

Expressions and Basic statements
Important
If there are more operators in the line of code, is it clear that the order of operations of the expression is determined with parentheses?
Do you write complex expressions that are too complicated or multipurpose?
Important
Do you want to confuse a composite expression with a "true mathematical expression"?

Important
Do you want to write an if statement in an implicitly incorrect way? For example
(1) The Boolean variable is compared directly with true, false, or 1, 0.
(2) Use the floating-point variable with "= =" or "! = "Compared to any number.
(3) Use the pointer variable "= =" or "! = "Compared to NULL.

If there is a logical judgment in the loop and the number of loops is large, has the logical judgment been moved out of the loop body?
Important
Does the end of the case statement forget to add a break?
Important
Have you forgotten to write the default branch of switch?
Important
Do you leave a hidden danger when using goto statements? For example, skipping the construction of certain objects, initialization of variables, important calculations, and so on.

Constant


Do you use represented, which is intuitive, to represent numbers or strings that will appear more than once in a program?
In a C + + program, do you use const constants instead of macro constants?
Important
If a constant is closely related to other constants, is this relationship included in the definition?
Have you misunderstood the Const data members in the class? Because const data members are constants only for the lifetime of an object, they are mutable for the entire class.

function design


Is the writing of the parameter complete? Don't be greedy. Write only the type of the parameter and omit the parameter name.
is the parameter named and order reasonable?
is the number of arguments too many?
Do you use parameters of type and number uncertainties?
Is the type of the function return value omitted?
Does the function name conflict with the return value type semantically?

Important
Do you want to mix the normal and error flags back together? The normal value should be obtained with an output parameter, and the error flag is returned with a return statement.
Important
In the "entrance" of the function body, is the validity of the parameter checked with an assert?
Important
Use abuse of assert? For example, to confuse illegal situations with erroneous situations, the latter being inevitable and must be dealt with.
Important
Does the return statement return a pointer or reference to "stack memory"?
Do you use const to improve the robustness of a function? A const can enforce the protection of a function's arguments, return values, or even the definition of a function. "Use const whenever need"

Memory management

Important
Does the pointer value be checked for null immediately after requesting memory with malloc or new? (Prevent use of memory with pointer value NULL)
Important
Do you forget to assign an initial value to both arrays and dynamic memory? (Prevents uninitialized memory from being used as the right value)
Important
is the subscript of an array or pointer out of bounds?
Important
is the request for dynamic memory paired with the release? (Prevent memory leaks)
Important
Is the "memory exhaustion" problem handled effectively?
Important
Do you want to modify the contents of pointers to constants?
Important
Is there a wild pointer? For example
(1) The pointer variable is not initialized.
(2) After releasing memory with free or delete, forget to set the pointer to null.
Important
Do you confuse Malloc/free with New/delete?

Important
Is the malloc statement correct? For example, is the number of bytes correct? is the type conversion correct?
Important
Is the New/delete statement correct when creating and releasing a dynamic object array?

Advanced features of C + + functions

Does the overloaded function have two semantics?
Important
Do you confuse the overloading, overwriting, and hiding of member functions?
Do the overloads of the operators conform to the programming specifications that are developed?
is the inline function abused? For example, the code in the function body is longer and loops within the function body.
Important
Did you replace the macro code with an inline function?

constructors, destructors, and assignment functions for classes

Important
Whether the C + + compiler will automatically generate four default functions for the class if it violates the programming specification: (1) The default parameterless constructor, (2) The default copy constructor, (3) The default destructor, and (4) The default assignment function.
Important
Is there some initialization work missing from the constructor?
Important
Do you want to use the initialization table of the constructor correctly?
Important
Are some cleanup jobs missing from the destructor?

are copy constructors and assignment functions wrongly written or wrongly used?
Important
The assignment function is generally divided into four steps: (1) checking the self-assignment, (2) releasing the original Memory resource, (3) allocating the new memory resource and copying the content, (4) returning the *this. Have you missed important steps?
Important
Are constructors, destructors, assignment functions for derived classes correctly written? Precautions:
(1) A derived class cannot inherit a constructor, destructor, or assignment function of a base class.
(2) The constructor of the derived class should call the constructor of the base class in its initialization table.
(3) The destructor of the base class and the derived class should be virtual (that is, add the virtual keyword).
(4) When writing assignment functions for derived classes, be careful not to forget to re-assign values to the data members of the base class.

Advanced features of the class

Important
Do you violate the rules of inheritance and composition?
(1) If logically B is a "one" and all functions and attributes of a are meaningful to B, then B is allowed to inherit the functions and properties of a.
(2) If logically A is a "part" of B, then B is not allowed to derive from a, but to combine B with a and other things.

Other FAQs

Important
Data type issues:
(1) Is there an error in the data type of the variable?
(2) Are there assignments of different data types?
(3) Is there a comparison of different data types?

Important
Variable Value problem:
(1) Is there an error in the initialization or default value of the variable?
(2) Are there overflow or underflow of variables?
(3) is the accuracy of the variable sufficient?

Important
Logical judgment Question:
(1) is the comparison invalid due to the accuracy reason?
(2) Is there an error in the priority of an expression?
(3) is the logical judgment turned upside down?

Important
Cyclic problems:
(1) is the cyclic termination condition incorrect?
(2) Unable to terminate normally (dead loop)?
(3) Do you want to modify the loop variable incorrectly?
(4) Is there an accumulation of errors?

Important
Error handling issues:
(1) forgot to do error handling?
(2) Error handler block has never had a chance to be run?
(3) Is there a problem with the error handler block itself? If the reported error is inconsistent with the actual error, the processing method is incorrect, and so on.
(4) is the error handler block "Hindsight"? The software has been faulted before it was called.

Important
File I/O issues:
(1) Do you want to operate on a file that does not exist or is wrong?
(2) Does the file open in the correct way?
(3) is the file end judged incorrect?
(4) Did you close the file correctly?

C++/C Code Review Considerations (excerpt, non-original)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.