The Default Case Dilemma
April 20, 2009 I have to confess something. In my last post, I talked a lot about good coding practices. Well, it seems I have a tendency to break one of my own rules. I was looking through some of my Memories of Hyrule code and I realized that I have a tendency to leave the default cases, in my switch statements, blank.
Here is an example of what I'm talking about.
case BLUE_POTION:
imgBluePotion->draw(3, 2);
break;
case RED_POTION:
imgRedPotion->draw(3, 2);
break;
case MAGIC_WAND:
imgMagicWand->draw(4, 2);
break;
default:
//Something terrible has happened.
break;
This is the tail-end of a method that draws the player's current inventory items on the screen. There is a different case for each item that is available in the game. So in the end, I'm left with a blank default case. I want to say that this isn't a big deal because I'm sure that the default case with never be reached. But can I really be 100% certain of that? No, of course not. So what can I do to fix this?
I've seen other programmers simply use their last case as their default case. I can see the logic in doing this, but I really don't like it. I think that if I'm going to draw something on the screen, it's going to be because it absolutely belongs there, not because nothing else belongs there.
My next thought was to handle this like any other error or exception. But I don't want to do that, because getting into that default case isn't necessarily something that would be fatal to the application or even something that would disturb the player. So I don't want to turn it into something that will.
I'll be honest. I'm a little stumped with this one. For the time being, it's still just a comment (although it's a little better than the one displayed above), but I still feel like it should be doing something. Perhaps this is a scenario where an empty control block really is the best way to go?
C++,
Code,
Memories of Hyrule,
Programming 






Reader Comments