Refactoring Conditional Structures with Map
I often encounter pieces of code that look 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 must be taken depending on the weather. This kind of code is pretty hard to test and maintain. This short article aims to refactor it using a Map
.