20 juin 2007

"Whitespaces are a waste of time - I swear"

As I see it, it seems that many programmers really think that white spaces use loads of processor power at compilation and maybe even more at runtime. That's the only way I can explain functions or methods definitions that contains so little white spaces. Which is quite a bad news if these functions or methods are several hundreds lines long - and you are trying to debug them.

Trust me, debugging is a pain. Even if you're a good software developer, you'll have a hard time debugging your own code - and the longer the elapsed time between the development and the discovery of the bug is, the harder it will be to correct it - and chances are that you'll have to debug code that you didn't write in the first place. And that's even a bigger pain, because even if you are used to your way of writing code, the guy in the next cubicle has its own way to think and to deal with problems (obviously, yours is better). He also has his own way to write the code, and you'll be the lucky guy of the week if his main idea was to ease the reading of his code.

Of course, most of the time, he's not that concerned about your forthcoming debugging sessions. He just doesn't care (that's probably because he does not write bugs). So he just spits his code in his own way (which appear to contradict every single piece of methodology that you take for granted (I mean, it's not "your own way" of doing things), and when you have to read it you realize that most of your time is spent in deciphering the code - and that just makes the understanding of his algorithm a lot harder.

Why does people find it so hard to write code which is easy to read? I don't even speak about function or variable names (although they are important, of course). I just deal with these damn white spaces - correct indentation, line breaks between logical blocks, and a correct use of the space (' ') character. Bad indentation is something I really have a hard time to understand: the editor does most of the job by itself, and most of the time you don't even need to press that stupid TAB key. Missing line breaks is also a big pain - don't they see that their stack of code doesn't look great when they write it? This is probably one of the first thing I learnt when I went to my CS school: "if your code doesn't look good, chances are that it is not good". And how do you see if your code looks good? Simply move from your seat, goes two of three meters away from your screen, and look at it. If all you see is a black blob with horizontal filaments surrounded by a white bacteria, then something is probably wrong. If, from this distance, the code looks neat and clean, with correctly delimited blocks, then it will probably be easier to decipher - and the debugging session will be less stressing.

Finally, let's talk about this dreaded space character.

Do you see the difference between:

for(int index=0;index<maxIndexValue&&loopContinue!=false;++index) 
{
   result=someFunction(variable,((maxIndexValue-index)*factor)+offset);
   if(result>=treshold)
     break;
}

And:

for (int index = 0; index < maxIndexValue && loopContinue[1]; ++index) 
{
   int parameter = ((maxIndexValue - index) * factor) + offset;
result = someFunction(variable, parameter);
if (result >= treshold) break; }

I do. Especially if you have hundreds of lines that looks like the first case. Conclusion: I strongly believe that a wise use of the space character really enhance the readability of the source code.

And that's because I hate spending time deciphering code because of these really minor issues. To be honest, I hate debugging code, so when I see code that isn't even readable, my angriness-o-meter goes far in the red, and my debugging session is twice longer.

People have to remember that they don't write code for the compiler. If this was the case, we wouldn't have object oriented languages, because the compiler is extremely happy when it doesn't do anything - so we'll still program in machine code (yeah! I don't even need an assembler! my keyboard has only one key, and it's labeled 1!). They write code for posterity - and for their coworkers.

Note

[1] I also still don't understand why people continue to test for the (in)equality to true or false on a bool value. In the C++ standard, bool are defined to be either true or false. If it's not false, then it's true. If it's not true, then it's false. In very rare cases, it can be FILE_NOT_FOUND, but if that happen, please change your programmer - either his batteries died or he has no brain.

Commentaires

1. Le lundi, juillet 2 2007, 16:08 par someonesomewhere

Emmanuel, this is so damn true dude...

At first I thought "whitespaces" were I dunno, some fancy thing in new programming languages or something (little misuderstanding of the title), but then I look at the example and I laugh my ass off.

I really hate the first example, it sucks!
I wish people would use whitespaces.... Im usually the guy in the second example, because I like things tidy. And in the same lines, I also hate to see code without proper alignment (i.e tabs) or really tight use (or not use at all!) of parenthesis/brackets:

if(something){do this;};

And the like.... disgusting :)

2. Le vendredi, octobre 26 2007, 16:46 par Eddy Luten

Hi Emmanuel!

Nice article. Even if whitespace took up preprocessing time, what would be the big deal? OK, so your code doesn't compile fast but at least you'll be shipping a product that's easier to debug. I've always been a fan of white spacing and clustering code.

Good post :)

Eddy

3. Le dimanche, décembre 30 2007, 12:44 par Garcon

Hey ! toujours agréablemen supris de lire des billets intéressantsv :) qu'est-ce que tu sous entendais dans cete parenthèse : that's probably because he does not write bugs ? je te souhaite une bonnne continuation !

Ajouter un commentaire

Les commentaires peuvent être formatés en utilisant une syntaxe wiki simplifiée.

Fil des commentaires de ce billet