When using a switch statement, with an exhaustive list of available items (for example an enum), and in the event that each item has its own conditional code, should I use the default label? For example:
public class MyClass {
public enum Type {
TYPE1, TYPE2
}
private Type type;
public void withDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
default:
// some conditional code for TYPE2
break;
}
}
public void withoutDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
case TYPE2:
// some conditional code for TYPE2
break;
}
}
}
In that case, what should I use: the withDefault() method or the withoutDefault() one? Or maybe is it only a matter of taste?
I usually use a case for each TYPE1 and TYPE2 and then a default that throws an exception, so that later when a type is added the exception will remind me to change the switch.