Switch statements are bad? [closed]

A switch is like any other control structure. There are places where it’s the best/cleanest solution, and many more places where it’s completely inappropriate. It’s just abused way more than other control structures.

In OO design, it’s generally considered preferable in a situation like yours to use different message types/classes that inherit from a common message class, then use overloaded methods to “automatically” differentiate between the different types.

In a case like yours, you could use an enumeration that maps to your action codes, then attach an attribute to each enumerated value that will let you use generics or type-building to build different Action sub-class objects so that the overloading method will work.

But that’s a real pain.

Evaluate whether there’s a design option such as the enumeration that is feasible in your solution. If not, just use the switch.

Leave a Comment