Reduce method nesting by returning earlier

The method can not only simplify the logical judgment of the method, but also make the nested relationship in the method more concise. Handle exceptions first, simple cases and return, and complex cases as primary logic last. If a function needs to perform a cleanup operation before returning, different languages provide different cleanup mechanisms. In Java, yestry finallyMechanism implementation.Copy the code

Example 1: Complex logical judgments and nested relationships make code unreadable

        if (user_result == SUCCESS) {
            if(permission_result ! = SUCCESS) {reply.writeErrors("error reading permissions");
                reply.done(a);return;
            }
            reply.writeErrors("");
        } else {
            reply.writeErrors(user_result);
        }
        reply.done(a);Copy the code

Example 1 applies early return, formatted code is more readable and elegant:

        if(user_result! =SUCCESS){reply.writeErrors(user_result);
            reply.done(a);return;
        }
        
        if(permission_result! =SUCCESS){reply.writeErrors(permission_result);
            reply.done(a);return;
        }
        
        reply.writeErrors("");
        reply.done(a);Copy the code

Arrange the order of if/else blocks

In the writingif/elseStatement, you are usually free to change the order of blocks. For example, you could write:Copy the code
        if (a == b) {
            //case one
        } else {
            //case two
        }
Copy the code

It can also be written as:

        if (a! = b) {//case two
        } else {
            //case one
        }
Copy the code
So what's the best, more readable way to arrange blocks? It has been proven that dealing with the positive logic of the control flow first rather than the negative logic makes the code easier to understand. For example, useCopy the code

If (debug) instead of if (! The debug).

Arrange the order of parameters in a conditional statement

There are rules for arranging the order of parameters in conditional statements:Copy the code

On the left side of the comparison: the “questioned” expression, which tends to keep changing. To the right of the comparison: the underlying expression of the comparison, whose value tends to be more stable

Here are a few examples to see which solution is easier to read: example 1:

if ( length> =10) orif ( 10< =length )
Copy the code

Example 2:

while ( bytes_received < bytes_expected ) orwhile ( bytes_expected > bytes_received )
Copy the code

The principle of using ternary operators

Using the trinary operator for choosing between two simple values makes the code more compact and readable. Remember to use it only for choosing between two simple valuesif/elseAnd other conditional selection statements. Here are a few examples to get a feel for which solution is easier to read:Copy the code

Example 1:

time_str += (hour> =12)?"pm" : "am"; orif (hour> =12) {
    time_str += "pm" ;
} else {
    time_str += "am";
}
Copy the code

Example 2:

return exponent> =0 ?
           mantissa * (1 << exponent) : 
           maintissa /(1 << -exponent); orif (exponent> =0) {return mantissa * (1 << exponent );
} else {
    return mantissa / (1 << -exponent);
}

Copy the code