简体   繁体   中英

How do i condense these return statements or avoid the checkstyle error all together?

Currently, I'm working on writing classes for a brick breaker game for my java class. I have a question about a checkstyle error I'm getting for my public javafx.scene.paint.Color getColor() method which gets the color of the brick based on how many times it has been hit by the ball.

The checkstyle error says: Return count is 6 (max allowed for non-void methods/lambdas is 2). which I assume it means I can only have 2 return statements as opposed to the 6 that I have currently. All of the statements have different returns based on the value of this.hits which is what I don't understand. The documentation for it is as follows:

public javafx.scene.paint.Color getColor()

The current color to represent this Brick's breakability state.
Returns:There are five possible Colors that can be returned based on 
theBrick's current strength: Color.BLACK if this Brick cannot be 
broken;Color.WHITE if this Brick has been completely broken; and 
Color.RED, Color.YELLOW,Color.GREEN if this Brick will break after 1, 2, 
and 3 more hits, consecutively.

And the code:

public javafx.scene.paint.Color getColor() {

        if (this.hits == -1) {
            return javafx.scene.paint.Color.BLACK;

        } else if (this.hits == 0) {
            return javafx.scene.paint.Color.TRANSPARENT;

        } else if (this.hits == 1) {
            return javafx.scene.paint.Color.RED;

        } else if (this.hits == 2) {
            return javafx.scene.paint.Color.YELLOW;

        } else if (this.hits == 3) {
            return javafx.scene.paint.Color.GREEN;
        }

        return null;
}

You could populate a HashMap<Integer, Color> with your values and use a single return off fetching the value from the Map . Like,

private static Map<Integer, javafx.scene.paint.Color> COLOR_MAP = new HashMap<>();
static {
    COLOR_MAP.put(-1, javafx.scene.paint.Color.BLACK);
    COLOR_MAP.put(0, javafx.scene.paint.Color.TRANSPARENT);
    COLOR_MAP.put(1, javafx.scene.paint.Color.RED);
    COLOR_MAP.put(2, javafx.scene.paint.Color.YELLOW);
    COLOR_MAP.put(3, javafx.scene.paint.Color.GREEN);
}

public javafx.scene.paint.Color getColor() {
    return COLOR_MAP.get(this.hits);
}

The following method does the same with one return statement:

    public javafx.scene.paint.Color getColor() {

        javafx.scene.paint.Color color = null;

        if (this.hits == -1) {
            color = javafx.scene.paint.Color.BLACK;

        } else if (this.hits == 0) {
            color = javafx.scene.paint.Color.TRANSPARENT;

        } else if (this.hits == 1) {
            color = javafx.scene.paint.Color.RED;

        } else if (this.hits == 2) {
            color = javafx.scene.paint.Color.YELLOW;

        } else if (this.hits == 3) {
           color = javafx.scene.paint.Color.GREEN;
        }

        return color;
 }

Completing @cOder and @Elliot answers, you can use switch-case to improve your code. In your case, using this architecture is faster to run and you don't have to save a HashMap into memory to make it work.

public javafx.scene.paint.Color getColor() {
    javafx.scene.paint.Color color;
    switch (this.hits) {
        case -1:
            color = javafx.scene.paint.Color.BLACK;
            break;
        case 0:
            color = javafx.scene.paint.Color.TRANSPARENT;
            break;
        case 1:
            color = javafx.scene.paint.Color.RED;
            break;
        case 2:
            color = javafx.scene.paint.Color.YELLOW;
            break;
        case 3:
            color = javafx.scene.paint.Color.GREEN;
            break;
        default:
            color = null;
            break;
    }

    return color;
}

You can take a look at this request because it slightly explains the difference between our 3 answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM