DEV Community

Discussion on: Your most controversial opinion

Collapse
 
cjbrooks12 profile image
Casey Brooks

Don't use switch statements or chained if-else statements. While they certainly can be useful in some situations, the majority of cases I see it in use signals a place where that logic needs to be extracted behind an interface, where each case is an implementation of that interface. The presence of a switch is a major code-smell when it comes to building maintainable or modular software.

Collapse
 
elmuerte profile image
Michiel Hendriks

So basically a chain-of-responsibility pattern. I can get behind that.

I think we can adapt Craig Zerouni's quote a bit to give us a nice rule:

If you have too many [special] cases, you're doing it wrong.

Collapse
 
cjbrooks12 profile image
Casey Brooks

Ah, I had no idea there was a name for this pattern, but that's exactly it! I see references to GoF everywhere, and I usually find I'm actually doing a bunch of these patterns myself already when I see it. Maybe I should actually read that book sometime...