Inert Detritus The Internet's dust bunnies

Posted
5 November 2008 @ 11pm

Tabs? Spaces? Yes.

Tabs ver­sus spaces is one of the life­long debates between pro­gram­mers. “tabs ver­sus spaces” has as many hits on Google as “vi ver­sus emacs”: it’s that big a deal. (VI 4 LYFE!)

Argu­ments on both sides are quite valid. Pro­po­nents of tabs defend the flex­i­bil­i­ty of defin­ing a \t char­ac­ter to what­ev­er width you desire. Pro­po­nents of spaces defend the con­sis­ten­cy of align­ment of mul­ti-line com­ments and mul­ti-line method calls or mes­sages, as are so com­mon in Objective‑C.

Before we go any fur­ther, I need you to leave one thing at the door: your sense of supe­ri­or­i­ty about code formatting.

“I’ve nev­er worked with code writ­ten or com­ment­ed how you showed it!”

You obvi­ous­ly work with some­one who shares your cod­ing style pref­er­ences. Con­grat­u­la­tions, you’ve nev­er worked on a team before.

“I’d imme­di­ate­ly refor­mat it to suit my taste! Damn their style, it’ll be writ­ten how I want it!”

I’d rather spend my time read­ing their com­ments and under­stand­ing what they’re doing with the code, not being angry at the way they wrote it. An SVN com­mit set that sim­ply turns tabs into spaces or vice ver­sa 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 com­ments at the end of the line, like so. This looks insane at first glance, but I’ve seen edi­tors and IDEs that enable it, and I’ve seen code com­ment­ed like this.

TabsSpaces.java


This style works well for the author, regard­less of his pref­er­ence for spaces (shown above) or tabs (show below, four spaces per tab as Text­Mate shows at the bottom):

TabsSpaces.java


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

TabsSpaces.java


What about Ollie Objective‑C? He loves his mul­ti­line meth­ods, because Objective‑C hasThisWay:ofBeing verbose:whenItComes toParameterNames:andValues.

Here’s a snip­pet from the source for CPU His­to­ry:

TabsSpaces.m

This is fan­tas­tic with spaces! What if I set Text­Mate to tabs, indent this a bit, and ask it to realign things with control‑q?

TabsSpaces.m

That did­n’t work. Now, if some­one views this code with tabs set to any­thing oth­er than four spaces, that whole block is misaligned:

TabsSpaces.m

The new rule for inden­ta­tion and align­ment: “use tabs for achiev­ing an inden­ta­tion lev­el, spaces for char­ac­ter align­ment with­in an inden­ta­tion level.”

This achieves the flex­i­bil­i­ty of let­ting indi­vid­u­als define their own width for a tab for how deep an indent lev­el trans­lates to in columns, while still pre­serv­ing align­ment between lines at a giv­en inden­tion level.

Edit­ed to add: @boredzo decid­ed to steal my thun­der and write a sim­i­lar post. I make the case for the scheme, he asks IDE and edi­tor devel­op­ers to Make It So. We’re a one-two combo.


3 Comments

Posted by
Mattias
6 November 2008 @ 1am

I was actu­al­ly about to write some­thing 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 fea­ture request over at Apple while you’re at it?


Posted by
Steven Fisher
6 November 2008 @ 7pm

“use tabs for achiev­ing an inden­ta­tion lev­el, spaces for char­ac­ter align­ment with­in an inden­ta­tion level”

This is EXACTLY what I used to do with C++, but I feel like I’m fight­ing my text edi­tor for every sin­gle line. When I saw Objective‑C align­ment with the colons, I just gave up completely.

It’s a sim­ple set of rules: Tabs are for indent­ing. Spaces are for align­ing. THEY ARE NOT THE SAME THING!

I hope this comes about soon.


Posted by
Marius Andersen
1 June 2009 @ 4am