Best Practices

Refactoring Switches to Classes

John Sonmez · Feb 21, 2012 · 3 min read

I’ve talked about refactoring switch statements several times before.\n\n

\n\nI’ve even created a defaultable dictionary for refactoring a switch statement into a dictionary of actions.\n\nThis time, I am going to talk about refactoring switches when you have switch statements operating on the same set of data, but have different actions in different circumstances.\n\nswitch\n\n

\n\n

First let’s recap

\n\nWhen I talked about refactoring switches before, we were mainly dealing with a single switch statement somewhere in code.\n\nIn the case where you have only a single switch statement, or multiple switch statements that do the same thing based on the data, using a dictionary is still a great way to go.\n\nHowever, there are going to be circumstances where you are going to be switching on the same data, but in different contexts.  In these cases, you will want to perform different actions.\n\nLet’s look at an example.\n\n

\n\n\n\n

\n\nIn this example, we are switching on the same enumeration, but we are doing it in different locations of the code.\n\nUsing a dictionary would not work well here because we would need multiple dictionaries.\n\nWe still don’t want to leave this as it is though, because the code is pretty messy and fragile.\n\nMage\n\n

Separation of concerns

\n\nThe problem is the code that contains these switch statements has too much responsibility.  It is being asked to handle logic for each one of our character class types.\n\nWhat we need to do to improve this code is refactor the enumerations into their own classes.  Each switch statement will become a method that will be implemented by our enumeration based class.\n\nIf we are using Java, we can use Java’s enumeration implementation that allows for methods on an enumeration.  If we are using a language like C#, we still have to map the enumeration value to each class.\n\nLet’s start by making our classes.\n\nFirst we need a base class, or interface.\n\n

\n\n\n\n

\n\nNow we can create classes that implement this interface, that contain the logic that was in each switch statement.\n\n

\n\n\n\n

\n\nNext we can map our enumeration to our class.\n\n

\n\n\n\n

\n\nWe could also get rid of the enumeration if we wanted, and just create the appropriate class.  It will depend on what your existing code looks like.\n\n

No more switches!

\n\nNow let’s take a look at what we end up with in the two locations where we had switches.\n\n

\n\n\n\n

\n\nIf we want to add a new character class type, we just add a new class that implements the CharacterClass interface and put a mapping in our dictionary, or in our character initialization code.\n\nIf we end up having other places in our logic where different character class types should have different behavior, we just add a method to our CharacterClass interface and implement it in any classes that implement CharacterClass.\n\nOur code is much more maintainable, and easier to understand.

John Sonmez

John Sonmez

John Sonmez is the founder of Simple Programmer, author of "The Complete Software Developer's Career Guide" and "Soft Skills: The Software Developer's Life Manual." He helps software developers build remarkable careers.