I would prefer
double apply(double x, double y) {
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
default: assert this==DIVIDE: return x / y;
}
}
- We should not be throwing
AssertionError
because it should be reserved for actual assertions.
- Other than assertions and some catch blocks there should not be a single bit of code that can not practicably reached.
But I would most prefer https://stackoverflow.com/a/41324246/348975