Friday, August 31, 2007

Why programming sucks



This is a screenshot of something hideously funny we did to Razvan (not his real name) during fourth year. This is how geeks get their kicks. And now that it's about five years later, the statute of limitations has expired so I can freely admit to having done this. One day Razvan asks me for a hashtable implementation. Sure, I said. Have this hashtable, I said. While I was preparing this hashtable for Razvan, Brad (a friend in CS with a decent sense of humor) and I hit upon a brilliant idea. This picture is kind of blurry but it shows what we did. Look very closely at the second line. Can you make it out? It says:
#define if while
This clandestine directive, inserted into the code we gave Razvan, was the source (pun not intended) of a huge amount of grief. Programmers will immediately recognize the nightmare world into which we have plunged this unwitting little hashtable. But the rest of you must be wondering, what does this mean?

Well, let's start with the basics of programming. Flow control is how you get the computer to do things conditionally. This is what the if keyword does. Think of it like this.
if (raining) {
bring(umbrella);
}
Unless you're abysmally retarded you probably got the gist of that. Loops are similar to flow control and work like this.
while (money > 0) {
spend(money);
}
Easy, right? Now the #define keyword. This is the same as doing a find-and-replace in a word processor. Check it out, y'all. If I put the lines
#define if while
if (raining) {
bring(umbrella);
}
in a program, the #define line tells the computer, "any time you see 'if,' replace it with a 'while.' " And the computer will do that because computers are stupid. So what you've essentially written above is
while (raining) {
bring(umbrella);
}
which is utter nonsense: you're going to keep bringing more and more umbrellas with you until die of umbrella poisoning. You're just going to keep stocking up on umbrellas until it stops raining and accomplish nothing else.

Well basically, this kind of logical fallacy is exactly what we introduced in Razvan's shiny new hashtable. Any time the program we gave him tried to do an if statement, it would spin off into an infinite loop that would just keep chewing up processor power until you had to restart Windows 2000. And all because of that one line. If you were to remove that one line, it would work perfectly. Absolutely perfectly. And that is why it was so funny. It's also incredibly hard to detect, because when you debug you never expect anyone to put in a bug intentionally. Poor Razvan.

Of course, this implementation, as it is, doesn't work completely. Anything with an else clause won't compile (although it will throw up some very confusing error messages). You'd have to add the line
#define else if(0)
or something in order to cover all your bases and get all programs to compile. Or you could do
#define if(x) if(!(x))
(the expansion is not recursive). I'll leave it up to you to determine what kind of havoc that wreaks. Non-programmers:
!x
means “not x,” or the boolean negation of x.

This is just one example of things geeks do instead of talking to girls.

3 comments:

razvan said...

ffs :P
not his real name.
i never asked anyone for anything. i took a break, and someone (let's call him Brad K... wait, that's too obvious, let's call him B. Kimmel) did a yuhongesque prank.
it caused me no grief 'cause i debugged it and saw it. :P

lisseut said...

Ha ha!

Oh "Razvan", there is no shame in being pranked.

Jen
:o)

razvan said...

yeah, that was a great prank.
but you make me look incompetent by putting all that stuff about hash tables... :P