Friday, April 17, 2009

On tabs vs spaces

If I understand the debate correctly, the problem of tabs vs spaces arise only when there are both in the same file:

1 if (a_condition &&
2     another_condition)
3 {
4     do_something(variable1,
5                  variable2,
6                  variable3)
7 }

The indentation doesn't really matter. It works either way. But the alignment would break in spectacular fashion if we aligned lines 2, 5 and 6 with tabs.

So always tabbing won't work.

People like to be unique, and everyone has their own favorite tabstop settings. I've seen different co-workers use 2, 4 and 8, for example. Tabs make the files automatically look nicely indented to you in your favorite editor(tm). Spaces make it look nice only to some people (doesn't that remind you of the three bears fairy tale?). Also, having a key insert not only a different character than it states, but also several of them, is weird. So pure spaces don't quite work either.

No editor I'm familiar with can smartly figure out what portion of the leading space is supposed to be tabs and what is supposed to be spaces AND do the conversion automatically when you press tab and/or spam the spacebar. And we're assuming we can't rely on the programmer to remember to check (a fair assumption imho).

So tabs for indentation and spaces for alignment doesn't work either.

Solution?

Well, if there was one, we'd all be using it. What if we just avoid aligning things altogether?

//with alignment
doSomething(var1
            var2
            var3)

//indented
doSomething(
  var1,
  var2,
  var3
)

//with alignment
if (foo == true &&
    bar == true &&
    baz == false)
{
  //bla
}

//refactored
var isNameValid = foo == true
var isEmailValid = bar == true
var isDateValid = baz == false
if (isNameValid && isEmailValid && isDateValid) {
  //bla
}

Then again, that's javascript. Probably doesn't work so well for C or lisp.

But the question is: why do people align stuff, rather than indent it? I can see that it'd look ugly in the case of the if statement, but time and time again I've seen huge parameters lists for some OpenGL call being aligned to some random column for no particular reason when they could just break the line after the opening parens and indent the parameters (and they'd even fit in the screen!).

For things like huge if statements, I find that the refactored pattern above is nice especially when the code is dense (e.g. when expressions are queries to data structures, like in LINQ or Python comprehensions), since the variable name describes what information is being pulled out of the data structure.

Back to the topic

Anyways, coding patterns aside, I still don't know what's a good way to tie spatial code organization more closely to the abstract syntax tree (and in the case of alignment, to the metagroups of data) that provides a consistent coding style convention that works everywhere and for everyone.

Is bickering about which ASCII character is better the best we can do?

1 comment:

  1. “No editor I’m familiar with can smartly figure out what portion of the leading space is supposed to be tabs and what is supposed to be spaces and do the conversion automatically when you press tab and/or spam the spacebar.”

    Emacs can do it. As can Vim. It’s trivial to implement if the editor already knows how to auto-indent.

    ReplyDelete