Here I am going to share my experience as how I used enum without using switch statements. Now let’s see what is the problem in using switch statements in your code base.
Before that we can recall what is an enum.
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
Problem with Switch cases:
A solution to a problem may need different logics/execution paths based on the different cases set. So basically each case of an enum in a switch statement takes on a particular path. As the application code base grows whenever we add a new case to the enum we have to refactor every single place the enum is switched. So basically you end up a in a long list of switch in lot of places there by increasing the code complexity.
Real world example
To understand better let us understand with a real world example.
We want to develop an app that resolves the covid regulations on a given location based on the number of actively affected patients. Based on the number of affected patients per 1000 residents, there are four phases like Green, Yellow, Light Red, Dark Red.
Based on the phase the corresponding covid regulations will be displayed in the UI.https://pratheeshbennet.medium.com/media/353d3a58e47d2f7a2398601b32a63760
To set the themes and data, I have four types of struct namely Green, Yellow, LightRed, DarkRed and Undetermined. Each type has the corresponding UI themes like colours and data content. Let’s see sample for Green type alonehttps://pratheeshbennet.medium.com/media/4d406e95fa8a6db1b7bf80f3d166f66c
Theses structs will drive the entire app UI and content.
So the first idea to set and check values will look something like thishttps://pratheeshbennet.medium.com/media/ee96f804d69d017ffae73cdd5fa1ff13
This is a very big code smell as tomorrow when the cases grow in the Phase enum the switches also grow.
The idea is to dismantle the switches by the use of dictionaries in the place of switches to the below points,
- Map the cases green, yellow, lightRed and darkRed to the custom types Green, Yellow, LightRed and DarkRed respectively.
- Set the Phase based on the number of active cases.
Solution: Using dictionaries to replace switch cases
For both the uses cases I am going to use dictionary,
The dictionary mappingType maps the case to the custom types Green, Yellow, LightRed and DarkRed respectively. The dictionary range is used to set the Phase based on the number of active cases.https://pratheeshbennet.medium.com/media/36cf54213686966e88f6d5d578daaa2d
Setting and consuming the cases are as below,https://pratheeshbennet.medium.com/media/682575bb43f2ea04d564d6aa4a922c25
Here we can see that we have avoided the usage of Switch cases to set and get the corresponding cases of the enum. The entire core functionality is moved inside the enum. The case alone can be set from the calling classes. And moreover the implementation is now dynamic. In the event of adding more themes or ranges, we just need to add those values in the dictionaries.
Note that the setAttributes function can work on any type that conforms to the protocol Phaseable thereby hiding the actual type.
The dictionary approach here lends itself nicely to being more dynamic and clean, concise code. Also as a bonus we have also seen the dynamic mapping of Structs for the cases in an enum and dynamic configuration of the UI using type erasure.
Happy clean coding. Any thoughts please feel free to reach me at email@example.com.