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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With