About Java coding conventions

First, try to use statements Wei

Wei concept statement

The conditional expression usually has two forms, the first form is: all the branches are all normal behavior; The second form is: The answer provided by the conditional expression, only one is normal behavior, others are not common Happening. These two types of conditional expressions for different purposes, it should be manifested through the code.

If the two branches is normal behavior, you should use the form if ... else ... conditional expression; if a certain condition is extremely rare, you should check the condition of the individual, and is true immediately from the condition function returns. Such checks alone often called "Guardian statement" (guard clauses)

1. Alternatively condition check

This is a method to calculate employee salary, which pay special rules for handling of foreign employees and retirees. The situation is not often, but it does occasionally occur.

public double getSalary() {
    double result;
    if (this.isSeparated) {//驻外员工
        result = this.separatedSalary();
    } else {
        if (this.isRetired) {//退休员工
            result = this.retiredSalary();
        } else {//正常员工
            result = this.normalSalary();
        }
    }
    return result;
}

This code, check the abnormal circumstances of the case to cover up examination was normal, so it should be replaced with these conditions Guardian statement checks to improve program clarity. For each check, put a guard statement. Wei statement or to return from the function, or to throw an exception.

public double getSalary() {
    if (this.isSeparated) {//卫语句
        return this.separatedSalary();
    }
    if (this.isRetired) {//卫语句
        return this.retiredSalary();
    }
    return this.normalSalary();
}

2. Replace the reverse condition

This method is a known seek length and breadth of the cuboid volume, but there are special requirements: when the height is greater than 0, the print 万猫学社. (Meaning no surprise surprise no unexpected sudden metamorphosis is not no abrupt change, and sometimes we received such a demand is????.) Code is as follows:

public double getVolume(double length, double width, double height) {
    double result = 0.0;
    if (height > 0.0) {
        System.out.println("万猫学社");
        if (length > 0.0 && width > 0.0) {
            result = length * width * height;
        }
    }
    return result;
}

Or replaced with guard statements condition check, but we need the appropriate conditions reversed, that is, to do the logical NOT operation.

public double getVolume(double length, double width, double height) {
    if (height <= 0.0) {//卫语句,!(height > 0)
        return 0.0;
    }
    System.out.println("万猫学社");
    if (length <= 0.0 || width <= 0.0) {//卫语句,!(length > 0 && width > 0)
        return 0.0;
    }
    return length * width * height;
}

Why use guard statement?

Wei is the essence of the statement: a branch to give a special attention. If you are using if ... else ... structure, if you focus on the branch and the branch else it is equal. This code structure passed to the readers of the message is this: each branch has the same importance.

Wei statement is different, it tells readers: "This is rare, if it really happened, please do the necessary finishing work, then quit." If the remainder of the method is no longer interested, of course, should immediately drop out. Boot code reader to see a useless else section, will only hinder their understanding. Wei with the statement after the code easier to understand, it is maintained.

 

 

Reproduced in part: https: //www.cnblogs.com/heihaozi/p/11818042.html

Guess you like

Origin www.cnblogs.com/jmy520/p/11846746.html