I know switch statements are considered “evil” because they are very procedural code, they violate the Open-Closed Principle, etc etc. But every now and then I don’t see a need to do anything more than a switch statement because the complexity of the scenario does not need anything more, and the code’s maintainability is not going to be so adversely affected by it.
I had such a case today, when examining the “Status” value – an enum – that is found on the data structure returned by a web service call. Here’s the original code:
1: switch (newContainer.Status)
3: case ScannedAssetState.ConfirmAssetRemove:
5: ConfirmAssetRemove(newContainer.TagInfo, assetTag);
8: case ScannedAssetState.Error:
13: case ScannedAssetState.Success:
Though this code isn’t horrible, I do tend to hate the excessive syntax of switch statements. So… I wrote a simple fluent API to condense this code. Here’s the end result:
2: .Case(ScannedAssetState.ConfirmAssetRemove, () => ConfirmAssetRemove(newContainer.TagInfo, assetTag))
3: .Case(ScannedAssetState.Error, () => View.ShowError(newContainer.Errors.FirstOrDefault()))
4: .Case(ScannedAssetState.Success, () => RestartWithNewContainer(newContainer.TagInfo))
5: .Default(() => Init(newContainer.TagInfo));
And here’s the simple way I implemented it:
1: public static class Switch
3: public static Switch<T> On<T>(T value)
5: return new Switch<T>(value);
9: public class Switch<T>
11: private bool hasBeenHandled;
12: private readonly T value;
14: public Switch(T value)
16: this.value = value;
19: public Switch<T> Case(T comparisonValue, Action action)
21: if (AreEqual(value, comparisonValue))
23: hasBeenHandled = true;
26: return this;
29: public void Default(Action action)
31: if (!hasBeenHandled)
35: private bool AreEqual(T actualValue, T comparisonValue)
37: return Equals(actualValue, comparisonValue);
I’m sure the AreEqual method could use some work to make it more robust, but it satisfied my current need just fine.
Of course, this code is nothing ground-breaking and I’m sure plenty of people will tell me how much worse it is than the original, and how I should have / could have avoided a switch statement in the first place… Meh… It was a fun little exercise. We’ll see how long it actually lives in my code base.
Post Footer automatically generated by Add Post Footer Plugin for wordpress.