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


Friday, March 09, 2007

The linux merit badge

When I started on linux, it was the black magic of the computing world. Novell hadn't bought SuSE, HP wasn't writing drivers, Dell hadn't honored its existence, and pretty much the only company actually doing anything with it was RedHat. Back in the days where Gnome 1.x and KDE 1.x reigned supreme, blackdown was the only way to get Java functional without heavy hacking, the 2.6 kernel had just recently gone stable and so many users were scared to upgrade, x86_64 wasn't even a publicly released concept on the hardware end of the spectrum much less the software world, PowerPC was still going strong over at the Apple Camp, WindowsXP was much anticipated by the Microsoft huggers, and when you said "I run linux" people automatically assumed you knew what you were talking about, and at the time there was a 95% chance that you really did.

Fast forward to today: Vista is released, Novell owns SuSE, RedHat offers Certifications, Canonical rushed in and took over the linux desktop market with throwing millions of USD at their Ubuntu distribution, HP writes native linux drivers for their printers and random peripherals, Dell is now offering linux on PCs, specialty companies are all over the place offering linux centric services and hardware, and any noob with a computer that is set to boot from cd-rom can run and, in most cases, install linux on their computer. Is this a good thing? Well, yes and no.

Yes:
More users means more support, which is a very good thing.

No:
I am annoyed with people who won't read documentation to learn things on their own, this up and coming generation of linux users wants to be spoon fed everything. When I started I was taught how to use man pages and learned that google was my best friend.

I recently started as a TA for one of my professors teaching an Abstract Data Types and programming algorithms class in Java at my University and there are two, only two, linux users in the class of roughly 25. That doesn't bother me so much, linux isn't for everyone and it still has an "under dog" aura about it, but when I started speaking to these linux using students (who run Ubuntu) I quickly realized they know nothing about linux. They don't realize that Gnome != "a version of linux", they were lost as soon as I opened a terminal window, and weren't familiar with even the trivial task of checking their screen resolution. I asked a few questions regarding the Operating System and simply got the reply,"I dunno, when I installed it just worked" and I thought to myself "That is horrible" but as the day went on I truly thought it through and realized that this is a mile stone for desktop linux. Yes, they are under informed but ask your grandmother what the difference between explorer.exe and iexplorer.exe on a Windows machine and you will receive a blank stare. What has happened is that the linux merit batch, as I like to call it, no longer certifies your knowledge of linux but simply your endorsement of the open source movement by being open minded enough to use something different and see what the "under dog" has to offer. Your level of involvement and further reputation you have earned along the way will prove your skill level.

Conclusion:
The linux merit badge has lost a little power behind its punch but at the cost of betterment for the movement as a whole. The GNU/Linux world stands to gain a lot from the fact that your "average Joe" can use it as a desktop system without flaws. Will linux ever take over the desktop market? I don't know, nobody can really know, but I think we are at least stepping in the right direction of strengthening our user base in numbers and as time goes on and curiosity is sparked I think the next generation of linux users will educate themselves out of desire, not necessity. So basically, the "Yes" outweighs the "No" and I think the "No" will work itself out with time.

/me