2 ms·
Replacing a switch statement or large if statement with a virtual call (enum -> interface refactoring) can definitely be very beneficial. It can turn [crimes ag
by Strilanc 14y ago
Replacing a switch statement or large if statement with a virtual call (enum -> interface refactoring) can definitely be very beneficial. It can turn [crimes against humanity](https://github.com/aidanf/Brilltag/blob/master/Tagger_Code/final-state-tagger.c#L145 https://github.com/aidanf/Brilltag/blob/master/Tagger_Code/f...) into perfectly respectable code.
But, obviously, don't take this too far. If you find yourself not using an if statement (or ternary operator) when writing the absolute value (for non-optimization reasons)... you've gone too far.
int UnclearAbs(int value) {
return value * (value >> 31);
}
- lmm 14y agoAbuse of bitwise operators is an abomination, but I'd instinctively write "value.abs" (scala), which is clearer than an if. Obviously many things are ultimately implemented using if, but it's too low-level a construct to be using for day-to-day work.