Frog frog recommendation: stupid way to improve code Quality

Source: Internet
Author: User

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.

    1. Input Review: input check, mainly for parameter validity check and security check
    2. 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;
    3. Exception Review: exception check in two aspects
      1. First, a method you call may throw an exception. Do you want to capture it or continue to throw it up;
      2. Second, you may throw any exception in this function, so you have to think about it;
    4. Null reference review: NULL reference check. Can a variable you get be blank? Empty reference is the most common low-level error;
    5. Out of bounds Review: checks for out-of-bounds indexes. The use of arrays and dictionaries may result in out-of-bounds indexes;

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.