“Use break in every switch case!” aka “The idiot in me.”

Today I experienced an Eureka! moment at work. I was working on some JavaScript code the whole week and didn’t understand why that dynamic stuff didn’t do what it should. I wanted to make some (yeah, boring .. HTML tags … sorry) multi-select list box where you can add your own options. I made buttons to select all options or default options or none. I just didn’t get those buttons to work. My code seemed correct and I just didn’t get it (note … because I used ActiveX-stuff I couldn’t really debug it in firebug…  and the Microsoft debugger  for IE wasn’t too helpful either).  Anyway… I thought ” hey .. if that multi-select list box won’t work I’ll program my own thing!” . Wrong, just wrong… 😉   .

I really made my very own nice multi-select   in JavaScript. But ..  the same thing happenend again. The same error. I thought I’m getting insane or something.  How could this be. Should I already arrange myself a room in an nursing home?

After several hours of searching help online and trying to find mistakes in the code I suddenly saw something.  In a function I had used the switch statement which you also have in other languages like C, Java or even in Visual Basic although it’s named “SELECT CASE … END SELECT there. And in the Basic-version you just have CASEs and only the stuff you put in a case will be executed. That makes sense.

A trivial example (yeah this doesn’t really make sense… I know that.) :

Visual Basic (I think, in VBScript it would work, too):


dim animal

animal = “dog”

Select Case animal

Case “dog” : MsgBox(“Dog.”)

Case “cat” : MsgBox(“Cat.”)

Case else : MsgBox(“Unknown pet.”)

End Select

-> the result will be a dialog bog with the text “Dog.” in it.

If we do this the same way in Javascript (or probably many other languages)…

JavaScript:

var animal = "dog";

switch(animal){

case “dog”: {

alert(“Dog”);

}

case “cat”:{

alert(“Cat”);

}

case default: {

alert(“Unknown pet.”);

}

}

What will happen here? First we get a dialog box saying “Dog.” then another dialog box saying “Cat.” and a third dialog box saying “Unknown pet.“.

Why is it like it is? Probably because you can construct stuff that will trigger several options with less code.  That’s if you want to do that. I wouldn’t want to do that. That it’d be very hard to read after a few days.

So what I have learned is…  use “break;” in every switch case! I don’t want to debug code for such a joke of bug anymore in the future.

The cool thing about such mistakes is… you most probably will remember them for a long time. And when it happens on a Friday you really have achieved something! Hooray.

I bet (and hope) everybody has their little bugs they had to invest massive amounts of time. The feeling of joy you get when you finally solve the problem correlates directly with the amount of time invested and is really great. Except when the problem inducing thing was something ultra small. Then you’d (or at least I’d) like to hit your (my) head on the table for several hours.

It’s nice to be an idiot when it’s only for a short moment.

Advertisements

One thought on ““Use break in every switch case!” aka “The idiot in me.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s