Are there instances where switch(case) is is a good design choice (except for simplicity) over strategy or similar patterns...
相关问题
- Switch String with Enum variables
- Name for a method that has only side effects
- What is a correct approach to manage test data usi
- Can a [GoF]-ConcreteSubject override the notify me
- Can the builder pattern ever be doing too much?
相关文章
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- go tutorial select statement
- What is Scope Creep? [closed]
- Builders in Java versus C++?
- “Adapter” or “adaptor”?
- Is copy-and-paste coding ever acceptable?
My view is that switch is always wrong:
The body of a case is code and is behaviour, therefore, the thing in the case (the 'value') has a behavioural type, therefore, polymorphism would be a better choice.
This implies that values are in fact types, e.g. the number 1 is a type of everything that equals 1 in some way. All that remains is for us to map 1-ness to the behaviour for our specific case, and we have polymorphism with all those other types (a Good Thing).
This is easier done in some languages than others, unfortunately, most languages in common use are pretty awful, so the path of least resistance is the wrong one, and people end up writing switches or if statements (same thing).
it's usually ok, as long as you only have one switch in one place. when you have more than one (or many), then it's time too consider alternatives.