Hacker News new | past | comments | ask | show | jobs | submit login

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.




>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.


Because 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.


That is not the case in C# though, the break is required by the syntax.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: