3 ms·
Why are switches an anti-pattern? Switching a simple switch with a strategy pattern can make code far more complex and hard to understand. Switches are simple
by JamesBarney 7y ago
Why are switches an anti-pattern?
Switching a simple switch with a strategy pattern can make code far more complex and hard to understand. Switches are simple to code, simple to read, and easy to change.
- flukus 7y ago>Why are switches an anti-pattern? Because the gods of OO decreed as such and now the logic must be distributed across a dozen classes/files. Notice the language "that couldn't (or shouldn't) be solved in a more OO way", their goal is to create OO code, not simple to read and easy to change code.
- tigershark 7y agoBecause it’s a switch statement, not an expression and a lot of people tend to use it for doing stuff more complex than a single line. Being it a statement there is nothing that forces you to return from each case and you may forget to use the break keyword mistakenly executing also the next case.
- goto11 7y agoThat is not the case in C# though, the break is required by the syntax.