6

I used to be a tab freak -- I would always use tabs to indent my source code, and if I saw any spaces, I would instantly do a regex search/replace to replace all leading spaces with tabs.

...until I realized that I hadn't noticed the presence of a lot of the spaces in some files, such that they opened up inconsistently in different editors (e.g. Notepad++ vs. Emacs vs. Visual Studio).

What are good reasons for why one is better than the other in general? Is either one generally known as better practice?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    Google "spaces vs tabs" for a pretty good coverage of this issue. – Nate C-K Jan 19 '11 at 06:25
  • 8
    And when I say "issue", I mean "holy war". – Nate C-K Jan 19 '11 at 06:25
  • It. Doesn't. Matter. Really. – slugster Jan 19 '11 at 06:27
  • i believe tabs are faster to type than spaces, imagine typing a nested if loop using spaces instead of just 2-3 tabs – robobooga Jan 19 '11 at 06:27
  • @Nate: I like your litotes... :] – user541686 Jan 19 '11 at 06:27
  • 2
    To those who said so: I'm just curious, why is this not a real question? It's not vague, it's about a specific issue, and it's not difficult to tell what I'm asking, so why the votes? Please don't close this question without telling me what's wrong with it first; thank you! :) – user541686 Jan 19 '11 at 06:29
  • @robobooga: If you find tabs faster than spaces, it's because you're not using an editor that is friendly to indent-with-spaces style (or haven't configured your editor for it). Using vim with :set expandtab, for example, makes it pretty much a non-issue. – Nate C-K Jan 19 '11 at 06:32
  • 1
    Oh, it's a real question, it just doesn't have a correct answer. It comes down to personal preference. In your question, you yourself say, "I hope this isn't too subjective of a question..." Well, sorry to dash your hopes: it is. – Nate C-K Jan 19 '11 at 06:34
  • @Nate: If it's subjective, then why did people mark it as "off topic" and "not a real question" instead of marking as "subjective and argumentative"?!?! – user541686 Jan 19 '11 at 06:36
  • @Lambert: It's too early in Europe still. – Johan Kotlinski Jan 19 '11 at 06:38
  • You are right. I voted subjective and argumentative. It is very simple: this question cannot be answered. – Kobi Jan 19 '11 at 06:39
  • @Nate C-K yup i agree with you, those who configured may have different views, but i usually work with client's computers and most of them do not configure it, therefore I came to a conclusion that tab is the standard, but seriously it still boils down to each and everyone's working environment and preferences – robobooga Jan 19 '11 at 06:39
  • @Kobi: Well thanks for voting and then justifying your own vote, but I was wondering about why others voted the way they did... :\ – user541686 Jan 19 '11 at 06:40
  • Well jeez, thanks for closing this. But it's still not making sense to me why those votes saying this was "vague" or "not a real question" were cast. – user541686 Jan 19 '11 at 06:44
  • @Lambert - it will become clear to you why it was closed if you just spend a few minutes googling the subject. – slugster Jan 19 '11 at 07:38
  • @slugster: Again, I'm not asking why it was closed, but not why people voted it as "not a real question"... I can see this as being subjective and argumentative, but I can't see why it's vague or not a real question. – user541686 Jan 19 '11 at 07:41
  • @Lambert - if it still bothers you you can ask a question at Meta, but I don't think it's really that important `:)` – Kobi Jan 19 '11 at 08:20
  • 1
    @Lambert - it was most likely judged as not a real question because: - there is no definitive answer; - it has been discussed a billion times already; - nobody wants it rehashed here on SO; - nobody uses spaces these days unless using an extremely old fashioned editor; - most know that spaces were originally used because the output device (printer or terminal) treated the tab differently. End of story.... hopefully? – slugster Jan 19 '11 at 08:21
  • @Kobi: Haha okay. @slugster: I had no idea about the last part about printers treating tabs differently (never really gave it a thought... I guess I wasn't part of "most people" :-) ). Thanks for the explanations though, at least now it makes a little sense. (Although I'd say that marking it as subjective still makes a lot more sense than "not a real question"!) – user541686 Jan 19 '11 at 08:25
  • 1
    @slugster: not true, there are many who use spaces. e.g. Guido van Rossum advocates it – Johan Kotlinski Jan 19 '11 at 09:53
  • 1
    @slugster: MS Visual Studio 2010 indents with spaces by default, at least if you're in C# mode. I don't think you could consider that "an extremely old fashioned editor". – Nate C-K Jan 24 '11 at 20:20
  • `Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting.` https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md – NVRM Feb 11 '18 at 16:08

1 Answers1

9

With good text editors, it does not really matter. It is just a technical background detail. The UI behavior will be just the same.

The only important thing here is that everyone in a project makes the decision what to use, and that everyone sticks to that. Because, as you discovered, there will be problems if both tabs and spaces are used for indentation inside a file.

UPDATE: When I mean that everyone in a project makes the decision what to use, I of course mean that everyone should agree on the same decision. :D (This can be the tricky part.)

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • 4
    @Hahaha I like your update... +1 :) – user541686 Jan 19 '11 at 06:39
  • 1
    If you use spaces instead of tabs, you may get a slight increase in your source file size. That can always be mitigated if you have some method to omit spaces (ie: compiler, minifier, etc). – Timothy Perez Aug 22 '12 at 06:35
  • 1
    PSR-2 and PSR-12 say: `Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting.` https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md – NVRM Feb 11 '18 at 16:06
  • The thing is that with tabs, every team member will be satisfied because they can configure their IDE at their will. With spaces, there will be inevitably different preferences about how many spaces to use, and whatever you choose as a standard, some people will be forced to work uncomfortably. That for me is a horrible engineering practice. – Petruza Dec 22 '20 at 16:24