I can’t explain how much I hate the switch statement. I often do everything and anything to avoid using it. I just think that it makes code very very ugly. Moreover, the amount of times I have seen people using it when they only have two cases, you just wouldn’t believe me! Let’s look at an example in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch (n) { case 0: System.out.println("I'm zero"); break; case 1: case 3: case 5: System.out.println("I'm odd"); break; case 2: case 4: System.out.println("I'm even"); } |
Surely no-one can tell me this actually looks “nice”? I mean coding is all about looking nice and neat after all. You can give the compiler something that looks like dog meat and it won’t give a shit as long as it’s valid syntax. Code is ALL about us humans reading it. The above is also a simple example and switch statement are normally way more complicated than this.
The normal argument for using a switch is that it looks better than loads of if-then-elses, which I completely agree with as that looks ugly as well. Also, with Java (and others) you can only switch on integer values which is the height of annoyance. Furthermore, there can’t be a single person that has not suffered the “forgetting the break” problem on a switch statement. For the record, my main bugbear is the extra indentation the switch forces on me along with the fact that scrolling all the cases is frequently a nightmare.
So what am I proposing? Well what I tend to use to solve both the ugliness problem and the lack of support for switching on stuff other than an integer is a HashMap. I’m sure some are screaming “Am I hearing this idot correctly”!
I really do think this looks particularly nice when used in code and can reduce a massive sprawing switch statement in a method to one simple call. So what does this entail. First lets assume that we have a method that takes a string as an argument and depending on the contents of this string we perform a specific operation – this is a particularly common task. Let say we have a class like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Operations { // Operation is an interface with the method do attached HashMap<String, Operation> mapper; public Operations() { mapper.put("eyeSurgery", new EyeSurgery()); mapper.put("heartSurgery", new HeartSurgery()); mapper.put("heartKeyHoleSurgery", new HeartSurgery()); // etc } /** * Carries out an operation, which is dependant on the value of op * and returns a string containing the results of this operation. */ public String do(OpContext op) { return mapper.get(op.toString()).run(op); } } |
Now surely the code in method do
looks better than a switch statement would? Obviously in this case you couldn’t even use a switch statement in Java, but even if the type of the HashMap key was Integer, I still proclaim this looks better. OK, you have the extra setup cost but it’s got to be worth it and should run fast enough for most needs. It also opens up the possibility of “switching” on any object based type rather than just an integer – presuming a sensible hash function being declared for your object. Obviously this is a simple example but hopefully it illustrates what I’m try to put across.
I’m not sure if this was really obvious and everyone is doing this already. Hopefully someone will get something out of this. So, down with the switch statement, long live the hash map!