Why Tabs Suck

Posted by Andrew on August 05, 2008

I’m quite often receive a puzzled look when I explain to .NET coders that, like Ruby developers and others, I prefer spaces (2 to be precise) over tabs. If you’re already a space aficionado you may go about your business, otherwise read on.

First of all, I don’t have a problem with the tab character per se. My problem is that it’s all too easy to misuse it. Observe:

This code, taken from an open source project that mandates tabs, illustrates the problem - The author has mistakenly applied a liberal sprinkling of spaces in with the tabs:

Of course, the problem manifests itself because my indent level is different to that of the author. And, to be honest, I don’t think I’ve ever seen a tab-indented project that didn’t suffer from this problem.

Spaces, on the other hand, are atomic and consistent - They always render the same way.

So which indent level? Studies have shown that 2 to 4 is the sweet spot:

Although blocking style made no difference, the level of indentation had a significant effect on program comprehension. (2-4 spaces had the highest mean score for program comprehension.) We recommend that a moderate level of indentation be used to increase program comprehension and user satisfaction.

Personally, I prefer 2 spaces because it’s no less readable but uses less horizontal real estate.

kick it on DotNetKicks.com

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. SergeyNo Gravatar Tue, 05 Aug 2008 05:40:17 CDT

    Hi, Andrew

    Holy war.

    I mostly agree with your arguments,
    though tab == 1 key press, space == 2.
    If you need a three levels of indentation you have to press space bar 6 times! :)

    In real life I prefer simple rule - it’s doesn’t matter space or tab, but _all_ teammates must use the same one.

    Cheers,

    Sergey

    PS
    moving cursor throw spaces are also ugly :)

  2. SergeyNo Gravatar Tue, 05 Aug 2008 05:42:52 CDT

    oops

    moving _through_ not moving throw :)

  3. AndrewNo Gravatar Tue, 05 Aug 2008 06:00:49 CDT

    Hi Sergey,

    Most good editors allow you to deal with these gripes. i.e. In VS hitting Tab can insert spaces and, for the most part, indentation happens automatically. ReSharper is great for keyboard navigation.

    Cheers,

    Andrew.

  4. Sergey MirvodaNo Gravatar Tue, 05 Aug 2008 06:17:31 CDT

    Hi Andrew,

    Yes. All these tools are great and we are using ReSharper since version 1.0, but all that magic incline towards to different problems like with indentation in Brail. I think It is a result of misconfigured Resharper’s Code Clean Up feature.

    Sergey

  5. MartinNo Gravatar Tue, 05 Aug 2008 15:44:01 CDT

    Personally I believe TABs are better as long as everyone is inconsistant.

    Firstly, TAB = 1 key press, SPACE = X key presses.

    More importantly, each developer can configure how wide a TAB is in their editor.

    For instance, developer A likes seeing indents at 4 spaces wide and developer B likes seeing indents at 2 spaces wide. If everyone uses TABs then there is no issues as each developer sees the code with the indents sized to their own preference.

    Using SPACEs fixes the idents to the size the last developer set them to and so is bound to look ugly to someone.

    TABs just add the flexibility of easily adjustable TAB widths without resorting to reformatting the code.

  6. AndrewNo Gravatar Tue, 05 Aug 2008 17:29:08 CDT

    > Personally I believe TABs are better as long as everyone is inconsistant.

    LOL, presume you mean consistent. Yes, but this is hard as illustrated by my example from a project staffed by ostensibly very good developers.

    > Firstly, TAB = 1 key press, SPACE = X key presses.

    As mentioned above, when you hit the Tab key spaces can be inserted. Almost every editor except Notepad has this feature.

  7. CharlieNo Gravatar Tue, 05 Aug 2008 20:03:11 CDT

    Fights between the tab people and space people on my team got so bad that management decided that neither was allowed anymore.

    Then when productivity slowed over arguments about whether or not to have blank lines between methods, we got rid of the hard return as well.

    Sure, we do a lot of horizontal scrolling now and the breakpoint is pretty much useless, but at least we’re not arguing anymore.

    Plus we were able to cut the file size of our code by almost 8%! The only time we’ve seen a greater decrease is when we put a three character max on variable, class and method names.

  8. MartinNo Gravatar Tue, 05 Aug 2008 20:41:58 CDT

    Haha… Yes I do believe I meant “consistant” :)

    > Firstly, TAB = 1 key press, SPACE = X key presses.

    My point was meant to be that there are X number of SPACEs inserted to create the indentation.

    Isn’t that what TABs are for?

  9. AndrewNo Gravatar Tue, 05 Aug 2008 21:31:36 CDT

    @Charlie,

    Surely a team such as yours wouldn’t need “the breakpoint” anyway?

  10. TomNo Gravatar Thu, 07 Aug 2008 06:48:23 CDT

    Your argument is flawed. You suggest that because the open source project mandates tabs that you get the above mess. If they had mandated 2 spaces instead, you’d still get that mess.

    But think of the disk space you save with tabs, if you use 2 spaces then it would be a 50% savings. If you use 4 spaces, it would be a 75% savings :-)

  11. AndrewNo Gravatar Thu, 07 Aug 2008 07:11:34 CDT

    @Tom,

    Please explain how would you still get that mess?

    I’ll assume your second argument is a joke :-)

    Cheers,

    Andrew.

  12. TomNo Gravatar Thu, 07 Aug 2008 07:17:21 CDT

    Above the developer added spaces when they should have been using tabs. But the converse it also true, they would most likely add tabs when they should be using spaces. Hence, it’s not the tabs that suck, it’s the developer ;-)

    I do use tabs, but don’t really have anything against spaces. I just like consistency.

    (Yeah, I’m not that concerned about disk space ;-)

  13. AndrewNo Gravatar Thu, 07 Aug 2008 08:07:28 CDT

    @Tom,

    As alluded to in the comments above, you simply configure your editor so that pressing tab inserts spaces. In Visual Studio this is the “insert spaces” option.

    Cheers,

    Andrew.

  14. MartinNo Gravatar Fri, 08 Aug 2008 00:40:08 CDT

    Problem seems to really be about consistancy.

    So you should either always use X SPACEs or always use TABs.

    At which point I do not see how SPACEs are better than TABs.

  15. AndrewNo Gravatar Fri, 08 Aug 2008 01:55:14 CDT

    @Martin,

    Because when you insert spaces there is only one option: Spaces. When you use tabs there are two options: Tabs _or_ spaces. Thus, it is possible to be inconsistent one way but not the other.

    Cheers,

    Andrew.

  16. TomNo Gravatar Fri, 08 Aug 2008 06:48:12 CDT

    Um, what if I insert 4 spaces sometimes and 2 spaces others?

  17. AndrewNo Gravatar Fri, 08 Aug 2008 08:29:46 CDT

    You will see that you have erred. Not many people are intentionally inconsistent.

  18. samNo Gravatar Fri, 08 Aug 2008 09:53:10 CDT

    Andrew, I see you point. The problem as you see it is that when using tabs people can add spaces in there as well. I never really thought about this before and checked some of my code and low and behold I had done it in some places as well.

    So i get the point switch the tab key to insert spaces and know there in consistency. The only proble with this I see is this. If it is open source there still is no way to ensure that everyone touching the code is also inserting spaces with the tab key. Second lets say that i am looking at a project that used spaces and for som reason that person liked large indents or small indent say 1 space. I couldn’t normalize it very easily for my liking witxhhappen to be 2 spaces. If a tab was in there then it would look the way that any individual developer wanted (but then you have to somehaw garentee no one slides a space in there).

    In the end I have decided to go with spaces, mainly because here I was thinking “I always use tabs” and found that wasn’t true. So if I can’t force myself how do I force others. Now lets just make everyones IDE default to spaces :)

  19. Jake ScottNo Gravatar Thu, 14 Aug 2008 17:52:47 CDT

    I like using spaces in html, when you view source, or open the file in a simple text editor it way easier to see how the document is structured.

    On a side note teaching visual studio how to auto-format your html properly is pretty simple. Just go tools > options > HTML > Format > Tag specific options > Client Html tags..

    Cheers for the File explorer plugin too!

  20. AndrewNo Gravatar Thu, 14 Aug 2008 18:48:50 CDT

    Jake,

    No worries, enjoy!

    Andrew.

  21. AlecNo Gravatar Wed, 03 Sep 2008 18:51:40 CDT

    In my view, Sam’s argument trumps all the others, hands down. If everyone on a project uses tabs consistently, then each developer can view the code with their preferred level of indentation (2, 4, whatever).

    I teach, so half the time I’m writing code that’s projected on a screen. When I do, I use large fonts and an indentation level of 2. Since I use tabs, this code looks just dandy when I or my students work on it in an environment where the indentation level is 4 (a level I find easier to work with when fonts are normal working size vs projector size).

    So…
    “Insert tabs” == “individuals can choose indentation level”,
    “Insert spaces” == “you’re stuck with the indentation level chosen by the original author”

    You said it yourself, Andrew - the code example you looked at looked bad because the author had a different default indentation level than you. What if they had inserted spaces as you suggest, but used an indentation level of 4, while your preferred level was 3? Then if you wanted to add anything to the code, at the first level of indentation you’d have to hit tab-space; at the second tab-tab-space-space; at the third tab-tab-tab-tab; and so on. Personally that would drive me insane.

    In my view, the “insert spaces for tabs” setting is nothing but a source of evil inconsistency in code. Does anyone really intentionally hits 4 spaces when they could hit tab? I’d bet a reasonable sum of money that your open source example is the result of two people working on the code; one of whom had their environment set to insert spaces for tabs.

    One more question for those of you who prefer spaces: doesn’t it annoy you to have to hit multiple backspaces just to move out one level of indentation if you inadvertently tab in too far?

    Alec.

  22. AndrewNo Gravatar Wed, 03 Sep 2008 21:03:13 CDT

    Hi Alec,

    Thanks for your comment. I’ll answer a few inline:

    “If everyone on a project uses tabs consistently”

    Problem is they don’t (at least in my experience).

    “What if they had inserted spaces as you suggest, but used an indentation level of 4″

    If that project mandated 4 then I would happily use 4. Project standards trump personal preference. Don’t confuse viewing the code with editing it: The code should be correctly indented even when viewed with Notepad.

    “doesn’t it annoy you to have to hit multiple backspaces just to move out one level of indentation if you inadvertently tab in too far?”

    Once again this is purely a tooling issue. TextMate, for example, un-indents on backspace even when not using tabs.

    Cheers,

    Andrew.

  23. Mat HobbsNo Gravatar Wed, 03 Sep 2008 23:37:53 CDT

    Hi Andrew,

    I used 2-space indentation for perhaps up to 20 years but switched to tabs (can’t recall the reason) for C# for the last 5 years and am preferring it. Sure you sometimes see spaces mixed in with tabs but this was always the case the other way around too - in the past seeing tabs get mixed in with spaces - so no gain there.

    If you mandate spaces on a project you have no way of stopping someone with tab-settings turned on from adding tabs so the problem is symmetric.

    Furthermore, in a typical business there are usually many different projects and each may have their own styles, with spaces you usually end up with 2-space or 4-space with developers being used to each. With tabs it is always only one tab (nobody uses 2 tabs for an indent…) and devs can control apparent spacing to their taste.

    I’d be OK with switching back to spaces one day, and I fully expect having to do that on some future project, but, despite what you say, you still end up backspacing a lot more using spaces and selecting code segments is more problematic in the general case no matter what editor you use. I don’t think you’d really be happy switching between 2-space and 4-space projects on a day-to-day basis!

    Cheers,
    -Mat

  24. MartinNo Gravatar Wed, 03 Sep 2008 23:56:08 CDT

    Just as an experiment, I have started using two SPACE indent and converting TABs to SPACEs.

    Seems to be going okay so far, though I’m still getting used to my code being more “squashed up”. But on the other hand, my code doesn’t sprawl out to the right quite so much. Not that I nest my control statements that much.

    All in all, I am getting used to it. But at least if I want to change back it is a relatively easy code format to revert to TABs.

  25. DougNo Gravatar Tue, 23 Sep 2008 10:12:21 CDT

    The greatest part about tabs is that if one user prefers 2 spaces they configure their editor to turn a tab into 2 spaces, if they prefer 20 then can configure it to be 20. So same source, different based on user *preference*. This way you dont need to perform a user study to determine the proper indent.

Comments