Lately I have been trying to extract till I drop. Well not necessarily till I drop, but I've been trying to be more strict and look at some metrics of my code.
I have now come along an old class of mine which has a rather large switch case. Metrics say the method lines of code are beyond evil. It goes something like this:
private void startProcessing(Map<MyKeyEnum, String> map) {
Processor myProcessor = new Processor();
for (Entry entry : map.entrySet()) {
switch(entry.getKey()) {
case KEY1:
myProcessor.processStuffAboutKey1(entry.getValue());
break;
case KEY2:
myProcessor.processStuffAboutKey2(entry.getValue());
break;
case KEY3:
myProcessor.processStuffAboutKey3(entry.getValue());
break;
case KEY4:
myProcessor.processStuffAboutKey4(entry.getValue());
break;
...
...
}
}
}
So basically you can gather that it is necessary for me to invoke very different things for each key IF it is in the map. I have therefore already created the Processor
class and mind you this is already the absolute shortest and most compact that I was able to come up with. Basically this method does already do only one thing.
But isn't it possible to build this differently so that it is shorter? I currently have 25+ cases to handle
Edit: made clear what Key,Value
-Pair is and what they're used for
Map<Key, Value> map
but you're only using themap.keySet()
. Are you using theValue
s in theMap
for anything, or passing them into theProcessor
subroutines? \$\endgroup\$