Tabs vs Spaces is a small battle in the greater Indentation War. This is a silly
debate, one of the little annoyances a programmer comes across once in a
while. We each have a preference and none of us understands how anyone can think
differently. It sounds meaningless and petty, but as soon as you start working
with other people's files, indentation gets all messed up, and version control
logs go insane, this stuff matters.
Superficially, everyone seems to agree: "It doesn't matter which, just pick one
already. The problem is only mixed environments." Some groups have already tried
to enforce one: Makefiles and YAML force you to indent with tabs. Python
strongly suggests you use 4 spaces.
I think this choice is important, clearly, that's the object of this post. I'm
not looking to force anyone to change their habits, simply taking a look at the
implications of each alternative. To a greater extent, this choice says a lot
about what kind of programmer you are.
Why tabs?
Isn't it obvious? Tabs aren't a character to print, as much as a markup language
to signal indentation to the screen. In this string, "name\taddress\tphone
number", I'm creating a CSV variant with the "indentation" character as a
separator. That adds syntactic meaning to the text. In that aspect, indenting
with spaces sounds like another terrible habit in another markup language.
The advantages of giving syntactic meaning to your code are plenty. The
immediate effects come from helping your text editor understand your text
better. Sure, most editors have some sort of macro that says:
"WHITESPACES=' \t\n\r'" or something. But that is limiting.
A text editor that understands your text, can come up with wonderful features
like IntelliJ or the amazing magic of emacs. It also allows for better Static
code analysis and other formal proofs.
I'm not saying that space indentation is necessarily an obstacle. But if you
think that all this is important, tabs is the obvious choice.
Why not tabs?
I blame text editors. In a perfect world, everyone would agree that tabs make
more sense. It's safe to assume that almost every keyboard around has a tab key,
yet tabs are still hectic, awkward and messy.
The options for configuring proper tab display aren't always well defined, often
too hidden under series of menus and clicks. emacs and vim are targeted at power
users, but what about nano or pico? What about those GUI editors that mask the
features to take advantage of this markup?
Empirically, I noticed that a lot of people feel more comfortable removing tabs
altogether, going as far as programming their tab keys to insert a number of
spaces instead. Their attitude seems to say: "Tabs is a feature I could do well
without. It's important that everyone should be able to participate, space
indentation is a safer hack".
Why does it matter?
"Safer hack". This is a heavy one. A lot of issues of the programming world can
be resumed to this: "Powerful" vs "Safe". I can think of plenty: garbage
collections, dynamic typing, functional programming, just to name a few. It
seems to me that this can be generalized to "Getting things right" vs "Getting
things done". On some level, these are the two main conflicting motivations for
programming.
I don't have an absolute solutions for the matter, just a personnal
preference. I'm not implying that Python prefers the "Safe" mindset, I'm only
saying that I prefer the use of tabs for the same reason the C programmer
prefers manual memory management, the Lisp hacker enjoys dynamic typing and the
Haskell scientist likes the functional purity. I like "Power", not "Safe".
Which do you prefer?
debate, one of the little annoyances a programmer comes across once in a
while. We each have a preference and none of us understands how anyone can think
differently. It sounds meaningless and petty, but as soon as you start working
with other people's files, indentation gets all messed up, and version control
logs go insane, this stuff matters.
Superficially, everyone seems to agree: "It doesn't matter which, just pick one
already. The problem is only mixed environments." Some groups have already tried
to enforce one: Makefiles and YAML force you to indent with tabs. Python
strongly suggests you use 4 spaces.
I think this choice is important, clearly, that's the object of this post. I'm
not looking to force anyone to change their habits, simply taking a look at the
implications of each alternative. To a greater extent, this choice says a lot
about what kind of programmer you are.
Why tabs?
Isn't it obvious? Tabs aren't a character to print, as much as a markup language
to signal indentation to the screen. In this string, "name\taddress\tphone
number", I'm creating a CSV variant with the "indentation" character as a
separator. That adds syntactic meaning to the text. In that aspect, indenting
with spaces sounds like another terrible habit in another markup language.
The advantages of giving syntactic meaning to your code are plenty. The
immediate effects come from helping your text editor understand your text
better. Sure, most editors have some sort of macro that says:
"WHITESPACES=' \t\n\r'" or something. But that is limiting.
A text editor that understands your text, can come up with wonderful features
like IntelliJ or the amazing magic of emacs. It also allows for better Static
code analysis and other formal proofs.
I'm not saying that space indentation is necessarily an obstacle. But if you
think that all this is important, tabs is the obvious choice.
Why not tabs?
I blame text editors. In a perfect world, everyone would agree that tabs make
more sense. It's safe to assume that almost every keyboard around has a tab key,
yet tabs are still hectic, awkward and messy.
The options for configuring proper tab display aren't always well defined, often
too hidden under series of menus and clicks. emacs and vim are targeted at power
users, but what about nano or pico? What about those GUI editors that mask the
features to take advantage of this markup?
Empirically, I noticed that a lot of people feel more comfortable removing tabs
altogether, going as far as programming their tab keys to insert a number of
spaces instead. Their attitude seems to say: "Tabs is a feature I could do well
without. It's important that everyone should be able to participate, space
indentation is a safer hack".
Why does it matter?
"Safer hack". This is a heavy one. A lot of issues of the programming world can
be resumed to this: "Powerful" vs "Safe". I can think of plenty: garbage
collections, dynamic typing, functional programming, just to name a few. It
seems to me that this can be generalized to "Getting things right" vs "Getting
things done". On some level, these are the two main conflicting motivations for
programming.
I don't have an absolute solutions for the matter, just a personnal
preference. I'm not implying that Python prefers the "Safe" mindset, I'm only
saying that I prefer the use of tabs for the same reason the C programmer
prefers manual memory management, the Lisp hacker enjoys dynamic typing and the
Haskell scientist likes the functional purity. I like "Power", not "Safe".
Which do you prefer?