Thursday, March 15, 2007

The case of the missing switch statement

For all my C/C++ and Java programmers out there who have some to know and love the reserved word switch would find it rather interesting to venture in the direction of a language that lacks such a statement, just as I had. I have recently divulged into the realm of what I will call "open source at its finest" by learning the Python programming language. Python is a very verbose, flexible, powerful, and object oriented interpreted programming language (details, if you want/need them, here) that I have grown to love over the past month in my adventures of learning its ways, but I recently stumbled across something that I originally thought to be an oddity: the lack of a "switch" or a "case" statement. I later discussed it with a friend of mine who simply said,"why do you need a switch statement?" and I really couldn't find a solid answer. He later went on to explain how a switch statement is simply a way to make a complex/nested if statement faster because when it compiles there is just a static jmp (for those of us sadly stuck on a x86 machine) statement in the assembly to where it needs to be instead of multiple compare operations. Thus, since python is interpreted, it would never reap the benefits of this optimization.

Though, if you are simply stuck on switch statements you can take the following Java code:

int x;
//read in a value in some form or fashion and assign it to x
switch (x) {
case 1: this.someFunction(); break;
case 2: this.someOtherFunction(); break;
}

To the following python code (Note: the python code is utilizing the power of the "dictionary" data type that is part of the language)

self.x = #read in a value in some form or fashion to assign to x
mySwitch = {
1: self.someFunction,
2: self.someOtherFunction
}
callFunct = mySwitch.get(x)
callFunct();

#Neither of these code examples have been compiled or run respectively, just coded off the top of my head ... so if there is a slight syntax error, sorry :)

So... it can "be done" but what advantage does that code have over this python code?:

if x == 1
self.someFunction()
elif x==2
self.someOtherFunction()

Well, that honestly depends on who you ask and in what respect you are asking it but for all practical purposes there really isn't an advantage or disadvantage either way its just mainly stylistic preference.

Why did I write this?
Well, I am bored ... it is spring break and this was one of the best ways I could think of to kill time and procrastinate from actually writing the lexical analyzer and parser for my compiler theory class. And yes, I am writing a compiler in python ... why? because I think it will be funny.

/me


1 comment:

Unknown said...

It is totally achievable that if you're truly using these tablets you can expect to observe no main difference in dell battery inspiron 1545performance, though just one seems speedier on paper. We will know far more when we have the possibility to run some in-depth tests on the Kindle Fireplace High definition,dell battery inspiron 1525 and check out irrespective of whether it might contend with the quite strong Nexus 7.