Ramblings of Narc

When the issue isn't confused enough.

Whitespace, Indentation, And All That Other Fun Stuff

I want to comment a bit more on Coding Style, this time as it very much applies to what I like to see and what I don’t.

At one point, I played around with Python, and I remember liking it to some extent. But, surfing around the Web, I keep finding references to one particular feature of Python I don’t like: indentation-based scope. For the longest time, I put this down to mere preference (“it just looks better to me with the curly braces”), but thinking about it now, I realized: I like the curly braces. That is, I find them useful in the task of reading code, mostly because they introduce a bit of vertical mostly-white space, which adds to the scannability of the code.

Incidentally, I dislike Perl‘s “feature” of forcing you to put curly braces around single-line if() { /* code blocks */ }. In my mind, the code block is a part of the if, and there’s no reason to separate it with syntactic garbage. Now, I know Perl also offers the alternative syntax of putting the if part after the single line statement, and not requiring curly braces there, but reading “do(something) if(x is not null)” makes less sense to me than “if(x is not null) do(something)“. I assume that’s because of the peculiarities of the mental model I create in my mind, and like everything else I’m talking about in this article, this is a very specific “feature” of my own brain. Also, I can eventually read the former, too, it just requires an extra bit of mental effort.

What I also can’t stand are the semi-abbreviated code styles that urge you to put blocks’ opening braces on the same line as the if, while, or function definition that starts them. This harks back to my original preference of curly braces as whitespace — I’d rather see:

if(x is not null)
{
    x += 0;
    if(empty(x))
        return;

    do_something_with(x);
}

than something like:

if(x is not null) {
    x += 0;
    if(empty(x)) return;

    do_something_with(x);
}

And let’s not talk about “} else {“. That kind of thing should never have existed.

If you can read PHP, an example of my code is available on narc.ro, and was referenced earlier in this blog, as part of a discussion on recursion.

Finally, my preferred tab width is 4, and I prefer files that have been formatted using TAB (0×09) characters as opposed to multiple SPACE (0×20) characters. That’s because I can then read code written by people who prefer tab widths of 2, 3, or 8, or whatever else, and not care because the formatting will still be consistent. Which is also why I tend to turn on whitespace rendering when I edit, and correct to tab-indented whenever I can do so easily.

So what are your personal preferences, and what makes them work for you?


Add your comment

 

XHTML: You may use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>