Tabs? Spaces? Yes.
Tabs versus spaces is one of the lifelong debates between programmers. “tabs versus spaces” has as many hits on Google as “vi versus emacs”: it’s that big a deal. (VI 4 LYFE!)
Arguments on both sides are quite valid. Proponents of tabs defend the flexibility of defining a \t character to whatever width you desire. Proponents of spaces defend the consistency of alignment of multi-line comments and multi-line method calls or messages, as are so common in Objective‑C.
Before we go any further, I need you to leave one thing at the door: your sense of superiority about code formatting.
“I’ve never worked with code written or commented how you showed it!”
You obviously work with someone who shares your coding style preferences. Congratulations, you’ve never worked on a team before.
“I’d immediately reformat it to suit my taste! Damn their style, it’ll be written how I want it!”
I’d rather spend my time reading their comments and understanding what they’re doing with the code, not being angry at the way they wrote it. An SVN commit set that simply turns tabs into spaces or vice versa is frowned upon by all who work on the codebase.
With that out of the way, let’s look at some examples.
Let’s say Joe Java put some comments at the end of the line, like so. This looks insane at first glance, but I’ve seen editors and IDEs that enable it, and I’ve seen code commented like this.
This style works well for the author, regardless of his preference for spaces (shown above) or tabs (show below, four spaces per tab as TextMate shows at the bottom):
What happens when I view it with a tab set to two spaces? Life becomes miserable:
What about Ollie Objective‑C? He loves his multiline methods, because Objective‑C hasThisWay:ofBeing verbose:whenItComes toParameterNames:andValues.
Here’s a snippet from the source for CPU History:
This is fantastic with spaces! What if I set TextMate to tabs, indent this a bit, and ask it to realign things with control‑q?
That didn’t work. Now, if someone views this code with tabs set to anything other than four spaces, that whole block is misaligned:
The new rule for indentation and alignment: “use tabs for achieving an indentation level, spaces for character alignment within an indentation level.”
This achieves the flexibility of letting individuals define their own width for a tab for how deep an indent level translates to in columns, while still preserving alignment between lines at a given indention level.
Edited to add: @boredzo decided to steal my thunder and write a similar post. I make the case for the scheme, he asks IDE and editor developers to Make It So. We’re a one-two combo.
3 Comments