Switch statement smells

We all know Switch Case statement is very handy to use when we need to write code for decision-making based on some constants, but we can avoid it by using polymorphism or Enums(if preferred). The most important reason behind avoiding or refactoring existing switch statements is it violates Open Close Principle of SOLID Design Pattern

How ?

Lets have an example –

switch (animal) {
            case "Dog":
                //Todo do something
                break;
            case "Fox":
                //Todo do something
                break;
            case "Cat":
                //Todo do something
                break;
        }
  •  Now here the problem is if a new animal get introduced to the system, this switch needs to change except some exceptional scenarios, that means this switch is no more close for modification. And in some cases you might miss to update that new animal’s information within this switch as your compiler will not able to detect it.
  • Next issue will be if you have to take decision in multiple classes or in multiple methods based on animals you will landed up with another duplicate switch statement. Which will make your system difficult to maintain in future.
  • Again if the list is long, it makes your method long which will be again difficult to analyze.

Solution ?

Looking for the solution of this. You can solve it using any of two approaches, lets start with polymorphism

Approach 1 – Polymorphism

You can implement using abstract class or interface like  –

abstract class Animal
        {
            public abstract void doSomething();
        }
        
        class Dog extends Animal
        {
            public void doSomething()
            {
            }
        }
        
        class Fox extends Animal
        {
            public void doSomething()
            {
            }
        }

        class Cat extends Animal
        {
            public void doSomething()
            {
                
            }
        }

Approach 2 – Using Enums

This is another approach to avoid Switch Case statement. But few platforms do not recommend to use Enum as it affects performance like Android.

Let’s see how can use Enums in this case –

enum Animal{

        Dog{
            @Override
            public void doSomething() {
                
            }
        },
        Fox{
            @Override
            public void doSomething() {

            }
        },
        Cat{
            @Override
            public void doSomething() {

            }
        };
        
        Animal(){
            
        }
        
        public abstract void doSomething();
    };

Both the approaches –

  • will satisfy Open close priniciple.
  • It will prevent the code redundancy.
  • And makes methods short and precise.

For any further information check this out SwitchStatementsSmell

That’s all from my side on this article. If you like this please share this and comment your views.

 

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *