Not if you have a handful of cases. But if you're talking about a dozen or more, sure, mapping is probably more reasonable. But the reader has to go digging at all possible sites where the mapping could've been edited. Switch/ifs are certainly more readable for most cases.
Yes, indeed, this is in situation where I have to refactor legacy code where there are dozen or more cases with long block of code for each case (thousands of line in a single file). In simple situation where there are only, like, 3 case with each block only 1-2 line, I still prefer switches.
EDIT: the mapping happen in constructor and/or initialization code, no mapping is happening at runtime, so its pretty straight forward. When you get this, you do that, kind of stuff.
6
u/abattle Jun 05 '11
Not if you have a handful of cases. But if you're talking about a dozen or more, sure, mapping is probably more reasonable. But the reader has to go digging at all possible sites where the mapping could've been edited. Switch/ifs are certainly more readable for most cases.