Conditional becomes a code smell when we have to check an object’s type in order to make some logic or behavior decision. It doesn’t matter whether it is a stack of if/else block or a switch statement.This violates open-closed principle.
The open closed principle states that the entities(classes, modules, functions etc) should be open for extension,but closed for modification.Which means that these entities won't be allowed to make changes in its source code.
This can be achieved through Abstraction and Polymorphism.
Benifits of Polymorphism over Conditionals
- instead of asking an object about its state and then performing actions based on this, it is much easier to simply tell the object what it needs to do and let it decide for itself how to do that.
- Removes duplicate code. You get rid of many almost identical conditionals.
- If you need to add a new execution variant, all you need to do is add a new subclass without touching the existing code (Open/Closed Principle).