Inert Detritus The Internet's dust bunnies

Posted
5 November 2008 @ 11pm

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.

TabsSpaces.java


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):

TabsSpaces.java


What happens when I view it with a tab set to two spaces? Life becomes miserable:

TabsSpaces.java


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:

TabsSpaces.m

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?

TabsSpaces.m

That didn’t work. Now, if someone views this code with tabs set to anything other than four spaces, that whole block is misaligned:

TabsSpaces.m

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

Posted by
Mattias
6 November 2008 @ 1am

I was actually about to write something like this myself a few days ago, but haven’t had the time to do it yet. I guess I’ll just write a short post and link to you and Peter. And why not file a feature request over at Apple while you’re at it?


Posted by
Steven Fisher
6 November 2008 @ 7pm

“use tabs for achieving an indentation level, spaces for character alignment within an indentation level”

This is EXACTLY what I used to do with C++, but I feel like I’m fighting my text editor for every single line. When I saw Objective‑C alignment with the colons, I just gave up completely.

It’s a simple set of rules: Tabs are for indenting. Spaces are for aligning. THEY ARE NOT THE SAME THING!

I hope this comes about soon.


Posted by
Marius Andersen
1 June 2009 @ 4am