Search
Enlightenment
  • Beginning iPhone 3 Development: Exploring the iPhone SDK
    Beginning iPhone 3 Development: Exploring the iPhone SDK
    by Dave Mark, Jeff LaMarche
  • Code Complete: A Practical Handbook of Software Construction
    Code Complete: A Practical Handbook of Software Construction
    by Steve McConnell
  • How to Break Software: A Practical Guide to Testing W/CD
    How to Break Software: A Practical Guide to Testing W/CD
    by James A. Whittaker
  • How Linux Works: What Every Superuser Should Know
    How Linux Works: What Every Superuser Should Know
    by Brian Ward
  • Rootkits: Subverting the Windows Kernel
    Rootkits: Subverting the Windows Kernel
    by Greg Hoglund, Jamie Butler
Blog Archives
Powered By

Want an awesome looking site like mine? Look no further than Squarespace.

Looking for a Web Host or a Domain Name registrar? Check out Bihira.

« Google FAIL! | Main | A Passion for Programming »
Monday
20Apr2009

The Default Case Dilemma

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?

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>