Refactoring Conditional Structures with Map
I often come across code snippets like this:
public class Day {
public void start(Weather weather) {
switch(weather) {
case RAINY:
takeAnUmbrella();
break;
case SUNNY:
takeAHat();
break;
case STORMY:
stayHome();
break;
default:
doNothing();
break;
}
}
}
Here, a specific action depends on the weather. This kind of code is difficult to test and maintain. This short article shows how to refactor it using a Map
.