Do you ever feel like (well, you should) these huge switch + case statements or too many ifs seem to be wrong? What if I told you, there’s one simple trick that will change your life, by getting rid of them? Ok, seriously – I have nothing against switch or if as the way of controlling the flow (I use them quite often) however, there are certain occasions at which the things could be done better. And let me show you another way to achieve the same goal which is much cleaner in terms of code readability and maintenance.





This post is all about coding, so let’s include some code in here. Here comes our “problem”:

enum Operation { NotSet = 0, Cancel = 1, Block = 2, Suspend = 3, Disable = 4, Remove = 5, Approve = 6, Activate = 7, Create = 8 } 1 2 3 4 5 6 7 8 9 10 11 12 enum Operation { NotSet = 0 , Cancel = 1 , Block = 2 , Suspend = 3 , Disable = 4 , Remove = 5 , Approve = 6 , Activate = 7 , Create = 8 }

This guy above surely may possess many more types, it could be also a magic string or anything else that makes sense (sorry, magic strings actually make no sense).

Anyway, you get the idea – there’s a variable, and based on its value that has to be checked first, we need to perform some action. BTW, before you start judging me – I’m aware that I don’t need to assign numerical values to the enums, but I like to be explicit about what I’m doing with my code, so I tend to follow this practice.

Back to the point, there’s a function that returns the type of operation that has to be dealt with:

public void ExecuteOperation() { var operation = ResolveOperation(); //Now do something with this operation, switch/if anyone? } private Operation ResolveOperation() => //return the operation based on the current state of the application. 1 2 3 4 5 6 7 public void ExecuteOperation ( ) { var operation = ResolveOperation ( ) ; //Now do something with this operation, switch/if anyone? } private Operation ResolveOperation ( ) = > //return the operation based on the current state of the application.

First solution – switch + case:

switch (operation) { case Operation.NotSet: DoSomething(); break; case Operation.Cancel: DoSomethingElse(); break; //And many more lines below. } 1 2 3 4 5 6 7 8 9 10 switch ( operation ) { case Operation . NotSet : DoSomething ( ) ; break ; case Operation . Cancel : DoSomethingElse ( ) ; break ; //And many more lines below. }

Could be simplified a little bit if we could just use return statement and get rid of break keyword, yet still, it doesn’t make much difference for many available options.

Second solution – what if:

if (operation == Operation.NotSet) { DoSomething(); return; } if (operation == Operation.Cancel) { DoSomethingElse(); return; } 1 2 3 4 5 6 7 8 9 10 if ( operation == Operation . NotSet ) { DoSomething ( ) ; return ; } if ( operation == Operation . Cancel ) { DoSomethingElse ( ) ; return ; }

Same problem for too many options, could be simplified as well if the return value was used, but in the end, it doesn’t matter that much.

Third and final solution – dictionary of mappings between operation and function:

static readonly IDictionary<Operation, Action> Operations = new Dictionary<Operation, Action> { [Operation.NotSet] = () => DoSomething(), [Operation.Cancel] = () => DoSomethingElse(), //Moar mappings. }; 1 2 3 4 5 6 static readonly IDictionary < Operation , Action > Operations = new Dictionary < Operation , Action > { [ Operation . NotSet ] = ( ) = > DoSomething ( ) , [ Operation . Cancel ] = ( ) = > DoSomethingElse ( ) , //Moar mappings. } ;

What did I do? Well, I’ve just created very simple mappings between the available operations and functions. These mappings can be much more sophisticated ones e.g. you may use Func in order to provide some input parameters or return a value.

And what happens now, if we want to perform an operation? This single line of code:

Operations[operation](); 1 Operations [ operation ] ( ) ;

Quite easy, isn’t it? And you can even split your mappings declarations within dictionary into separate functions if there’s too many of them. The general idea, though, is that instead of using old fashioned switch/if statements for controlling the flow (that might grow tremendously) you just say: “Hey, let me point this operation to this function, and just invoke it if there’s a match” and create a dictionary or any other suitable collection to achieve the goal.

I do realize that the presented solution ain’t nothing fresh or new and it’s been there for years, yet there’s still so much code written using this old C style, so I hope that at least some of you will find this to be useful.