3 ms·
the ifs allow the code to function correctly even if you forget to update this function with newly-added cases in the enums. The switch statement makes the know
by pdpi 10y ago
the ifs allow the code to function correctly even if you forget to update this function with newly-added cases in the enums. The switch statement makes the known-cases behave O(1), while also bypassing unneeded validation.
- akkartik 10y agoThe known cases are O(1) even with an `if` -- all it's doing is a range check. Any performance benefit from this hack is miniscule at best. It may even end up confusing a compiler and making performance worse.
- pdpi 10y agoSure. I'm not arguing it's a good idea. It just serves as (somewhat forced) motivation for showing how to exploit this behaviour with switch labels, which is the real point of the post.