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

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.

• Read this first – finnw Sep 3 '10 at 7:43
• IDE default FTW - just press the tab key and see what happens – user281377 Dec 14 '10 at 11:12
• Whatever you choose, it must be used by the whole team but not when one uses tabs others spaces. – Disciple Aug 24 '11 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. – pgfearo Sep 28 '11 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? – Dean Harding Sep 29 '11 at 12:53

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.

• Your second paragraph is wrong; the answer is 4! – Daenyth Sep 24 '10 at 19:27
• @Daenyth - perhaps if you are using a curly-brace language. Most Pascal/Delphi uses 2. – Gerry Sep 27 '10 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? – Jé Queue Dec 10 '10 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. – Evan Plaice Jan 19 '11 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. – Andrea Apr 20 '11 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:

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.
• 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
• 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
• @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
• 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
• Another benefit of this approach - fewer keypresses when using arrow keys for left-right navigation. – Nicole May 18 '11 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!)

• 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
• 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. – paperjam Dec 2 '12 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.

• 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
• -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
• @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... ;) – Tamara Wijsman Nov 11 '11 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". – MaR Nov 11 '11 at 12:37
• @MaR: Where did I state 'equivalent number of spaces'? I've tried all those, tabs work perfectly in them! – Tamara Wijsman Nov 11 '11 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.

• 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
• Every editor has a "show whitespace" option now, and Visual Studio has a 'tabify' option – Chris S Jun 17 '11 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. – Martin Vilcans Jun 22 '11 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. – Mankarse Nov 8 '11 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). – szx Jul 19 '13 at 21:02

Some relevant opinions that may be of interest:

• Just because you're famous doesn't make you correct. – Jé Queue Dec 10 '10 at 16:52
• @Xepoch: have you read their rationale :) ? – Matthieu M. Dec 14 '10 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... – Jé Queue Dec 14 '10 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. – Matthieu M. Dec 15 '10 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. – Andreas Magnusson Mar 7 '12 at 11:25

Tabs for the win.

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.

• 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. – Wizard79 Sep 1 '10 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. – Timwi Sep 2 '10 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 :P – R. Martinho Fernandes Sep 15 '10 at 3:45
• 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
• @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).

• 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
• 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
• @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
• You've never tried writing HAML, have you. – alt Dec 14 '12 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?) – 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.

• 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
• Would babies really die if we just said 4 spaces, everywhere, always, no choice - and moved on? – MGOwen Dec 7 '10 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. – paperjam Dec 2 '12 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.

• That is a really good feature of many editors -- the ability to go quickly through mounds and mounds of spaces. – Christian Mann Nov 6 '10 at 8:05
• Sounds like you're drawing ASCII art rather than coding. – rmac Nov 6 '10 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. – MaR Nov 15 '11 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.

• 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
• 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
• @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? – Max Nanasy Aug 25 '12 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. – Jordan Reiter Aug 27 '12 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.

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?

• 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
• I'm sure they could build it in to SVN or find some other way of leaving the end tools out of it. – adolf garlic Sep 9 '10 at 18:15
• 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
• You can already set up RCS or CVS to use the indent program. – Jé Queue Dec 10 '10 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 – Tim Williscroft Dec 14 '10 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.

• 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
• @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. – Noah Goodrich Sep 9 '10 at 14:26
• I use tabs with python at work. It's terrible. – rhettg Sep 12 '10 at 15:55

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

• 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

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.

• 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. – GrandmasterB Sep 24 '10 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. – Peter Turner Sep 24 '10 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. – Gerry Sep 27 '10 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*

• 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

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.

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.

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!

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 :-)