You have been there and done that thousands of time, but did you know that switch statement is a code smell? I am sure lot of people don’t know about this. I my self got surprise to know that switch statement is a code smell, when I attended my first agile process training.
Why switch statement is code smell?
Where ever there is a need for switch statement there is a good chance of polymorphism. But we must need to consider the context as well. Note that there is another form of switch statement,
(if … else if … else if .. ) is also another from of switch statement where polymorphism can be considered.
One thing we must consider that where converting a switch statement to a polymorphic solution often create a bunch of classes so make sure before creating classes that they worth doing and actually abstract some part of the code and take significant amount of code in a child class so that it does not become a lazy class.
No comments:
Post a Comment