I found a piece of code on the internet that has a really simple objective, yet it uses an ugly approach. Supposedly, the author is using a switch case to determine if a few (non-contiguous) values of a previously-defined Enum belong in their scope. If it does, the function returns true
and that's that. Else, it returns false
.
It practically looks like this:
switch(value) {
case ONE:
case TWO:
/* many similar lines later */
case TWENTY:
case TWENTY_FIVE:
/* an afternoon later */
case ONE_HUNDRED:
return true;
default:
return false;
}
Their use of a switch case is justified with having an instant lookup thanks to a jump table generated by the compiler (even though a jump table doesn't necessarily mean instant lookup from what I've gathered). Even so, this generates countless needless lines of code.
I've read about function inlining and using an array of function pointers, but I don't know how to use that in such a specific case.
How do I avoid writing many lines of case X:
with such a simple case (no pun intended) as this?