I'm using Java 6.
Suppose I have an enum with 6 values, ordered A to F. About 4 of the values were handled 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 this.
switch (whichType) {
case E:
return someothertask();
case F:
return anothersomeothertask();
default:
return task();
}
Null values will never reach this switch.
In terms of conciseness and clarity, the second approach is better. In terms of being explicit, I think the first approach is better.
Are there any other pros/cons of each approach?
Also, this simple question risks being a duplicate, but I tried and couldn't find it asked anywhere yet. I apologize if I haven't searched it good enough.