Tabs versus spaces—what is the proper indentation character for everything, in every situation, ever?

  • The coding standards for the code hosted in suggest to use two spaces to indent the code; other sites suggest to use tabs to indent the code.

    What is the proper indentation character for everything, and in every situation? Please explain the answer you give.

    IDE default FTW - just press the tab key and see what happens

    Whatever you choose, it must be used by the whole team but not when one uses tabs others spaces.

    I tried to edit this to correct the implication that tabs/spaces are the only way to indent code. The edit was removed, but hopefully this comment will remain: Instead of indentation characters, why not use the left-margin instead? - this can be fully automated by the editor and word processors have done this for decades.

    I really don't think there should be an "accepted" answer here. The fact the the answer with fewer votes is actually first is testament to that: why should the asker get a special power (i.e. being able to put their favorite answer first) just because they asked the question?

    Remember that an accepted answer is not an answer that is universally accepted from all the users; it's the answer that is accepted from the OP, who chooses the answer that helped him more. Only the OP can know which answer helped him more than other answers; even in the case of two questions that basically say the same thing, there is still an answer that can help the OP more than the other.

    @Shabbyrobe: While I agree some should refrain from harshly downvoting, I think it's a powerful tool in S.E., and **a good thing** (in my opinion). There's a gap between indifference and disagreement, and the latter should be clearly voiced. If you put yourself out there w/ a statement w/o a strong basis, expect to collect a lot of these. If you do have a strong basis, you'll get the occasional disagreement (**hopefully with some explanation, which is what is often lacking**). If you **can't** back it up, then the question is possibly too open-ended, biased or subjective and shall be flagged.

    @Shabbyrobe: also, it gives a strong incentive for people to delete their own answers when they are of poor quality (which isn't done often enough: if a non-winning answer is devoid of any added-value in a thread, it should simply be deleted).

  • Correct answer

    10 years ago


    A tab could be a different number of columns depending on your environment, but a space is always one column.

    In terms of how many spaces (or tabs) constitutes indentation, it's more important to be consistent throughout your code than to use any specific tab stop value.

    Your second paragraph is wrong; the answer is 4!

    @Daenyth - perhaps if you are using a curly-brace language. Most Pascal/Delphi uses 2.

    @Gerry: No actually, python.

    Formatting errors don't often occur anymore: almost every IDE is at tab=4spaces by default (if anyone's dumb/stubborn enough to insist on 7 spaces, let them change the setting and deal with the mess. The rest of us have programming to do). On the other hand, converting spaces to tabs in web files (html, css, etc) saves not-insignificant download time (varies of course, but you'd be suprised - this page lost about 10% just on the html alone).

    Why should I be mandated to your 2 or 4 SPACES of indentation when a TAB can be dynamically set and indicate a single level of indentation?

    2 or 4 may depend on your screen size and the type of the code. For usually extremely nested HTML 2 might help keep the width in view.

    @MGOwen The download time argument for website coding is easily resolved nowadays using source minifying on css/javascript and whitespace removal on HTML. See mod_pagespeed to see what I mean.

    "A tab could be a different number of columns depending on your environment" Yes, this is exactly the advantage. Everyone can set it to his taste and be happy.

    A space is a single column, unless you use 2 spaces to represent a column. Or 4. Or 8!

    For languages where whitespace is signficant (F#, python), it has to be spaces anyway. Suits me fine :)

    A space is only one column if you're using a fixed-width font. Thereby you are also stating that you _must_ program with fixed width fonts.

    **A tab is always one indentation so it isn't different along environments**, while with spaces you have a variable amount. *Would you rather like a document with 1 space indentations or with 8 space indentations? Oh, seems I sometimes typed some other amount of spaces by accident...*

    Sorry, but this answer is terribly wrong! DON'T use spaces. If you share a code with other people and their code indents with spaces and yours indents with tabs, you'll have a terrible problem. Because you use tabs, your editor is configured to use tabs and if you open a spaced-indentation code you'll see a fucking piece of shit. If the code is indented with tabs you can configure the width!! You can set a tab to be 2 columns without the need to hardcode 2 spaces. I hate when the code is not tab-indented.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM