Tabs versus spaces—what is the proper indentation character for everything, in every situation, ever? [closed]
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.
Closed 11 years ago .
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
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.
community wiki
Sep 3, 2010 at 7:43
IDE default FTW – just press the tab key and see what happens
Dec 14, 2010 at 11:12
Whatever you choose, it must be used by the whole team but not when one uses tabs others spaces.
Aug 24, 2011 at 9:54
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.
Sep 28, 2011 at 21:50
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?
Sep 29, 2011 at 12:53
Comments disabled on deleted / locked posts / reviews | Show 4 more comments
22 Answers 22
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.
community wiki
Your second paragraph is wrong; the answer is 4!
Sep 24, 2010 at 19:27
@Daenyth – perhaps if you are using a curly-brace language. Most Pascal/Delphi uses 2.
Sep 27, 2010 at 1:15
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?
Dec 10, 2010 at 16:49
@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.
Jan 19, 2011 at 11:12
"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.
Apr 20, 2011 at 9:23
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:
- 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.
community wiki
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!
Sep 1, 2010 at 20:16
Tabs are not originally meant for indentation, they are meant for tabulation, and do a dreadful job at it.
Sep 2, 2010 at 19:15
@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.
Sep 2, 2010 at 19:31
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.
Dec 14, 2010 at 1:46
Another benefit of this approach – fewer keypresses when using arrow keys for left-right navigation.
May 18, 2011 at 17:20
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!)
community wiki
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?
Sep 24, 2010 at 20:23
Yes, but don’t use spaces to line up within a line. Your colleagues may be using a proportional font so your ASCII art is going to look weird on their screens.
Dec 2, 2012 at 13:45
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.
community wiki
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 ?
Dec 14, 2010 at 19:14
-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
Nov 8, 2011 at 13:30
@MaR: Noticable mistakes is true, the width of a tab is larger. Also. Python! Tabs are consistent as a single Tab is an indentation, while you otherwise have to define how many spaces you will use for an indentation. Customization is an advantage. You are using tabs in the first place, so it’s more productive. Seems you can’t even give a good reasoning why it is impractical, haters gonna hate. 😉
Nov 11, 2011 at 11:51
@TomWij: How is tab larger than equivalent number of spaces? Python is virtually the only language that got whitespace right to use tabs safely (but still – it offers only little advantage compared to "insert spaces"). Try also other languages, legacy environments, diffs/merges, terminals, sccs to see why tabs are still utopic "ideal".
Nov 11, 2011 at 12:37
@MaR: Where did I state ‘equivalent number of spaces’? I’ve tried all those, tabs work perfectly in them!
Nov 11, 2011 at 13:07
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.
community wiki
you can get an editor like Kate which presents tabs with a ‘ghost’ character – I can easily see tabs vs spaces when coding.
May 4, 2011 at 6:22
Every editor has a "show whitespace" option now, and Visual Studio has a ‘tabify’ option
Jun 17, 2011 at 9:31
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.
Jun 22, 2011 at 21:10
+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.
Nov 8, 2011 at 8:27
I used to use tabs too, but I was running into trouble with some IDEs that couldn’t insert spaces for continuation lines and I hated to replace all those auto-inserted tabs with spaces (Visual Studio was one of them). So I just sticked with spaces. Plus spaces allow you to use half-sized indents for things like public: in C++ class declarations so they look prettier (especially with nested classes).
Jul 19, 2013 at 21:02
Some relevant opinions that may be of interest:
- Guido says spaces
- Joel says spaces
- Atwood says spaces
- Zawinski says spaces, sort of
community wiki
Just because you’re famous doesn’t make you correct.
Dec 10, 2010 at 16:52
@Xepoch: have you read their rationale 🙂 ?
Dec 14, 2010 at 19:12
@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.
Dec 14, 2010 at 22:56
@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.
Dec 15, 2010 at 8:06
@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.
Mar 7, 2012 at 11:25
Tabs for the win.
community wiki
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.
community wiki
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.
Sep 1, 2010 at 22:52
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.
Sep 2, 2010 at 19:17
@Lorenzo: You are inefficient at keyboard code browsing. You should use w and b (or e and ge ) to skip through whitespace and tokens 😛
Sep 15, 2010 at 3:45
For the sake of devilish advocacy: don’t inflict badly-formatted code on editors that you consider inferior. Use tabs.
Sep 24, 2010 at 20:29
@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 🙂
Apr 12, 2011 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).
community wiki
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.
Sep 3, 2010 at 7:36
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.
Dec 12, 2011 at 19:55
@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.
Dec 2, 2012 at 13:53
You’ve never tried writing HAML, have you.
Dec 14, 2012 at 9:41
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?)
Feb 21, 2013 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.
community wiki
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.
Sep 3, 2010 at 7:32
Would babies really die if we just said 4 spaces, everywhere, always, no choice – and moved on?
Dec 7, 2010 at 1:34
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.
Dec 2, 2012 at 13:50
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.
community wiki
That is a really good feature of many editors — the ability to go quickly through mounds and mounds of spaces.
Nov 6, 2010 at 8:05
Sounds like you’re drawing ASCII art rather than coding.
Nov 6, 2010 at 17:21
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.
Nov 15, 2011 at 15: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.
community wiki
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).
Sep 2, 2010 at 19:20
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.
Dec 12, 2011 at 20:00
@JordanReiter RE: "You don’t see people getting all hung up on the return/enter key though.": Have you never faced CR (represents moving the typewriter head to the front of the line)/LF (represents rotating the roller to the next line) issues?
Aug 25, 2012 at 7:06
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.
Aug 27, 2012 at 15:07
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.
community wiki
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?
community wiki
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.
Sep 9, 2010 at 18:07
I’m sure they could build it in to SVN or find some other way of leaving the end tools out of it.
Sep 9, 2010 at 18:15
Sounds kind of like tabs. Each developer opens up the file and get the indentation width they want.
Nov 6, 2010 at 17:35
You can already set up RCS or CVS to use the indent program.
Dec 10, 2010 at 16:56
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
Dec 14, 2010 at 2:56
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.
community wiki
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.
Sep 8, 2010 at 22:07
@Macha – Thanks for the feedback. I’ve never worked doing Python development but my who have have all told me they use spaces so its interesting to meet someone who uses tabs.
Sep 9, 2010 at 14:26
I use tabs with python at work. It’s terrible.
Sep 12, 2010 at 15:55
I’m a 4 space kind of guy, tabs just aren’t consistent.
community wiki
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.)
Mar 2, 2012 at 14:30
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.
community wiki
Can you provide an example of this? I’m honestly curious because I’ve been using Delphi for 15 years and have never seen this.
Sep 24, 2010 at 19:32
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.
Sep 24, 2010 at 20:04
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.
Sep 27, 2010 at 1:24
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).

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:

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:

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*