Tabs Are Evil, Part 3

For part 2, see here.

We've got a new forum set up for the Java Posse. There's quite a bit of activity there already. One entry in this thread argues once again that there is nothing wrong with tabs and that they are a good way to express indentation. (We keep mentioning that Tabs Are Bad on the show). I've gotten similar feedback from my previous anti-tab blog entries.

A couple of weeks ago I was taking a look at the findbugs source code, and here's what I saw:

Findbugs follows a coding style where it uses tabs for indentation. In the above you can see that the tabs are really standing out since I have them highlighted with the NetBeans fixtabs module.

The thing to notice is that there are lines where spaces are (probably accidentally) used for indentation. For example, line 2, the beginning of the method signature. And also on line 3, where there is first a tab, then eight spaces, to indent the throws clause.

If this source file is interpreted with tabs expanded to anything other than 8, the code will not be aligned properly. And this is precisely what is problematic about Tabs. Tabs, in most editors, are visually indistinguishable from spaces. Thus, when you're editing, you can't tell that you've accidentally just indented a line with spaces. Obviously, developers might do this by accident, since they already hit the spacebar to insert whitespace between symbols. You can't have the IDE automatically insert tabs instead of spaces. But you can certainly do the opposite. And if you do, you'll avoid problems like these.

Comments:

That reminds me: when I was working in the Swing Team, code reviews would reject any change that would include tabs in the source code :)

Posted by Romain Guy on September 18, 2006 at 10:34 PM PDT #

So why not configure your editor to show tabs in some way, the same way you can display non-printable characters in any decent word processor?

Posted by Jasper Bryant-Greene on September 19, 2006 at 05:18 AM PDT #

Given your example you could argue that the only line with incorrect indentation is the line which uses no tabs, i.e. line 2 (the beginning of the method sig). Line 3 may have a mixture of tabs and spaces but the tabs are being used to signify the indent level of the code, on top of which spaces are used for formatting (aligning the throws keyword with the return type of the method in this case, which just happens to be 8 spaces).

The advantage of this approach is that it doesn't confuse code indents with formatting (pretty-fying) and thus allows other developers to use any tab width they prefer, without corruption. In your example the only line which would appear to have the wrong indent if you were to alter the tab width is line 2, line 3 would still look cool.

Now I'm not saying I prefer tabs over spaces but I did used to work this way since it allowed everyone to choose the tab width they preferred, without corruption. I eventually submitted to using spaces only because other developers used that approach and they don't easily interoperate.

Posted by Mark Hatton on September 20, 2006 at 12:12 AM PDT #

You could argue that the only line in your example with incorrect indentation is the line which uses only spaces, i.e. line 2. Line 3 may have a mixture of tabs and spaces but the tabs are being used to signify the indent level of the code and spaces are used for formatting (aligning the throws keyword with the method return type in this case).

The advantage of this approach is that it doesn't confuse code indents with formatting and thus allows other developers to use any tab width they prefer. In your example the only line which would appear to have the wrong indent if you were to alter the tab width is line 2, line 3 would still look cool.

Though I don't use this approach myself.

Posted by Mark Hatton on September 20, 2006 at 12:21 AM PDT #

(continued from prev. comment, apparently I was spamming?!?) I'm not saying I prefer tabs over spaces but the mixed approach does allow everyone to choose a tab width they prefer.

I submitted to using spaces over tabs long ago though because other developers use that approach and the two don't easily interoperate.

Posted by Mark Hatton on September 20, 2006 at 12:24 AM PDT #

Why is just about everything I post being flagged as spam ?! Grumble.

Posted by Mark Hatton on September 20, 2006 at 12:27 AM PDT #

JWZ on tabs vs spaces is well worth a read: http://www.jwz.org/doc/tabs-vs-spaces.html

Posted by Joe Walker on September 20, 2006 at 06:59 AM PDT #

I think the main argument the pro-spaces lobby put up is that tabs break the layout of code when viewed on editors with different tab settings.

The problem I have with spaces is that they freeze dry the editor's tab settings into the file. Far too often I've seen source code written by more than one coder (strangely, more scripting languages like PHP than compiled languages like Java) in which some of the code has indents of three spaces, some four, six, eight, etc. EVEN WITHIN THE SAME FUNCTION!

"Aha", say the proponents of spaces, "but if everyone agreed to use the same tab setting, that wouldn't happen!"

"Aha", say the proponents of tabs, "but if everyone has agreed on a universal tab setting, we might as well use tabs!"

Posted by Simon Morris on September 20, 2006 at 10:46 PM PDT #

I don't think those two scenarios are equivalent. If people are using tabs, its pretty easy to -accidentally- insert spaces to indent. If people are using spaces, they are pretty much -intentionally- doing their own indent levels. One of the most important rules about good coding practice is to follow the coding style that is already present in the file you are editing. Doing your own indent level, especially within a method, is either very lazy or very rude.

Posted by Tor Norbye on September 20, 2006 at 11:55 PM PDT #

In the real world accidentally inserting stray spaces into a tabbed document is a rare event. As rare as accidentally inserting stray tabs into a spaced document. Generally people don't mistake the spacebar (the long thin thing across the bottom of the keyboard) for the tab key (the stubby thing usually in the northwest corner.)

Of course, the assumption is that those who use spaces take more care in their code layout (...while those who use tabs are just banging the stuff down any-which-way???) The argument about 'intentionally' doing indentation would be a valid one, if it wasn't for the small matter that the majority of programmers (spacers and tabbers) use an auto-indent feature which does 95% of the work for them. ;)

I think the real reason why tabbers use tabs is because they just so hate the slowness of wading through the treacle of longhand whitespace when navigating a document and deleting lines (particularly 'blank' lines.)

I agree about following the conventions laid down in the source code (but surely that should include leaving code with tabs in intact? :) But... in the real world coders just jump in and hack a file, and don't want to be constantly switching and swapping their editor settings - so you do end up with conflicting indents all over the place. All the wishful thinking in the world won't solve the problem that people are lazy, and a good solution has to cater for that fact.

In my mind both tabs and spaces have an equal number of issues. They are both broken. For existing code I'll happily follow whatever conventions are in place - but if given a option I'll go for tabs, if only because I hate the editing 'speed bumps' of longhand whitespace.

(Not worth fighting a war over, though! :)

Posted by Simon Morris on September 21, 2006 at 01:29 AM PDT #

I have no oppinion about TAB vs. spaces. I'm working for a company at the moment witch has an ant script which will format your code according to theyr coding style. So I just code someway and the ant script generates the approved version of my code. White spaces and all these silly things you wine about can be fixed by running my code-style ant and then before commit company code style.

Posted by Birkir on September 22, 2006 at 03:32 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today