Most bugs in software are related to various low-level errors, while most low-level errors cannot be detected through tests. More code review is required to detect problems.
To cultivate good habits without any hypothetical programming, you can start by writing every function first. I want to use the annotation driver to remind myself to review every function frequently.
Below is my proposed code review comments:
/*### Code review * ### reviewer: Wawa * ### Last Review Date: 2013-01-29 **-Todo: input review *-Todo: output review *-Todo: exception review *-Todo: NULL reference review *-Todo: Out of bounds review ***/
Each function adds such a comment. After a review is passed, todo is changed to done. IfCodeAfter modification, review again and modify the Last review date.
It will certainly be difficult to do so, but my idea is that the slower the code is written, the better. You can use the check list to forcibly think about the code, so that you can gradually get used to it.
For example, the following user login code.
FunctionLogin (username, password ){VaRAccount =Accountdao. getaccount ();If(Account ['Password'] =Password ){Return{Code: 200, message: 'login success .'}}Else{Return{Code: 403, message: 'login faild .'}}}
Obviously there are a lot of problems, and after review, modify as follows:
/* ### Code review * ### reviewer: Wawa * ### Last Review Date: 2013-01-29 **-done: input review *-done: output review *-done: exception review *-done: NULL reference review *-done: Out of bounds review ** */ Function Login (username, password ){ If (Username = Null | Password = Null | Username = ''| Password ='' ){ Return {Code: 400, message: 'input invalid' };} Try { VaR Account = Accountdao. getaccount (); If (Account = Null ){ Return {Code: 404, message: 'account not found' };}} Catch (ERR ){ Return {Code: 500, message: 'server error' };} If (! ('Password' In Account )){ Return {Code: 500, message: 'server error' };} If (Account ['Password'] = Password ){ Return {Code: 200, message: 'login success .' }} Else { Return {Code: 401, message: 'login faild .' }}}
The first step is to do these basic checks first.
- Input Review: input check, mainly for parameter validity check and security check
- Output Review: Check the output and perform contract checks on the returned values. For example, a function that calculates the salary cannot return negative numbers;
- Exception Review: exception check in two aspects
- First, a method you call may throw an exception. Do you want to capture it or continue to throw it up;
- Second, you may throw any exception in this function, so you have to think about it;
- Null reference review: NULL reference check. Can a variable you get be blank? Empty reference is the most common low-level error;
- Out of bounds Review: checks for out-of-bounds indexes. The use of arrays and dictionaries may result in out-of-bounds indexes;