Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The coding standards for the code hosted in drupal.org 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.

share

locked by World Engineer Sep 21 '13 at 2:00

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as not constructive by Tom Wijsman, ChrisF Nov 11 '11 at 12:19

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

13  
IDE default FTW - just press the tab key and see what happens –  user281377 Dec 14 '10 at 11:12
8  
Unsurprisingly, there's -1 "I don't agree" happening everywhere in this thread. People, can we please just keep it to "+1 I agree" or "0 I don't agree"? –  Shabbyrobe Jan 12 '11 at 6:53
1  
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. –  pgfearo Sep 28 '11 at 21:50
4  
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? –  Dean Harding Sep 29 '11 at 12:53
2  
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? That's just showing you that subjective "question"s like this are not a good fit for this format. They do not belong here. –  Lightness Races in Orbit Mar 2 '12 at 14:32

22 Answers 22

up vote 129 down vote accepted

Spaces

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.

share
42  
Your second paragraph is wrong; the answer is 4! –  Daenyth Sep 24 '10 at 19:27
5  
@Daenyth - perhaps if you are using a curly-brace language. Most Pascal/Delphi uses 2. –  Gerry Sep 27 '10 at 1:15
72  
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? –  Xepoch Dec 10 '10 at 16:49
3  
@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. –  Evan Plaice Jan 19 '11 at 11:12
42  
"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. –  Andrea Apr 20 '11 at 9:23

Tabs

  • allow mistakes to be more noticeable, it's annoying to micro manage to spaces.
  • are consistent, they are only used for indentation, unless you are commenting wrong.
  • are customizable, you can specify in the options of the editor how wide they are.
  • are more productive, why type 4 spaces when ye can hit 1 tab.
  • take less size than multiple spaces.
  • take more space than a space.
  • take less time to go through.
  • tend to work better in IDEs.

Spaces should be used where tabs are completely useless.

Even for aligning parameters and comments, tabs still work better.

share
14  
Why type 3 letters "you" if you can hit 1 letter "u"? –  Christian Mann Nov 6 '10 at 8:06
24  
wrt "more productive", personally I hit TAB and let the IDE do the conversion for the appropriate number of space characters to introduce. Perhaps that you need to upgrade :p ? –  Matthieu M. Dec 14 '10 at 19:14
6  
@ChristianMann: Because I don't want to arrive on UTube. –  Tom Wijsman Dec 14 '10 at 20:57
3  
-1: More noticeable mistakes: not true; consistent: hardly because of customizable: yes, but it's also disadvantage at the same time; more productive: not true, virtually all ides can convert tabs to spaces; less size: yes, but practical? no; less time: no with reasonable IDE; tend to work better: not at all if you use multiple environments and tools –  MaR Nov 8 '11 at 13:30

Plenty of arguments have been given already, but nobody mentioned where we could be headed in the future.

Tabs nor spaces!

Ideally code should be considered data, and not be stored in any specific text formatting. Any developer can apply his own preferred view. Furthermore, this view shouldn't be limited to just text, but can include tables, color pickers and mathematical formulas.

This idea isn't too far-fetched. It was JetBrain's Language Oriented Programming editor Meta Programming System (MPS) which first made me realize this resolves the entire discussion, simultaneously adding a lot of extra possibilities. (Yes this is possible with editor plugins, but working on text directly adds so many unnecessary complexities, as opposed to the approach MPS takes.)

As opposed to tabs and spaces, there are little disadvantages which can be mentioned for working directly on abstract syntax trees. All that is needed is for the technology to mature to a commercially viable product. The first signs of this are showing up. Largely built upon MPS, a commercial actionscript editor, Realaxy was created.

I'd love to see one of the big players jump on the concept of this technology and see what happens!

share

Not mentioned until now: There are languages (Python, Haskell) where indentation matters. But 1 character counts as 1 character, be it a space or a tab so the indentation seen by the compiler may not be the same one you see on the screen if you use tabs.

Therefore, in languages like Haskell, spaces are a must. In Makefiles, TABS are a must. In all others, it's a matter of personal taste and nowadays not a great deal - every decent editor has a "(leading) tabs to spaces" and "(leading) spaces to tabs" command.

share

Neither is better, neither is worse. The only important thing is to be consistent.

If you are a team of one, pick whatever you personally like. Consider what the default behavior of your favorite editor, but pick whatever you like.

If you are on a team, do what the team does. Period.

In my various jobs I've used two spaces, four spaces, eight spaces, tabs, spaces-and-tabs, I think I may have also used one space. I tell my editor what to do then I don't think about it again, the editor works out the details.

The only other thing is to make sure you pick a smart editor. Emacs or vi? Now that's a holy war I'm willing to fight :-)

share

The answer is that there can be no single proper indentation character for every situation. Formatting using characters is inflexible and can cause conflict when different styles are used within a team.

The only method to format code flawlessly and flexibly with different formatting styles is to do it virtually, that is, without any indentation characters. The only code editor I know that supports this though is the one used in the sample below:

To demonstrate virtual formatting, the screenshot below is from an XSLT editor* that uses this indentation method (there's also a short video here). Every character in the XSLT has been highlighted in yellow, for illustrative purposes, to allow the only tab or space characters in the content to be seen clearly. The code indentation is handled by the editor's rendering system adjusting the left margin (which has a white background).

enter image description here

The only leading space characters precede the Books lines, because this is literal text content, not code, these space characters must be preserved.

With virtual formatting you choose the indentation width to suit the environment and indentation style without affecting any characters in the source file. You can even set the indentation width to 0, if you need a flattened view of the code as shown below:

enter image description here

To contrast this with space character formatting, the same XSLT opened in an editor without virtual formatting is transformed by that editor's auto-formatter to this:

enter image description here

The larger blank yellow blocks in the screenshot above clearly show the space characters added by the formatter of the conventional editor. Unfortunately, these now can't be distinguished from real content so the XSLT would have to be modified to correct this issue.

Summary

XSLT is possibly an extreme case, but this principle holds true for many programming languages: Characters should be used for content and an alternative method sought when it comes to formatting.

*Disclosure: The XSLT Editor with virtual formatting was developed by my own company

share
1  
Sadly, you're probably about 50 years ahead of your time. The frankly absurd idea that programs are sequences of characters is still very deeply engrained in most programmers' minds. –  Tom Anderson Feb 28 '12 at 20:48

Tabs

Now, of course, consistency matters more than either one, and a good IDE makes the differences negligible. That said, the point of this thread is to be a holy war, so:

I prefer tabs:

  • They're a character specifically meant for indentation
  • They allow developers with different preferences in indentation size to change how it's the code looks without changing the code (separation of data and presentation for the proverbial win!)
  • It's impossible to half-indent something with tabs. So when you copy code from some website that used 3 spaces into your 4-space indented file, you don't have to deal with misalignment.
share
12  
What about copying that 3-space indented code into your tab-indented file, which you have set your tabs to 3 spaces? When you send your code to your compatriots they wonder why that function has such bad indentation! –  Wayne Werner Sep 1 '10 at 20:16
27  
Tabs are not originally meant for indentation, they are meant for tabulation, and do a dreadful job at it... –  Timwi Sep 2 '10 at 19:15
14  
@Timwi: Wikipedia seems to say that they are meant for moving the cursor to the next of a set of fixed columns. Doesn't sound like tabulation was the primary function. Also, I was referring to the post-typewriter era. –  Fishtoaster Sep 2 '10 at 19:31
40  
Plus a lot of programmers spend a significant amount of time writing html, css, etc, were the fact that a file with 4 spaces instead of tabs is 5-15% larger is actually significant. Example: this page you are on right now loaded at least 10% slower than it should have because the SE team used spaces. Check for yourself. –  MGOwen Dec 14 '10 at 1:46
23  
Another benefit of this approach - fewer keypresses when using arrow keys for left-right navigation. –  NickC May 18 '11 at 17:20

I used to use spaces, but I've been using tabs lately purely because that's what Eclipse was set to when I finally noticed. All of the other developers on my team use Eclipse, so it made sense to standardise on tabs when we realised we'd already been using them for ages and there was no reason to bother changing to spaces. I've been surprised by how much of a non-issue it has been.

Setting the displayed tab size to 3 or 5 characters in your IDE greatly simplifies distinguishing between code sections that are indented by spaces (almost always 4 these days) and those that are indented by tab.

share

Apparently tabs mess stuff up in Delphi so I don't use tabs in Delphi.

However I do everything else using Emacs and always use tabs because my tabs go exactly where I want them to go.

share
1  
edn.embarcadero.com/article/10280#4.3 That's just the style guide, but we've had some weird errors that I can't explain because of tabs. It may also be due to Visual Source Safe. I can't explain it, it's just part of the lore of our company. –  Peter Turner Sep 24 '10 at 20:04
1  
The problem is that the installation defaults for (at least) some versions of Delphi don't match usual Delphi idioms. So you end up with code that has BOTH. –  Gerry Sep 27 '10 at 1:24

Why can't someone implement this:

  • code is stored in a compact format beneficial to the system in question
  • as each dev opens the code, it is formatted exactly how they want it
  • when they have finished with it, back to the compact format

Everyone is happy as they all see their 'own' format

Is that so difficult?

share
2  
Because I don't have enough free time to fix all the existing editors and IDEs that people use, and - even if I did - convincing everyone to upgrade would be even harder. –  Peter Boughton Sep 9 '10 at 18:07
13  
Sounds kind of like tabs. Each developer opens up the file and get the indentation width they want. –  rmac Nov 6 '10 at 17:35
1  
You can already set up RCS or CVS to use the indent program. –  Xepoch Dec 10 '10 at 16:56
3  
and it's prudent to use indent to force indenting/ code foramtting standarization so that devs with funny spacing ideas don't get blamed / annotated for all those formatting changes –  Tim Williscroft Dec 14 '10 at 2:56

Tabs for the win.

share

Spaces, because when you align comments to the right of code, or function parameter lists, or complex multiline expressions, or things of that nature, you want your beautiful work to appear right for everyone. If you use tabs and allow people to set their tabstops differently, they will break alignment for all but the simplest cases of code indenting.

Besides, it's blatantly obvious that everyone in the world should be using vim, which makes it trivial to indent, unindent, and navigate through "tab stops" even in space-indented files.

share
18  
Sounds like you're drawing ASCII art rather than coding. –  rmac Nov 6 '10 at 17:21
3  
I would add: Tabs musn't be used for alignment. If you are tab user and want to align you must first type correct amount of tabs for indentation and THEN correct amount of spaces for alignment. Only then you see tab formatted code right across all users and editors. –  MaR Nov 15 '11 at 15:30

All of the arguments for tabs are great in theory. But...

In theory, practice and theory are the same. In practice, they are not.

Yes, with tabs you can decide your indentation level. And yes, you can use a combination of tabs and spaces to align things. And in an ideal world it would

In reality, you can't see the difference between spaces and tabs, they always seem to get mixed up when moving code around, and viewing the code in another program that has tabs set to 8 columns is a nuisance.

I used to use tabs. Then I started to work as part of a team and share code. I quickly became a proponent of spaces. So while I can sympathize with the utopia of tabs, I can't imagine not using spaces.

share
6  
you can get an editor like Kate which presents tabs with a 'ghost' character - I can easily see tabs vs spaces when coding. –  HorusKol May 4 '11 at 6:22
8  
Every editor has a "show whitespace" option now, and Visual Studio has a 'tabify' option –  Chris S Jun 17 '11 at 9:31
2  
Good argument. That's the reason I gave up on trying to convince people that tabs are more logical. Even programmers who should be used to abstract thinking seems to confuse tabs and spaces ("if it looks like four spaces, then it must be four spaces!"). The nail in the coffin for me was that it seems to be impossible to configure Emacs to indent only with tabs. –  Martin Vilcans Jun 22 '11 at 21:10
3  
+1 The tab utopia is great, but every time that I moved code between editors with different tab widths the code has been messed up and misaligned. It is too easy to silently do the wrong thing with tabs. –  Mankarse Nov 8 '11 at 8:27

I'm a 4 space kind of guy, tabs just aren't consistent.

share
33  
facepalm..... –  Ullallulloo Sep 9 '10 at 2:20
5  
How are they "not consistent"? They are always a single character representing a single level of indentation. That is the very definition of a consistent indentation scheme. (And that you can personally, without affecting anyone else, visually render that any way you choose is a plus.) –  Lightness Races in Orbit Mar 2 '12 at 14:30

Tabs are the natural and orthodox choice, as they are by definition used for indentation.

Unfortunately, tabs are unevenly implemented, so the only real world solution is 4 spaces.

share
8  
Since when are tabs (tabulators) used for indentation “by definition”? By their very definition they were invented for tabulation (although they do a dreadful job at that too). –  Timwi Sep 2 '10 at 19:20
2  
Technically, the return key was invented to move the typewriter head to the front of the line, whereas the move to the next line was performed by rotating the roller. Later, the return key would also automatically advance the roller as well. You don't see people getting all hung up on the return/enter key though. The tab key was clearly added to computers to perform indentation. It's a digital analog of the tab stop, which I had on my old non-electric typewriter. Tab stops could be set to move the roller to any position you chose, almost identical to the tab key's current role in indentation. –  Jordan Reiter Dec 12 '11 at 20:00
1  
Yeah, but that is really an OS issue, not a keyboard issue. No matter what computer you are on, you expect hitting either the enter or the return key to take you to the next link. You don't use one key to issue a line feed and one to issue a carriage return. –  Jordan Reiter Aug 27 '12 at 15:07

Use tabs to indent the start of the line, one tab per indent level, and let everyone pick how wide they want that to be.

Use spaces if you're lining up characters within a line, so they always line up regardless of tab size.

And find and punch all the early software authors who let this stupid thing become an issue in the first place.
(Seriously, why is this even something that's discussed? Next you'll be telling me you also want to use multiple characters for line breaks!)

share
33  
Yes yes yes yes yes. One hundred percent yes. This lets everyone set their own tab width while preserving tabular formatting. If you're worried about keeping within 80 columns (as you probably should be!) then just pick an arbitrary maximum expected tab size (such as 4) and keep within the margins for tabs at that width. Honestly, is this so difficult? –  Jon Purdy Sep 24 '10 at 20:23
3  
This is the only correct answer here –  J.Ashworth Dec 6 '12 at 23:17

Spaces or Tabs - What Atwood really says is pick one thing and be consistent in your project. The only holy grail of code formatting is to make certain that its consistent so that the psychopath who maintains your code after you doesn't feel compelled to remedy the situation permanently.

That said, if you're working in Python or any other language where whitespace is an actual programming construct I can't imagine using tabs.

share
4  
I use tabs. I don't know if you've done any Python programming, but it's relative indentation that matters. If line 1 is x indents in, and line 2 is x + 1 indents in, it's considered more indents. Indents can be double spaces, 4 spaces, tabs, doesn't matter as long as you're consistent. –  Macha Sep 8 '10 at 22:07
3  
I use tabs with python at work. It's terrible. –  rhettg Sep 12 '10 at 15:55

Some relevant opinions that may be of interest:

share
105  
Just because you're famous doesn't make you correct. –  Xepoch Dec 10 '10 at 16:52
5  
@Matthieu M. , yes I have read each, but still don't agree. I don't like olives either but doesn't make me wrong because others do. I find it ludicrous that people wouldn't use the tab for indentation, but this is why they call them CS holy wars... –  Xepoch Dec 14 '10 at 22:56
2  
@Xepoch: yes, I suppose everyone is entitled to one's opinion :) I do think though that Guido has pointed the main issue: mixing tabs and spaces is evil, because both appear as "blank space" to the user. –  Matthieu M. Dec 15 '10 at 8:06
3  
@Fishtoaster: When you say that Joel says spaces, shouldn't the link be to an actual Joel quote? The link just goes to the JoelOnSoftware forum where some ppl (most likely not speaking for Joel) discuss tabs vs spaces. –  Andreas Magnusson Mar 7 '12 at 11:25

You obviously need a combination approach.

If you're sharing code with other developers, you need to standardize, and since that's impossible (koff koff), you need to make everyone do four spaces.

Then you need an editor that's smart enough not to be stupid about that, to know that it should treat a line with four spaces at the front of it like it's indented. Any modern IDE or programmers' editor can auto-flow code with spaces instead of tabs.

share

I. absolutely. hate. 4. spaces.

Why? Mainly because I'm tired of navigating my code with a keyboard and having to constantly hit left left left left to go over one indentation. This was born out of early versions of Notepad++ and even plain Windows notepad where there was no such thing as a format button. I had so many issues when people would only use 3 when I had used 4 everywhere else among other things.

The other reason is that the tab character exist specifically for indentation, and was only later adopted for navigation. Why are we doing space space space space when a simple tab would work? Why should IDE's have to deal with 2-5 spaced code and format correctly when a simple tab and a preference option would work?

Unfortunately I'm the minority.

share
31  
You are inefficient at keyboard code browsing. You should use CTRL+LEFT and CTRL+RIGHT to skip through whitespace and tokens. So indentation wouldn't matter. –  Lorenzo Sep 1 '10 at 22:52
7  
Where does this misconception come from that “tabs exist specifically for indentation”? Tabs are called tabs (tabulators) because they were invented for tabulation, not indentation, and they do a dreadful job at both. –  Timwi Sep 2 '10 at 19:17
7  
@Lorenzo: You are inefficient at keyboard code browsing. You should use w and b (or e and ge) to skip through whitespace and tokens :P –  R. Martinho Fernandes Sep 15 '10 at 3:45
6  
For the sake of devilish advocacy: don't inflict badly-formatted code on editors that you consider inferior. Use tabs. –  Jon Purdy Sep 24 '10 at 20:29
11  
@Timwi: sorry to necro, but the computer tab key was NOT invented for tabulation. The TYPEWRITER version of that key was, but by the time keyboards evolved for use with computers, the tab key was used for indenting more than tabulation, and the term "tab" had become synonymous with indent. Tabs for COMPUTERS are to indent, and it is only by convention that the name of the key matches that of the typewriter. Your argument is akin to claiming that the computer return key was intended to start a new line and move the cursor to the far left, because that's what it was for in a typewriter :) –  Beofett Apr 12 '11 at 16:17

I personally like to use tabs in everything, since each developer can control the amount of indentation per tab. That way you get flexibility in display.

That being said, I usually mimic whatever coding style is in the file to start with (since I spend a lot of time doing maintenance work).

share
3  
That breaks as soon as you start indenting for anything other than statement nesting. For example if you want to align function arguments on the same column, one per line. It's possible by mixing tabs and spaces but very tedious. –  finnw Sep 3 '10 at 7:36
2  
It's not tedious at all, since it means you can tab most of the way there and then just use a couple of spaces, rather than having to type dozens of spaces just to get to the right column. –  Jordan Reiter Dec 12 '11 at 19:55
1  
@finnw you assume everyone is using a monospaced font - in 2012, significant number of devs use proportional. If you have non-whitespace chars to the left of your first argument, there's no way to guarantee arguments on subsequent lines will align for everyone. –  paperjam Dec 2 '12 at 13:53
2  
I hate to say this, but I can't imagine programming in a variable-width font. The sheer wrongness of it dwarfs brace standards, tabs vs spaces, everything...am I just getting old? (What happens when you get an error message with a line and a column number?) –  lunchmeat317 Feb 21 '13 at 4:21

I do not think that there is such thing as proper indentation (at least not without minor war).

Personally I like four spaces. They enable me to read code much faster and they look same in every editor - even Vi.

share
9  
vi (or rather vim) is one of the better editors for working with tabs, As well as being able to set the tab width and automatically expand to spaces (like most editors can) you also have the softtabstop option and the retab and retab! commands. –  finnw Sep 3 '10 at 7:32
2  
Four spaces do not look the same everywhere. More programmers now are choosing proportional fonts in their IDE for a more pleasant and productive coding experience. Four spaces can look like a pretty tiny indent in these situations. –  paperjam Dec 2 '12 at 13:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.