Monday September 18, 2006 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.
(2006-09-18 20:45:50.0) Permalink Comments [11]
Posted by Romain Guy on September 19, 2006 at 05:34 AM PDT #
Posted by Jasper Bryant-Greene on September 19, 2006 at 12:18 PM PDT #
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 07:12 AM PDT #
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 07:21 AM PDT #
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 07:24 AM PDT #
Posted by Mark Hatton on September 20, 2006 at 07:27 AM PDT #
Posted by Joe Walker on September 20, 2006 at 01:59 PM 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 21, 2006 at 05:46 AM PDT #
Posted by Tor Norbye on September 21, 2006 at 06:55 AM 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 08:29 AM PDT #
Posted by Birkir on September 22, 2006 at 10:32 AM PDT #