C ++ code review level standard

Source: Internet
Author: User
C ++ Code Review Criteria 0 Preface

The record problems are caused by common or basic defects in the C ++ coding process.

It is just a standard compiled by an individual. It is used for "mine clearance" in normal code writing and is not authoritative. It is now applied to my development team.

The subsequent Supplementary improvement is still a long process.

ProgramPersonnel must be good at recording and summarizing so that they can grow and improve.

1 Introduction

Code review Definition: Check the quality assurance process for code defects by reading the code.

Code review content: Programming specifications, reconstruction methods, architecture design, performance security, logs, readability, and scalability...

Benefits of code review: Identify coding defects early, analyze coding defects, improve code quality, improve coding level, and promote team cooperation...

Code Review Form: Code reading, code explanation, Pair programming, cross-review, expert review, and review meeting (only problems are found and not discussed or resolved at the meeting )...

2 Level standard

Generally, there are three levels:

Severe: System crashes, program crashes, and functions become unavailable, causing serious consequences.

Average: May cause some function unavailability, design defects, and operational efficiency problems.

Prompt: This may cause poor code readability and violation of programming specifications.

3 Severity Level and problem examples
 
1) Problem type: NULL pointer operation example: ctestclass* P =NULL; P-> Getname ();//Operation NULL pointer
2) Problem type: Invalid Pointer operation example:Char* P =New Char[100]; Delete P; memcpy (p,"Hi!");//Invalid Pointer operation
 
3) Problem type: the pointer is not null. Example:If(Null = P );//The pointer must be null.
 
4) Problem type: Wild pointer example: malloc and free should appear in pairs in new and delete.
5) Problem type: endless loop example:While(...) {I++;//I ++ ensures no endless Loops};
 
6) Problem type: logical error example: the code logic is chaotic or related.
 
7) Problem type: resource leakage example: cdialog: Create requires cwnd: destroywindow to release createicon, getIcon, and destroyicon to release cbitmap LoadImage. You need deleteobject to release it.
8) Problem type: array out-of-bounds problem example:CharSzarray [Max]; printf ("% C", Szarray [Max]);//Array out-of-bounds
 
9) Problem type: loop body rewriting loop variable example:For(IntI =0; I <Max; I ++) {I=100;//Loop body rewrite loop variable}
10) Problem type: Io operation, database operation resource not released example: open operation must be closed, open () and close () operations should appear in pairs.
 
11) Problem type: determines whether the number of unsigned characters is smaller than 0. Example: uint ilen=Max;If(Ilen <0)//Judge whether the number of unsigned characters is less than 0{...}
  12  ) problem type: ignore the ending mark of memory copy   '  \ 0   '  example:   char  * s = "   AAAA  "  ;  char  A [ 4  ]; memcpy (A, S,   4 ); ///   memory copy ignores the ending sign '\ 0'  
13) Problem Type: an example of modifying the internal parameters of a function: String getname (IntIID) {IID=100;//Modify the parameter in the function....}
 
14) Problem type: New array, delete without [] problem example:Char* A =New Char[100];... Delete;//New array, delete none []
15) Problem type: duplicate names of local variables and local variables, function parameters, and global variables16) Problem type: the object cannot be initialized using memset.17) Problem type: multi-threaded resources are not mutually exclusive.18) Problem type: the complexity of the Code circle cannot exceed 1419) Problem type: the number of nested levels of Function Code cannot exceed 6.20) Problem type: the debugging or log information cannot contain sensitive information.
4 General level and problem examples
1) Problem type: Examples of uninitialized variables:Char*;//Variable not initializedCharB [100]; Strcpy (B, );
 
2) Problem type: the function input parameter is not verified. Example:VoidFuntest (ctestclass *P) {P-> Get ();//Function input parameter not verified}
  3  ) problem type: example of comparing boolean values with non-boolean values:   int  I =  0  ;  bool  flag =  true  ;   If  (I = Flag) ///   comparison between Boolean and non-boolean values   {...}  
4) Problem type: Example of a problem between a floating point number and a value of 0:DoubleDwlen =0.1;If(0= Dwlen)//Comparison between floating point and 0{...}
 
5) Problem type: Forced comparison or assignment of different data types example:LongLmax =32768;ShortIlen =0; Ilen= Lmax;//Mandatory value assignment for different data types
6) Problem type: complex expressions without parentheses indicate priority. Example:If(I = J & I = k | I = L | j = K)//Complex expressions without parentheses indicate priority{...}
 
7) Problem type: the function does not return a value or the return value is incorrect. Example:BoolFun (){IntI =0;...ReturnI;//The function return value is incorrect.}
8) Problem type: Incorrect pointer judgment example:BoolFun (Char*P ){If(P)//Incorrect pointer judgment}
 
9) Problem type: variables should be compared=Left-side question example:If(P = NULL)//Variable comparison should be placed on the left
 
10) Problem type: repeated code example: repeated code should be extracted or encapsulated, and repeated ones should be added with comments on their repeated rationality.
5 Level of prompt and example of Problem
1) Problem type: Devil's number example:For(IntI =0; I <1000; I ++)//Devil's number{...}
 
2) Problem type: Incorrect annotation example: Incorrect annotation or semantic expression
 
3) Problem type: Invalid Comment example: the comment must be reasonable, not redundant, and not nonsense.
 
4) Problem type: no comments or too low comments examples: normal comments should be at 30% Or above
 
5) Problem type: typographical or indentation disorder example: Empty rows and indentation must be normalized (4 spaces instead of tab)
6) Problem type: redundant code example: useless comments or useless code
 
7) Problem type: maintenance code without explanation example: maintenance code must be annotated
  8  ) problem type:   If ,  for ,  while  , switch and other statements should add {} problem example:   switch   (I)   case   1   :...   //   Add {}  Break  ;   case   2   :...   Break ; 
  9  ) problem type: the total number of file lines cannot exceed 2000   10  ) problem type: the total number of function lines cannot exceed 200   11  ) problem type: function parameters cannot exceed 5   12  ) problem type: the name does not comply with programming specifications  
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.