Introduction
Switch... Case statement is used for conditional operations. Sometimes, it is considered in the category of code smell. Switch case is not a bad syntax, but its usage in some cases categorizes it under code smell. It is considered a smell, if it is being used in OOPS. Thus, Switch case should be used very carefully.
Why is Switch... case considered as a code smell?
Switch...case is considered as a code smell due to the reasons given below.
- Violation of Open and close principle
Each time a developer wants to add a new type then he or she has to insert new switch.. case in each section. It promotes modification of an existing code.
- Difficult to maintain
Switch... case can grow on the basis of new requirements. Switch case is being kept in a single function, so function will increase and and maintenance will be hard.
- Provides the chances of Redundant Code
Switch... case provides the redundant code in some cases. The code given below is an example of containing lots of redundant codes.
- switch(input)
- {
- case "a":
- CreateValue("Hello");
- myData+= "Hello";
-
- case "b":
- CreateValue("Hi");
- myData+= "Hi";
-
- case "C":
- CreateValue("Hey");
- myData+= "Hey";
-
- default:
- myData = string.empty;
- }
Switching on type is a bad OOP style of programming, however switching on a value is fine.
How to Refactor the issue of code smell in Case...switch?
Switch... case can be refactored by using Polymorphism and strategy pattern with pattern searching.
The code given below is an example of Switch...case statement.
- public List<string> GetSortedData(string searchType, List<string> data)
- {
- List<string> sortedData = null;
-
- switch (searchType)
- {
- case "BubbleSort":
-
- sortedData = data;
- break;
-
- case "HeapSort":
-
- sortedData = data;
- break;
-
- case "MergeSort":
-
- sortedData = data;
- break;
-
- case "InsertionSort":
-
- sortedData = data;
- break;
- }
-
- return sortedData;
- }
In the code example given above, if a new sort type case is needed then the existing method will be modified and code sortedData for the assignment of the sorted data is redundant.
Thus, the code given below is an example with Polymorphism, strategy pattern, and pattern search.
- public interface ISortData
- {
- List<string> GetSortedData(List<string> data);
- }
-
- public class BubbleSortData : ISortData
- {
- public List<string> GetSortedData(List<string> data)
- {
-
- return data;
- }
- }
-
- public class HeapSortData : ISortData
- {
- public List<string> GetSortedData(List<string> data)
- {
-
- return data;
- }
- }
-
- public class MergeSortData : ISortData
- {
- public List<string> GetSortedData(List<string> data)
- {
-
- return data;
- }
- }
-
- public class InsertionSortData : ISortData
- {
- public List<string> GetSortedData(List<string> data)
- {
-
- return data;
- }
- }
-
- public class SortingContext
- {
- private Dictionary<string, ISortData> sortStrategy = new Dictionary<string, ISortData>();
-
- public SortingContext()
- {
- sortStrategy.Add("BubbleSort", new BubbleSortData());
- sortStrategy.Add("HeapSort", new HeapSortData());
- sortStrategy.Add("MergeSort", new MergeSortData());
- sortStrategy.Add("InsertionSort", new InsertionSortData());
- }
-
- public List<string> GetSortedData(string searchType, List<string> data)
- {
- return sortStrategy[searchType].GetSortedData(data);
- }
- }
-
- public class SortingClient
- {
- List<string> data = new List<string> { "Ball", "Apple", "Cat" };
- SortingContext sortingContext = new SortingContext();
- List<string> result = sortingContext.GetSortedData("BubbleSort", data);
- }
Conclusion
Switch... case is normally a very good conditional statement but in some cases, especially with OOPS, it gives birth to the code smell issue. This issue breaks some laws of the code design. Thus, it can be refactored by using polymorphism, strategy pattern, pattern searching etc.