Monday, September 18, 2006

Tabs Are Evil, Part 3

WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.


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.


9 comments:

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

    ReplyDelete
  2. 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?

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. (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.

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

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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.

    ReplyDelete