Jarrod Overson

Wanton Hackery, a guide to things you probably shouldn't do

Jarrod Overson - Wanton Hackery, a guide to things you probably shouldn't do

Why I use spaces over tabs

[UPDATE: I just got an email from someone saying they use tabs over spaces because visually aligning everything by repeatedly pressing the spacebar is too cumbersome. That is not what is meant by using spaces over tabs! Every IDE/editor supports replacing tab presses with a dynamic and variable number of spaces until you get to the appropriate indentation level. It would be insane if this misunderstanding turned out to be one of the cruxes of the issue.]


I never really understood the Tabs vs Spaces debate. When I first started programming I started using tabs but almost immediately realized they just weren’t suited for the job. They promise flexibility but, in practice, that flexibility is largely a lie and you tie other developers to your indentation style regardless. If you are tying developers in to a style, then why use tabs which can add unexpected confusion?

In the examples below, tab characters are colored a darker grey. Don’t get hung up on the coding style, the issues of alignment are what’s important and the code is terse for example’s sake.

Tabs with 4 char widths

This example shows an initial setup that uses a 4-width tab for nearly everything. It looks great and beautiful when you are set up for a 4-width tab, but try changing the tab width with the buttons below the example.


  
  

All the careful alignment goes to crap when you change the tab width. Yes, the fact that you can change the tab width is a great option to have, but if it requires you to reformat your code then it is not actually a real feature of using tabs, it’s the same situation in any scenario. Either manual or automatic reformatting all around.

Tabs with 2 char widths

Below is the same example as above, but optimized for a 2 character width tab. The exact same problem applies, but you can also see that it takes a different number of tabs to produce similar alignments.


  
  

Tabs for block indent only

The last entry on our tabstop tour is an example that only uses tabs for block level indentation, which some people use as a solution to the above problems. This still has its own issues when alignments occur across block level indentations (look at the last block) but also raises the simple question “why?” Block level indentation is the simplest thing for an IDE to take control over, and the actual act of block indentation is almost always hidden from the developer. It is also the easiest to reformat globally since the rules for that are relatively standard, configurable in most decent IDEs or, at the least, tolerable.

Even if this is the most tolerable solution, it introduces yet another wrench into enforcing style with other developers. Now you need to make sure a developer only uses tabs for block level indentation, and spaces for alignment purposes.


  
  

Spaces for tabs, two character indent

And here is the example that doesn’t include tabs at all, just spaces and a 2 tab indentation level. The benefit here is unmistakable intent.



There is no chance for confusion and what you see is what you get.

Contributors/teams do still need to set a common config as to the number of spaces for indentation, but that is better than assuming you don’t need to do that with tabs and getting conflicting code style regardless.

Personally, it really doesn’t matter to me as long as there is a standard. If you use tabs, then you still need to add the tab width you are coding with to coding guidelines. The reason I use spaces is because the environment is self-describing. If you see two spaces, you know the guideline is a two character indentation level. If you see a tab and effed up alignment, you need to experiment with a few configurations in order to find the style the original developers were coding towards.

(and yes, “flexible” or “smart” tabs sound awesome, but are impractical in mixed environments where IDE/editor support is so spotty)

Category: Uncategorized
  • Matt Babineau

    Jarrod- I also agree with what you’re saying. Another EXCELLENT benefit of using 2 spaces/tab is that when you want to share code (stackoverflow, gist.github.com, jsfiddle.com, etc…) and you copy from your IDE and paste in the “DHTML” enabled textareas, you actually get readable, code. Try cutting/pasting something that is tab based!

    Additionally, I think I saw an option the other day in git to auto-change all tabs to spaces (you specific how many). So no matter the mix or preference of your development team, what gets committed at the end of the day is the same. Kind of like a coding zen garden :-)

  • http://brad.is Brad Dougherty

    I’ve seen a lot of arguments for using spaces that use this weird comment style (or some other sort of weird, contrived code style). You’d have none of these issues if the comments were just on the line before.

    • http://opensourceame.com opensourceame

      @Brad Dougherty

      Putting comments on the end of the line is not “weird”. Nor is it the only case where people indent stuff *beyond* the initial indentation. It’s common these days to use a “column” view of variable assignments, for example.

      Forcing everyone to put comments on the previous line, however, *is* weird and limiting.

  • superbored

    Forcing anyone to use your point of view about what is nice or not, *is* weird and limiting.
    It’s just a matter of style, PREFERENCES in the deep. Even when you think there is some advantage using one method or other, really all is a problem of preferences.
    I used to use tabs, now i’m moving to using spaces instead tabs, and exactly 2 spaces… i was doing it before reaching this site, so…

  • Ludovic Urbain

    I’m glad to see all the points made against 4-column-tabs are irrelevant in the real world.

    Now one concern is really important however: how do we achieve consistency ?

    How about, since 4-col-tabs are really the most standard, let’s drop everything else ?

    Oh, and I have a whole blog post on the topic that really addresses all the points that can be made over tabs, spaces and other indentation.

    http://ludovicurbain.blogspot.be/2013/06/code-indentation.html

  • http://github.com/adriank Adrian Kalbarczyk

    Every IDE/editor has (at least it should!) tab->spaces and backward transformation. If you like spaces then use them. I will get your code, my editor will automatically transform your spaces to tabs and I will feel like home. Next you will do the same (but the other way around) and you will be happy.

    The biggest issue about spaces is when you – a 2-spaces fan – work with a 4-spaces fan. How will you work? If you used tabs there would be no problem AT ALL. You will see 2 spaces, he – 4 spaces and you both will be happy.

    And about comments at the end… There is NOTHING more annoying than that! It’s against readability. If you set your editor to not-wrapping mode then you need to scroll horizontally. On the other hand if you choose wrapping mode then there is nothing different in the look of your code and the code with comments one line before.

    I’m writing this based on my own near 10-year experience in coding with many different people, many languages and environments and on dozens of projects.

  • http://42letters.com/ Raitis

    Comment blocks like that are terrible idea. What if I need to change 1 line and other 3 expands automatically to be visually aligned? I changed 1 line, but version control shows 4 changed lines.

  • chris

    If you ignore the side-aligned comments for a moment, then tabs come out of your demonstration pretty well, you can modify the tab size and the code looks good for everyone.

    I personally like the side-style comments, however, obviously, you can’t indent them with tabs because they fall apart with different tab widths. You need them to be the same size so that all the lines will always match up.

    It’s good that the indent sizes can be changed, but it’s bad that the comment gap sizes can be changed.

    Therefore, to me, the solution is clear: use tabs for indentation, and use spaces for alignment in situations where tabs will not work. It’s not inconsistency, it’s just *using the right tool for the job*.

    This has the advantage of not inflicting your tab width choices on other people, while also being able to produce aligned comments and code and stuff. The only problem is that some editors want to transmute your tabs to spaces or back again — which I feel is quite wrong, as they are not the same thing.

  • http://www.watchsintellice.com/ アルマーニ 評判

    reibann

  • Algerator

    The main reason to use tabs rather than spaces IMO: I don’t know about you guys, but I mostly navigate through the code using the arrow keys. It is so annoying when I find some code indented with spaces and I have to go through all these spaces to reach the word i want to edit!

    And seeing that the main reasoning for using spaces is the side-line comments (which I personally use and like as much as the above-line comments), here is the alternative: Let’s agree with all the team the width of the tab character so everyone can see the code well formatted.
    You’d have to reach an agreement anyways if using spaces regarding the amount of spaces to use, so why not use tabs and agree in the tab width configuration instead?

    I don’t really see the point of using spaces :)

  • P5ycH0

    I think the most spaces vs tabs discussions are about indentation.
    Not about how you align comments & other stuff.

    There is no advantage on using spaces over tabs for indentation IMO.
    It’s annoying to navigate through spaces with the keyboard.
    It slows you down when you are trying to select text which starts the beginning of a line (You have to be more precise to NOT include the space).

  • ZeTa

    There is only ONE case when you should block indent with spaces, and that is if you are pasting code into an editor that does not support tabs. :P

  • Idea

    !)
    Almost all programs use now tab = 4 spaces as default;
    All programs allow to customize the tab length.

    2)
    Experience:
    Once, i had a very long PHP file. I replaced every 4 spaces with 1 TAB. After, the file was 50kb LIGHTER!
    The server with the repository had hundred of such files. A lot of Mb wasted.

    3)
    not possible to have problem like using 7 spaces instead of 8

    4)
    not need to agree how many tabs to use: tabs is always one, spaces are usually 4, but some prefer 2 spaces.

    5)
    it’s FASTER to tab than to 4-space

    6)
    In a file with TAB, one can know which character is used for indentation and which one for spacing.


    Compared to that, the problems mentioned in this article are irrelevant, and anyway very rare.

  • WayneB

    Spaces are bad for indentation for much more common reasons than what you’ve listed here for Tabs.

    The number one complaint about spaces is that they’re more work to de-dent in most editors because you can’t effectively use the Backspace key. Go edit some code in JSFiddle and try de-denting your code by pressing Backspace. You have to press it four times to de-dent. If you used two-space indents, you’d have to hit it two times. Twice or four times the work.

    Also, many editors get de-denting wrong so you can’t even fall back to using Shift+Tab. Try de-denting *just* the word Hello in this code without using Backspace – http://jsfiddle.net/p74ve/

    It’s funny that it seems to be mostly web programmers who prefer spaces – which were not invented for indentation – as they shoe-horn HTML and HTTP into any and every possible role.

  • Supermenn

    Space vs tab, a different opinion. Why I prefer tab (or: why tab exist):
    1)
    Almost all programs use now tab = 4 spaces as default;
    All programs allow to customize the tab length.

    2)
    Saved space:
    Once, i had a very long PHP file. I replaced every 4 spaces with 1 TAB. After, the file was 50kb LIGHTER!
    The server with the repository had hundred of such files. A lot of space wasted with no gain.

    4)
    not need to agree how many tabs to use: tabs is always 1, spaces are usually 4, but some prefer 2 spaces.

    6)
    In a file with TAB, one can know which character is used for indentation and which one for “string spacing”.

    3)
    no problems because somebody used 15 spaces instead of 16

    5)
    it’s faster to type a tab than to 4 spaces

    7)
    the allignment is easier to obtain, the comments are nicer to see with less effort

    Conclusions
    - Of course, that’s only my opinion and anybody can have a different one.
    - I think that (2) is the one that in practice has more importance, and generally most of them are part of the reasons why TAB exists.

    • http://twitter.com/alexs77 AlexS77

      Re. “2: Saved space on disk”
      This is IMO no longer relevant. We’re in the age of terabyte disks. Also with compressing proxies (Opera and Google) and also on-the-fly gzip compression done by the server and undone by the client, over-the-wire space isn’t that important either – especially, if one takes into consideration, how many images and how huge these beasts are.

      No, space is no matter. That’s not the 90ies anymore.

      • Supermenn

        Maybe it’s not very relevant… but you don’t gain anything by using more space and generating more C02.
        And here another one:
        8) when you move some code from an inner block to an outer one, it’s easier to remove 1 TAB for each line than 4 (or X) spaces. Example:

        if(condition)
        {
        boolean condition = true //this code must be moved before the if(1)
        ….

        }

        // (PS i saved this code with spaces but they are removed by disquss)

      • http://www.AncientBeast.com/ Dread Knight

        Some areas have problems with bandwidth and slow connections, so adding a few dozen useless kb to your pages means a few extra seconds for them to load.

  • Nathan Holtman

    You can make the third example code work perfect with tabs and spaces:

    http://pastebin.com/qNNkc3xT

    You just have to be patient and add staces to lines that have less chars than other lines, and add tabs before the comments.

  • DMC

    Tabs are clearly superior to spaces :-)

    Take the scenario where you’re using spaces.
    You want to indent a line? You press TAB and the editor gives you X spaces.
    You decide you didn’t want to indent that line? Press Backspace. Instead of removing the indentation, you now have an indentation of X-1 spaces.

    I rest my case…

    • LJE

      Any decent editor is either smart enough to figure that out on its own and remove the entire indentation worth of spaces, or configurable enough that you can make it behave that way. Worst case, use shift+tab.