DEV Community

Cover image for Code Smell 36 - Switch/case/elseif/else/if statements
Maxi Contieri
Maxi Contieri

Posted on • Updated on • Originally published at

Code Smell 36 - Switch/case/elseif/else/if statements

First programming lesson: Control structures. Senior developer lesson: avoid them.


  • Too many decisions together

  • Coupling

  • Duplicated code

  • Violation of Open/Closed Principle.

  • A new condition should not change the main algorithm.

  • Nulls


  1. Polymorphism

  2. Create hierarchies/compose objects following Open closed principle.

  3. Use State pattern to model transitions.

  4. Use Strategy Pattern/Method Object to choose for branches.


  • Discrete Values

  • State transition

  • Algorithm choice.

Sample Code




Since there are valid cases for If/else usages, we should not pull the plug and forbid these instructions. We can put a ratio of if statements/other statements as a warning instead.


More info


Photo by Adarsh Kummur on Unsplash

If debugging is the process of removing software bugs, then programming must be the process of putting them in.

Edsger Dijkstra

Top comments (3)

richardsprins profile image
Richard S Prins Jr.

The code isn't at all different, not sure of the intent here?

mcsee profile image
Maxi Contieri

If you see the code exactly the same is due to a bug in cache. Try refreshing the cache

moopet profile image
Ben Sinclair

It's been a couple of years and it's definitely the same for me :)