3 ms·
Not so much .NET/Java but Structured Programming (1972 Edsger W. Dijkstra, Ole-Johan Dahl, and Tony Hoare) has prompted the gradual backlash against switches. (
by Supermancho 5y ago
Not so much .NET/Java but Structured Programming (1972 Edsger W. Dijkstra, Ole-Johan Dahl, and Tony Hoare) has prompted the gradual backlash against switches. (https://youtu.be/eEBOvqMfPoI?t=1512 https://youtu.be/eEBOvqMfPoI?t=1512, re: Duff's Device). Senior developers should watch the whole thing to understand how block scope and functions and classes relate.
Modern development has drifted toward lots of small functions (which causes a mess because modern IDEs dont have supporting features like this: https://youtu.be/baxtyeFVn3w?t=1786 https://youtu.be/baxtyeFVn3w?t=1786, re: gtoolkit). A switch requires you to look inside and reason about the internals of a function to understand how it will behave before exiting, as the compiler. This doesn't matter so much if you dont care about early exits (no branch prediction, etc). Either way, it's extra cognitive overhead and error prone for a small improvement in readbaility. It's the line where programming becomes tricky programming.
As a thought experiment, imagine there is ANOTHER keyword that acts like an if/else (or switch with mandatory breaks). If A. it's terse, B. it's structured, C. it's readable, would that be better?
We primarily use switch because it's easy to read (versus a ton of if{}/else{}), as it gives us almost-block scoping without having to define another indirection/function. Python has this switch equivalent in match/case, but you have to use Python to get it.
Switch use is a syntax problem. Lots of software is colored by bad syntax and language design^.
^ I think most people realize that type hierarchies are a brittle/limited form of mixin. Even Java has softened to this a bit (aspects, etc) but will likely never be fun to use.