4 ms·
I realized that I essentially never use a "switch" statement. It seems like a control-flow construct that made sense in the 1970s to help the compiler generate
by kens 4y ago
I realized that I essentially never use a "switch" statement. It seems like a control-flow construct that made sense in the 1970s to help the compiler generate jump tables, but doesn't seem particularly useful now. Moreover, it seems error-prone with accidental fall-through. And doing "fancy" things with it makes code very hard to read. Does anyone else find "switch" kind of redundant?
- aidenn0 4y agoI use switch statements all the time for state-machines. There are linters that will warn on fallthrough; you should use one of them.
- oweiler 4y agoI still find switch statements easier on the eye than a chain of if-else-if-...else.
- tristan957 4y agoswitch statements are better for certain cases since the compiler can warn if you aren't handling an enum variant within it.
- silisili 4y agoI typically use them with more than 2 or 3 options as I find them much easier to read. In addition, the compiler/vm typically can optimize them better than long if chains, depending on implementation, if that matters to you