I am using Java 6.
Suppose I have an enumeration with 6 values, ordered A to F. About 4 values were processed the same. I could write it like this.
switch (whichType) {
case A:
case B:
case C:
case D:
return task();
case E:
return someothertask();
case F:
return anothersomeothertask();
}
Or like that.
switch (whichType) {
case E:
return someothertask();
case F:
return anothersomeothertask();
default:
return task();
}
Null values will never reach this switch.
In terms of brevity and clarity, the second approach is better. As for the explicit, I think the first approach is better.
Are there any other pros / cons of each approach?
Also, this simple question may be duplicate, but I tried and could not find it anywhere. I apologize if I did not look for him well enough.
source
share