I have a game project at the Uni. We’re implementing it in Java. I’ve never done any real Java except for some Android and Hadoop playing around. But you know, I know the syntax… eh.
I needed a switch. I thought I really shouldn’t use a switch since every time I’ve used one there has been a bug somewhere in there. Either in the first implementation of the code or some later iteration when I’ve added or removed something. That’s the toughest case (pun intended!): you touch your working switch. There’s always been something that breaks and it has been hard to spot. So this in mind I opened Java reference and looked up the syntax. I glanced over it and paid some careful attention to the break statements. I implemented my switch and it worked. I added more cases and it didn’t. I spent ~10 minutes locating the bug. The bug wasn’t obviously connected to the switch and I actually was pretty sure it had something to do with another part of the code that was called after this new version of my switch. Then it hit me.
I had forgot the breaks.
I laughed and decided to write this. Really. I opened the Java reference thinking that I really really shouldn’t use a switch and examined the examples carefully not to miss how the breaks and defaults etc. should work. And I forogot the breaks.
Don’t use a switch.