c# - How to refactor such a big IF-ELSE statement -
i have such big if-else statement. how can refactor it? can give example how make more pretty? thanks!
public enum firestate { none, nonetominor, minor, minortonone, minorrecovery, minortomajor, major, majortonone, majorrecovery } private static firestate getfirestate(int previousrespincount, int currentrespincount, bool isrecovery) { firestate fire = firestate.none; if ((isrecovery || previousrespincount == 0) && currentrespincount < minorjackpotminrange) { fire = firestate.none; } else if (previousrespincount == 0 && (currentrespincount >= minorjackpotminrange && currentrespincount < minorjackpotmaxrange)) { fire = firestate.minorrecovery; } else if (isrecovery && currentrespincount >= minorjackpotmaxrange) { fire = firestate.majorrecovery; } else if (previousrespincount == (minorjackpotminrange - 1) && currentrespincount == minorjackpotminrange) { fire = firestate.nonetominor; } else if (previousrespincount >= minorjackpotminrange && (currentrespincount > 0 && currentrespincount < minorjackpotmaxrange)) { fire = firestate.minor; } else if ((previousrespincount >= minorjackpotminrange && previousrespincount < minorjackpotmaxrange) && currentrespincount == 0) { fire = firestate.minortonone; } else if (previousrespincount == (minorjackpotmaxrange - 1) && currentrespincount == minorjackpotmaxrange) { fire = firestate.minortomajor; } else if (previousrespincount >= minorjackpotmaxrange && currentrespincount >= minorjackpotmaxrange) { fire = firestate.major; } else if (previousrespincount >= minorjackpotmaxrange && currentrespincount == 0) { fire = firestate.majortonone; } return fire; }
you start writing unit tests; , use coverage come point sure understand paths through method. start refactor it, using known methods so; , tests tell you don't break while going forward.
finally, throw away of this; , restart tdd, , focus come reasonable design. in case: implement state machine ... using polymorphism; instead of cascaded if/else statements.
long story short: choices coverage-based refactoring; or if time allows, re-design whole component; current implementation state machine in poor disguise.
Comments
Post a Comment