4 ms·
switch is an extremely useful control structure, though needed rarely. It's basically a kind of computed goto, something not found very often outside C/C++ anym
by edflsafoiewq 7y ago
switch is an extremely useful control structure, though needed rarely. It's basically a kind of computed goto, something not found very often outside C/C++ anymore.
If there's a real inspect, people will be able to stop treating switch as a deficient approximation to one, and maybe appreciate it for what it is.
- dvt 7y agoSwitch is used pretty often when UI needs to look different based on certain application states (consider any 3+ state button). I was curious if I used it recently, and, in fact, here it is in some code I wrote a few months ago[1]. It's also used all the time (also happens to be fairly idiomatic) when dealing with Redux, MobX, etc. [1] https://github.com/dvx/lofi/blob/master/src/renderer/components/Lofi/Cover/index.tsx#L232 https://github.com/dvx/lofi/blob/master/src/renderer/compone...
- edflsafoiewq 7y agoBut that, like most uses of switch, is essentially uninteresting as a control-flow construct. It's just a stylistic point to use a switch here instead of an if or a match. JS's switch has no extra power other than fallthrough anyway. Here's a poor-man's coroutine (with serializable state!) where switch plays a more interesting role. void move_robot(state* s) { switch (s->resume_at) { case 0: #define SUSPEND(resume_pt) s->resume_at = resume_pt; return; case resume_pt:; #define PAUSE(resume_pt) s->pause_time = 20; while (s->pause_time) { SUSPEND(resume_pt) } for (;;) { /* three knight moves, then forward */ for (s->i = 0; s->i != 3; s->i++) { forward(s); PAUSE(1); forward(s); PAUSE(2); turn_left(s); forward(s); PAUSE(3); } forward(s); } } }
- saagarjha 7y agoSwitches are quite useful when you have a finite set of options you want to match against.
- stabbles 7y agoYeah, it creates a jump table, so basically an O(1) operation in assembly rather than O(n) comparison worst case for every case. However, this means switch _only_ works with integers, which is rather limited and confuses newcomers. For instance this does not compile: void f(std::string const &str) { switch(str) { case "option_1": std::cout << "First option\n"; break; case "option_2": std::cout << "Second option\n"; break; default: std::cout << "None of the above\n"; } }
- beached_whale 7y agoThe workaround is constexpr hashing void f(std::string const &str) { switch(hash(str)) { case hash("option_1"): std::cout << "First option\n"; break; case hash("option_2"): std::cout << "Second option\n"; break; default: std::cout << "None of the above\n"; } } It could be as little as converting the final 8 bytes to a uint64_t above and using that. That just happens to be all of them in this case, but there are a bunch of them. Or using minimal perfect hashing. None of this is the language though
- rowanG077 7y agoCan you guaranty at compile time that any hash you switch on is unique? Else this is just really dangerous if you don't have perfect hashing for the object you switch on.