4 ms·
I've always seen switch statements as an anti-pattern in C#. These examples however, do demonstrate neat time saving techniques. Does anybody have examples of
by jesseryoung 7y ago
I've always seen switch statements as an anti-pattern in C#. These examples however, do demonstrate neat time saving techniques.
Does anybody have examples of real-world use cases that take advantage of this that couldn't (or shouldn't) be solved in a more OO way (Visitor, Strategy, Template Method, etc)
- sixothree 7y agoI try to use switches only for enums or things with a 1 to 1 simple mapping. Enums to strings for example. Although even there my preference is to use an attribute on the enum.
- rswail 7y agoExcept that enumerations are not just "strings as magic numbers". Rust's enumerations (and others) enforce that all the enumerations are dealt with in the match (a superclass of switch, mixing metaphors).
- JamesBarney 7y agoWhy 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.
- wolfspider 7y agoWell sure, I’ve been using “is” alongside switch statements for a while now but admittedly doesn’t come up too often. Let’s say you have a ControlTemplate that’s so deep in the stack you have to pull symbols for it yet most of the properties are inaccessible in the current scope. The only thing it will spit out which is accessible at runtime is the type. The requirement is to process the data correctly (e.g. transform it into a List<T> or custom object). You could either write GetType() all over the place and attempt to access this mega-object at runtime or just use a switch statement to determine the resulting type with “is”. The situation I was in where this became preferable was an application where the user picks the controls themselves (a form builder with fancy controls) which has the ability to add 3rd party controls from different libraries. It reminded me of throwing darts blindfolded- case target is bullseye: try { throw.dart; } catch(Exception ouch) { LogThrows(“you probably don’t want to catch this”, ouch); } break;
- Merad 7y agoAt work our team just finished implementing a filtering library for our web services that's loosely based on OData. The query string value is parsed into an expression tree, then all of the operations on the expression tree are implemented using pattern matching. You could of course implement this with the visitor pattern, but in comparing the two we decided to use pattern matching because it's significantly less verbose and easier to understand.