3 ms·
I know senior Java devs with 20 years experience, who can not write switch statement. It is pretty limited language construct.
by jankotek 9y ago
I know senior Java devs with 20 years experience, who can not write switch statement. It is pretty limited language construct.
- jostmey 9y agoIt's been years since I've used a switch statement. I have completely forgotten the correct syntax, though I could look it up in a second
- paulddraper 9y agoIt's weird, and even harmful in many cases. A modern goto of sorts: it has performance benefits [1], but not overall readability ones. switch (x) { case 0: doThing1(); break; case 1: doThing2(); break; default doThing3(); } vs if (x == 0) { doThing1(); } else if (x == 1) { doThing2(); } else { doThing3(); } This somewhat unsafe form of switch-case made it into C, Java, C#, and JavaScript, and cases the great iOS SSL bug of 2014. [2] [1] https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device [2] https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/ https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-got...
- archagon 9y agoI use it because if your switch is exhaustive, it's nice that the compiler can tell you exactly which parts of your code need to be updated when you add cases to an enum. (Also, in Swift, fallthrough is opt-in instead of opt-out, which is great.)
- paulddraper 9y agoScala pattern matching is the same. Both are less easy to misuse than C switch.
- gremlinsinc 9y agoLaravel dev 4+ years... I use switch VERY rarely.. only if I have 4+ different possible outputs from the result of a variable... I also think switch's are kind of ugly and don't jive w/ my mindset.. I think more in if/then's... if using loops using laravel collections instead.