Why tabs are clearly superior

If you follow me on twitter or have heard one of my talks you’ll probably know I despise spaces for indentation with a passion. However, I’ve never gone into the details of my opinion on stage, and twitter isn’t really the right medium for advocacy. I always wanted to write a blog post about my take on this old debate, so here it is.

Tabs take up less space

Yes, this might be an insignificant difference after gzipping and a nonexistent one after minification. But it means you need these processes to keep your code size reasonable. You depend on them, for no reason. Comments for example are useful, and it’s worth having them even if you knew you couldn’t minify or gzip your code. Tabs could do the same thing as spaces, so you’re just bloating your code for no reason.

Tabs can be personalized

The width of a tab character can be adjusted per editor. This is not a disadvantage of tabs as commonly evangelized, but a major advantage. People can view your code in the way they feel comfortable with, not in the way *you* prefer. Tabs decouple the code’s presentation from its logic, just like CSS decouples presentation from HTML. They give more power to the reader rather than letting the author control everything. Basically, using spaces is like saying “I don’t give a rat’s ass about how you feel more comfortable reading code. I will force you to use my preferences because it’s my code”.

Tabs are better for collaboration

Personalization is incredibly valuable when a team is collaborating, as different coders can have different opinions. Some coders prefer their indents to be 2 spaces wide, some coders prefer them to be 4 spaces wide. Rather than manually or automatically converting the code post-pull, and then back pre-commit, it would be adjusted automatically, depending on the editor’s tab-width setting, so every coder could start editing right away, with their favorite type of indent.

You don’t depend on certain tools

When using spaces, you depend on your editor to hide the fact that an indent is actually N characters instead of one. You depend on your editor to insert N spaces every time you press the Tab key and to delete N characters every time you press backspace or delete near an indent. When you have to use something that’s not your editor (for example when writing a snippet of code on a webapp that embeds something like codemirror) you will have to face the ugliness of your decision. Especially with codemirror, everyone else will have to face the ugliness of spaces too, as it converts tabs to spaces :(

Tabs are easy to select

Assume for some reason you want to select all indents and double them or convert them to spaces. This is very easy with tabs, because that’s their sole meaning. Tabs were invented for this sort of thing. Spaces on the other hand, have many meanings, so you can’t just find & replace space characters. And how do we usually call the practice of using things for a different purpose than they were made for? Yup, that’s right, using spaces for indentation is a hack.

Code indented with tabs is easier to copy & paste

As pointed out by Norbert Süle in the comments, when you copy and paste code that’s indented with spaces, you have to manually adjust the indentation afterwards, unless the other person also happens to prefer the same width indents as you. With tabs, there is no such issue, as it’s always tabs so it will fit in with your (tabbed) code seamlessly. The world would be a better place if everyone used tabs.

But what about the web? Tabs are super wide there!

This used to be a big problem, and even the enlightened coders that prefer tabs usually convert them to spaces before posting code online. However, CSS3 solves this problem, with the tab-size property. It’s supported by Opera, Firefox and very soon by WebKit too. It also degrades gracefully: The code is less pretty, but still perfectly readable.

Are spaces always evil?

Spaces are the best choice for aligning, rather than indenting. For example, in the following code snippet:

var x = 10,
    y = 0;

you need 4 spaces to make the variables line up. If you used tabs, they would only line up when the tab width is 4 and the formatting would look messed up in every other case. However, if this code snippet was also indented, the indents could (and should) still be tabs.

Another example is aligning CSS3 properties with different vendor prefixes. The indent should be done with tabs, but the aligning with spaces, like so:

div {
	-webkit-transition: 1s;
	   -moz-transition: 1s;
	    -ms-transition: 1s;
	     -o-transition: 1s;
	        transition: 1s;
}

It’s just a pointless detail, are you seriously that obsessed?

Um, ok I am exaggerating a bit when I say how spaces suck. I do think they suck, although I admit the world has much bigger problems than coders who use spaces for indentation.

For example, coders that don’t name their variables properly. Or the ones that prefer Emacs over Vim ;)

Further reading

 
Thanks to Oli  for his proofreading!
  • Tab Atkins

    Final reason: because I’ve never met anyone named “Space”.

  • Anonymous

    Yup. Totally agree. Thanks for putting the reasons in writing. =)

  • Sander Aarts

    em vs px

  • Anonymous

    YES! Another strong win for us Tab people! Thank you :)  
    another disadvantage of space-indented code is what I call the “asshole” principle. If I were using spaces to indent, I could be an asshole and indent with 1 space per level! Any normal person that opens the file would have to make sure his/her IDE or text editor is configured correctly to convert the spaces,  and convert back (like you said with committing). It’s just a mess.

  • http://glyph.twistedmatrix.com/ Glyph

    Tabs don’t decouple a code’s logic from its presentation.  A structure-aware editor is what does that decoupling.  We need more tools that can understand the shape of our code and free us from these kinds of pointless debate.  Graphic designers don’t put up with manually serializing their PNGs and JPEGs, why do all of us who write code put up with tools that make us constantly decide about pointless ephemera like this?

    But, until everyone uses such tools, following local convention is far more important than any particular rule.  For new projects, you may feel free to use tabs; unless you’re using Python, where 4 spaces is always the rule :) .

  • http://oli.jp/ Oli Studholme

    Spaces AND tabs!? What new devilry is this!????
    http://www.emacswiki.org/emacs/TabsSpacesBoth

    Also, everyone knows Emacs is teh best. What do you mean, a world PROBLEM!?!?
    infidel!
    :|

  • http://beeminder.com Daniel Reeves

    Heresy! I shall attempt a rebuttal:

    1. Less space. Commenting on this point would waste too much space!

    2. Can be personalized. My code is a precious, precious work of art and you should not be changing the indentation! That would mess everything all up! (Slightly serious. I’m also religious about 80 characters per line, for reasons of space efficiency: if some lines are much longer than average then most of your editor window will be empty space to accommodate them.)

    3. Better for collaboration. See above.

    4. Certain tools. You do depend on them! More so! Because everything will be fubar unless the editor is set to interpret tabs the way you intended. You’re saying you depend on your editor to insert N spaces when you hit tab. I guess so, but aren’t you relying on editor cleverness to jump you to the right indent level anyway? I use 2 spaces per indent so not much difference in my mind.

    5. Easy to select. You often want to slide a block of code right or left, and I suppose that with tabs you have another option for doing so (not a very good option, I don’t think). You never want to do a global search and replace on tabs, except of course to convert them to spaces!

    6. Spaces aren’t evil, unless mixed with tabs. Like you say, you want spaces sometimes for aligning things. But you suggest that in that case you can just *mix spaces and tabs*. That hurls us into a dark pit of befuddled despair!

    I too may be exaggerating. I actually feel more strongly about indentation being only 2 spaces wide than I do about spaces vs tabs. So I’d rather you give me code with tabs than code with 4-space indentation. Which may mean I just defeated my whole case.

  • http://leaverou.me Lea Verou

    Which may mean I just defeated my whole case.

    Yes you did, haha :D

  • Anonymous

    exactly!

  • http://twitter.com/nek Thibaut Allender

    Back in the 90s there was no gzipping on the web and trust me, table layout code was usually huge AND needed to be indented correctly. No need to say you wanted to use Tabs instead of spaces with those tiny modems… Yep, size mattered and still does. I’ve always used Tabs and will always use them.

  • http://profiles.google.com/bhofmann.hotmail.co.uk Bernhard Hofmann

    I still maintain that tabs and coding style are a presentational issue. Our tools are failing to abstract us from the underlying implementation of the code.  Whether I use tabs or spaces, Allman or K&R style should be irrelevant; the code is code, not the layout of the characters in the file. I wrote a note about this a few years ago:

    “I would like it to know that a source code file should be stored in a certain format. This would allow me to use tabs and Allman style, whilst other colleagues could use spaces for indentation, and K&R style. I suspect it would then either be a case of the SCM storing the code in the most efficient format and converting to user-style when the file is retrieved from the repository. This would still allow me to use my preferred text-based diff tool to compare code written by myself and others, without having to worry about extra blank lines and braces in the wrong place, tabs and space indentation. The focus would not be on code-representation, but the code itself.”Source:  http://bernhardhofmann.blogspot.com/2009/06/source-control-and-diff-software.html

  • Panagiotis Astithas

    I used to share your preference years ago, as well as most of your arguments. In my case, the first one that got shot down was the tools. At first it seems that tabs leave you free to set your editor to your personal preference, but the reality is that not all tools are as configurable. And of those that are, you are not always in a position to alter them. Think of hg, git, cat, diff, etc. Not what you’d normally think of as “your tools”, but it’s what all of us use along with our editors. Not much configuration there for tabs. And then think about other tools that embed these tools, like GUI apps and web apps. Like Google Code, or GitHub. Can you get those to use 4 characters per tab?

    Everybody agrees that it’s more important to not mix both tabs and spaces, than to pick a horse in this race. But reaching an agreement about indentation size (or maximum line size, etc.) is predominantly a collaboration concession. It just happens to tie in with the tool limitations. If you prefer 8 char tabs and I prefer 2 char spaces, when one of us reviews the other’s diff on Google Code or Bugzilla, won’t he necessarily have to concede to read code in an unfamiliar format?

    The collaboration card is held by spaces. If you need to work with others, it’s in your best interest to remove all obstacles to collaboration and in most cases, this means settle on a common denominator. Spaces are solid. They cannot be misinterpreted. They don’t change from one workspace to another.

    If they were people, you’d be hanging out with them and buying them beer!

  • http://leaverou.me Lea Verou

    Completely agree!

  • Hanzi Fey

    spaces just feel more right for indentation

  • http://leaverou.me Lea Verou

    Sigh. 

    Frankly, I don’t think you read *anything* past the titles in this post. I have addressed pretty much all your points in it.

  • Panagiotis Astithas

    To be fair, I did read all of it before commenting. Twice actually. Three times by now, after reading your comment. I still don’t see your counterpoints, but maybe I’m just dumb. That’s OK. At least I should get some credit for trying! :-)

  • Anonymous


    I still maintain that tabs and coding style are a presentational issue. ”
    Couldn’t agreee more. That’s all that it is. 
    I just 

  • http://twitter.com/IllusionMH 109th

    I’m using spaces! Yes, I said that.
    Did you try to code in textareas where Tab key means switching to next element? I hate this kind of pages, but there is no another option((
    In this case I prefer use 2 spaces, not Shift + Tab.

    In my own projects – 2 spaces. My code – my habits. If you want to read it, you can easy convert spaces to tabs.
    Every project must have code guidelines, so all contributors can easy “understand” each other.

    P.S. Search&Replace (or automatic feature) can stop the “holy war”

  • http://santiagorojo.com.ar/ Santiago Rojo

    Totally agree. I hope that trend will change in the future. I had to change my tabs to spaces in order to fit in the coding world.

    Tabs FTW :)

  • http://boltanium.com/ kylebolton

    Decent points, however I challenge you to copy a block of code formatted using tabs and try pasting it into a REPL (for example irb) to evaluate it.

  • http://vorb.de Paul

    Or the ones that prefer Emacs over Vim

    Thank you for the compliment.

  • http://leaverou.me Lea Verou

    Ok, lemme try to help:
    a) Your points about web apps are answered with tab-size. It could be in your user stylesheet too ;) b) “Everybody agrees we shouldn’t mix tabs and spaces” -> Um, no. Many people, including myself think the best way to go is using tabs for indenting and spaces for alignment. Everybody agrees we shouldn’t be inconsistent, as in, using spaces for indentation in one code block and tabs in another.
    c) Your point on collaboration doesn’t even attempt to refute my arguments about how tabs are better for collab. It’s as if you never read that part beyond the heading.
    d) “If you prefer 8 char tabs and I prefer 2 char spaces” Then we just use tabs and I set a width of 8 and you set a width of 2. Problem solved.

    Also, I’ve never, ever had issues with git & tabs, so not sure what you’re talking about there.

  • http://usb3gvn.com USB 3G

    Well, interesting post,
    thanks!

  • Isaac

    Trying to propose the absurdity that indenting != aligning is a sidestep of the failure of your argument. Long live spaces.

  • Norbert Süle

    Totally agreed. It is a pain in the a$$ when you have to adjust some external code you just pasted into your code, because that code is indented with 3 spaces, while yours are not

  • Paul d’Aoust

    Just wanted to offer my support and approval; you hit the nail on the head! I love being able to choose my own tab stops, thank you very much.

  • http://afraser.tumblr.com Adam Fraser

    You might have me sold on this, but Dabblet doesn’t allow me to customize tab-size so I’ll be using spaces in the meantime. ;)

  • http://twitter.com/indexzero Charlie Robbins

    Valid points, but there is one big, important tool that unfortunately does not support customizing your tabs: **GitHub**

    Before I could really consider moving away from soft-tabs as an organization that one would have to be fixed. 

  • http://leaverou.me Lea Verou

    Yes it does, same way as every web app: User stylesheet + tab-size.

  • http://www.grailbox.com/2012/01/why-tabs-are-clearly-superior-lea-verou/ Why tabs are clearly superior | Lea Verou « Grailbox

    [...] Why tabs are clearly superior | Lea Verou: “If you follow me on twitter or have heard one of my talks you’ll probably know I despise spaces for indentation with a passion.” [...]

  • http://twitter.com/sbrandwoo Stephen Brandwood

    Your own dabblet.com is a prime example of why tabs are horrible: they get set to 8 spaces by default.

  • http://leaverou.me Lea Verou

    And your comment is a prime example of why commenting on blog posts you haven’t actually read is horrible.

  • http://twitter.com/sbrandwoo Stephen Brandwood

    The 80 characters is a very good point though. If we strive for short line lengths for portability, hope do we cope with someone with a large tab-width setting?

    True portability can only be achieved if you can control how the output will be displayed, and this can only happen with spaces.

    P.S: 2 spaces? you madman!

  • Rob Steward

    Haha, yes! Vim FTW!

    Seriously, as with many things the true way lies in combining both approaches. As you rightly point out:

    Tabs for indentation
    Spaces for alignment

    …and the world is a better place.

  • http://twitter.com/sbrandwoo Stephen Brandwood

    I read it and you have some very good arguments. I’m very much on the “I don’t mind, but lets stick to either tabs OR spaces” side of the fence (a.k.a on the fence?).
    The default of 8 spaces per tab is the one thing that always drags me back into the world of spaces, as it bugs me that I need to spend time converting code sometimes.

  • http://leaverou.me Lea Verou

    If you read the blog post, you would have read about tab-size.  Put that in a user stylesheet and you’re done: Every page now has a proper tab width (unless they specified otherwise).

  • http://www.gregboggs.com/ Greg

    Is this only supported in firefox? How about when you click “view source”? I always, always use tabs when programming, because it’s faster to type than 4 spaces.

    However, when I write HTML, especially if I have to use the WordPress editor, an 8 space tab just kills me. I read the post, I read your comments. I read all the comments down to this one… I just don’t understand what this property does for me.

  • http://leaverou.me Lea Verou

    Opera and Firefox at the moment. WebKit is working on it and should be in it soon (I linked to the bug report, you can follow the progress).

    You can use this property in both websites you make, and in user stylesheets so that every other website conforms to it. View Source (at least in Chrome) is also styled by a CSS file, which could have tab-size I guess.

  • http://www.gregboggs.com/blog Greg

    Thanks Lea. I’ve been making websites for years, and I didn’t realize what a user style sheet was until today.  Thanks for the great post. I can’t wait to start using tabs in my HTML again. 

  • billy bob

    “Spaces are solid. They cannot be misinterpreted. They don’t change from one workspace to another.”
    This is wrong!  TextEdit (on a Mac) vs Notepad on a PC vs DW code view vs BBEdit. Character spacing is different in nearly every editor available.

    You’ve never tried to convert an MS Word Doc to another Editor before have you ?

  • mks

    I’m with you. Smart tabs are semantic.

  • http://twitter.com/chuckneely Chuck

    I’ve always preferred tabs for the simple reason that it takes less button hits to get your cursor in the correct place (especially when editing existing code using a non-gui editor such as VIM (terminal) where you can’t point and click to place the cursor). The problem is not all the devs here use the same code editors, in fact almost all of us have a different preference. We’ve simply found it’s easier to set a standard of say, 4 spaces for tabbing than configuring common ‘tab’ behaviour across a range of editors and devices

  • Anonymous

    Tab oriented languages not withstanding, I think spaces could use a little defense here:
    1. Less space. Any distribution which is concerned about space goes through a packing process before delivery, so this point seems irrelevant.

    2. Can be personalized. Isn’t this the kind of mentality that leads project members to allow their text editor to rewrite tab/space/newline settings? Don’t you conform to the standards of the project you work in? if you merrily let your editor handle it settings who knows what kind of havoc you can create?(answer: the guy who manages the repo knows). 

    3. Better for collaboration. The best thing for collaboration is the thing which creates consistency among collaborators, a view which maps 1:1 to representation is what creates that. If you had said ‘better for balkanization of code by letting each dev set their own standards’, I’d wholeheartedly agree with you.

    4. Certain tools. I don’t depend on auto-spacing any more than you depend on auto-indention. Yes this is a useful feature which save me many keystrokes, but no I would not suddenly regret my decision. In fact often times when I’m in a remote editor or using raw text for something that I am pleased at the uniformity spaces provide. So I would say the person noodling with how their tabs look is they guy depending on a tool. My code always looks right, in any editor, all day(’till some jerk finds a bug). 

    5. Easy to select. I usually band, line or block select, so this drawback isn’t something I’d considered. If I use a select within the editor I can easily select indentation by selecting a string of continuous chars, so again… not something that impacts me. Most importantly, if I walk over to someone else’s editor under a ‘spaces’ regime I don’t see what looks like a ransom note from a third person mingling spaces and tabs without realizing it. For some reason it’s always *that* guy that wants the whole project to switch to tabs. 

    6. Spaces aren’t evil. Spaces mixed with tabs just wreck readability. completely. But on the plus side… this sort of thing leads me to refactor shitty code.

  • http://twitter.com/jaffathecake Jake Archibald

    Hits the nail on the head. Tabs are for semantic indentation, spaces are for presentational indentation.

  • http://bittersmann.de/ Gunnar Bittersmann

    +1 pro tabs. Using spaces for indentation in code is like using ‘& ’ for margin on webpages.

    Also annoying with spaces: when clicking on the beginning of a line in the editor, the cursor is anywhere. With tabs: the cursor is at a defined position.

    Spaces for aligning? I’m not sure. The concept relies on monospaced fonts. I prefer proportional fonts in my editor even for coding.

  • Anonymous

    #1 – If you are so worried about space why are you not minifying your output in production?
    #2 – This is why you have coding standards such as PEP8
    #3 – You’re counting on your editor understanding the correct tab key behaviour. If I hit tab here does it indent, or does it change focus from the textarea? I can always enter spaces. So your certain tools point works both ways.
    #4 – Every editor I have ever used has some implementation of smart-tabs to cope with this.
    #5 – So I copy code with tabs and it doesn’t fit me cause I use spaces? Can’t please all the people all of the time.
    #6 – “Enlightened coders”? Sod off.
    #7 – Tabs and spaces, mixed. Now you’re being really stupid.
    #8 – Nope not just pointless detail. For us Python programmers it is a big deal. 

  • http://leaverou.me Lea Verou

    1. Who says I’m not? I only don’t do it in sites targeted to developers, as I believe in View Source.
    2. So that everyone can be coerced into a certain preference, whereas with tabs they could have their cake and eat it too? Cool story bro.
    3. You can always copy the tab character and it’s still one keystroke. Even *pasting* tabs manually is less work than pressing the Space key four times.
    4. Not every editor does and indentation shouldn’t dictate your choice of editor. 
    5. Which is why I said the world would be a better place if everyone used tabs. You could have different tab widths and it still would fit.
    6. Go get a sense of humor, it’s on sale these days I hear.
    7. Haha, I know I’m winning a debate when the opponent starts resulting to childish insults over logical arguments. Well done!
    8. My condolences. 

  • Anonymous

    > Go get a sense of humour, it’s on sale these days I hear

    > Haha, I know I’m winning a debate when the opponent starts resulting to childish insults

    Shall I pick up a spare sense of humour for you while I’m there?

    #1 – So on these sites where you don’t minify size isn’t an issue, so why worry?
    #2 – Yes everyone should follow the same standards. Otherwise you end up with horrible to maintain code, regardless of wether it has tabs or spaces. You do understand the importance of shared standards yeah? Cutting edge or not…
    #3 – Yay for hacks and work arounds!
    #4 – If an editor doesn’t have such a basic function, it is going to have much bigger problems.
    #5 – No it wouldn’t, what about your “awesome” idea of mixing tabs and spaces? If you have different tab widths then your alignment is going to be off all the time. But a space is always the same width.
    #8 – No condolences required. I much prefer working in a real programming language. You have fun marking things up though :)

  • http://leaverou.me Lea Verou

    > Shall I pick up a spare sense of humour for you while I’m there?

    Sure, if you’re paying. More can never hurt. But I doubt you’d have the taste to pick a good one ;)

    1. It’s always an issue, just being open is a more important one.
    2. Of course standards are important, where there actually is a dilemma. With tabs, nobody has to give up their indent width preferences, so forcing a fixed indent on them is pointless coercion.
    4. I assure you, there are plenty of such editors that are widely used and adored. E.g. Espresso by MacRabbit.
    5. Firstly, it’s not only my idea, as you seem to suggest. It’s a widely accepted coding style, as is evident by both the linked articles and some of the comments here. Secondly, you seem to be failing to understand the distinction between indentation and alignment. If you use tabs for indentation only, no matter the tab width, your code is perfectly aligned. 
    8. Your ignorance shows by the fact that you think that front-end languages are limited to markup languages. Or you actually think that JavaScript is “marking things up”? I can’t decide what’s more ignorant.

  • Anonymous

    From the Espresso preferences - http://cl.ly/Doj2

    You can’t make a sweeping statement that mixing tabs and spaces is widely accepted, even the suggestion of it is likely to make a Python developer gag. So perhaps in your language(s) it is, but not all. Where-as if you spaces for indentation as well, you never have to mix :)

    I don’t think all front-end languages are markup languages, that was a joke. I do think they’re (by-and-large) horrible inconsistent messes. But I think the same about many server-side languages (such as PHP). But I don’t think they’re all markup languages. I would still rather gnaw my own arm off than spend my days writing Javascript, but I know it is not a markup language.

  • http://rachelnabors.com Rachel Nabors

    I am forced to use spaces by coders at two jobs now. I’m not sure why they don’t use tabs and auto-adjust the width. Perhaps it is an adoption issue in the opensource community.

  • http://www.bradezone.com Brade

     I appreciated this back and forth :)

    Aaron, I develop in both Python and PHP and I fully agree w/ the author. If anything, since Python depends on indentation as a language construct, it makes all the more sense to use tabs instead of spaces. As Lea said, each developer should be able to quickly specify their own indentation prefs when looking at code.

    As for PHP, I have taken over a couple of projects recently where the author apparently had no indentation standards at all. It’s an absolute smorgasbord of both spaces and tabs and it’s driving me insane >_<

  • Anonymous

    Have you never heard of PEP8?

  • http://leaverou.me Lea Verou

    > From the Espresso preferences – http://cl.ly/Doj2

    I thought you meant preferences for converting pasted indents to your preferred indentation style. Of course every editor has preferences for using spaces instead of tabs. Your point being?

    > You can’t make a sweeping statement that mixing tabs and spaces is widely accepted, even the suggestion of it is likely to make a Python developer gag.

    So? Since when are Python developers a majority in the coding world? And as Brade commented, it’s not even true about all Python developers either. I was talking about C-style languages where indentation doesn’t have meaning (like it does in Python).

    > I do think they’re (by-and-large) horrible inconsistent messes.  But I think the same about many server-side languages

    Apparently you seem to think all languages suck, except maybe Python. Maturity at it’s finest (NOT).

  • http://bittersmann.de/ Gunnar Bittersmann

    Hm, I had tried to post a follow-up… Am I not allowed to answer my own postings? I’ll give it another shot.

    The character entity references got resolved , unfortunely. What I was gonna say is: Using spaces for indentation in code is like using NBSP for margin on webpages.

    To quote Terry Pratchett: “Multiple exclamation marks are a sure sign of a diseased mind.” Doesn’t this hold true for multiple spaces as well? ;-)

  • http://leaverou.me Lea Verou

    Hi Gunnar,
    I see both “Edit” and “Reply” in my own comments, you don’t? Maybe I misconfigured something on DISQUS.

  • Anonymous

    Since when did “many” become synonymous with “all except one”? I was amused however by your comments on my maturity, followed by a large “NOT”. Pot calling the kettle black?

    And I’d like to refer you back to your earlier comment;

    > Haha, I know I’m winning a debate when the opponent starts resulting to childish insults over logical arguments. Well done!

  • http://bittersmann.de/ Gunnar Bittersmann

     I see “Reply”, but not “Edit”. I’d have to register, I guess.

  • http://twitter.com/structerer Martin

     … wow, so many words on so less content

  • Anonymous

    Spaces!! Spaces!!

  • Stephen Wotton

    I completely agree Bernhard, this would be the most comprehensive solution.

  • Yousuckalot

    You sire, are an idiot.

  • http://leaverou.me Lea Verou

    Maybe, but I’m certainly not a sir (or a sire)

  • http://twitter.com/bne Ben Miller

    At the risk of being “that guy”, I don’t care either way, just as long as everyone is using the same thing.
    At the moment I’m writing mostly Python which, if you’re adhering to PEP 8, dictates 4 spaces as recommended indentation. 
    It isn’t the spaces or tabs I value, it’s the fact that the code in our team is written in a common style.

  • http://www.facebook.com/arraymac Randy A MacDonald

    Sure, tabs are neater when you need block structures, like loops, or if you need multiple lines for your algorithm.  The ‘dependence on a pre/post processor’ argument is a light one, considering how trivial I find them to build.

  • http://www.facebook.com/people/Jasmine-Adamson/1461455741 Jasmine Adamson

    Really? What a worthless comment. It’s a little over the top for one, and completely lacking evidence for another. Your opinion is yours to have of course, but it’s completely worthless without an explanation.

  • Brian M

    At least he tried to be polite – but that didn’t quite work, clearly a bit spaced out….. !

  • Brian M

    Use to be in favour of tabs – but have gone across to the dark side :)
    The main reason is for consistency, code layouts should look the same no matter who’s screen or editor you are looking at.
    Not to mention the temptation of coders to randomly use spaces or  tabs depending on their caffeine levels is no longer an issue, so we set tabs to generate spaces -  Sorry I know this is not good for your blood pressure :) As we like to say Code shops are not a democracy they are a dictatorship………

  • Jonny Cook

    I agree 100%.

  • Spacer

    I usually prefer spaces because of formatting/alignment concerns. But your point about using spaces for that, and tabs for indenting, has really hit me.

    Since you also seem to prefer vim, is there a good way or any tricks for editing code with mixed tabs/spaces? Something as easy as setting expandtab, shiftwidth etc. in the .vimrc. That’d do it for me.

  • Frieder

    Couldn’t agree more. Great post.

  • Thaddee Tyl

    I am pretty sure I cannot convince anyone here, and I will always maintain consistency with the coding style that is currently used in the file I’m editing. This being said, the only reason I don’t use tabs is that there is no reason for a single Unicode character, in a monospaced font, to have a size bigger than another character. Tabs undermine the very philosophy of monospaced fonts. That point was not answered to in your article, hence this very comment.

    The way I see it, this issue is very much related to the Markdown vs. Github Markdown conflict. The former lets you put newlines in your paragraph in order to maintain the 80 characters limit, while the latter breaks the paragraph on each line feed.

    Independently of whether the 80-c limit is a good or a bad thing, programmers that want their lines to fit in their editing window implicitly expect everyone to have tabs of the same width. They only enter a line feed if the line they are on goes past that sacred width. Those with a bigger tab width have their line go past the limit. What should they have done? If everybody must count on their editor’s line wrapping, how should the editor know how much to indent the line, so as to please the eyes? Those issues only come up because tabs break the monospace philosophy. Imagine a world without tabs. All those issues would be solved.

    By the way, tabs were invented with certain characteristics. Tabs are not designed to look like eight spaces. Tabs are designed so as to align on the 8th slot, 16th slot, 24th slot, etc. in the 80-slot editor (each slot being able to contain one character, except for tabs, which span across slots). That’s what was called “tab stops”.

    Believing that nobody expects tabs to behave this way still today is wrong. The Go community (based around the programming language) still use this a lot, and their tabs *must* conform to the original idea of a tab (which means that code is indented by 8 slots). They are very vocal about it. (They are very vocal about hating syntax highlighting, too, but that is another issue.)

  • Kencrismon

    Glad you did this.  Your responses are spot on.  I am a space guy and your conforming to the standards is well put.  Bottom line.  I do what the coding standards of the project say to do.  Bar none, ALL coding standards in my 20+ years of doing this have been NO TABS; Sorry Mr. Atkins, spaces are to be used.

    Just my $0.02 worth but regardless of the religious side you take, a good blog and some good comments.

  • http://profiles.google.com/boctorbill Bill Cernansky

    Your arguments here are about as useful as the arguments for or against CR vs. CR/LF, with one glaring difference. Spaces always work. ALWAYS WORK. The very fact that tabs require special consideration to work across systems, editors, HTML/CSS display, user accounts, source control, etc. makes working with them a nightmare unless you are all alone in your development arena.

    You can keep your tabs and their lousy way of appearing different for every freaking user. Your position is akin to proposing that shoes only come in sizes 4, 8, 12 and 16.  And that if that doesn’t work for you, just change the size of your foot.

  • http://www.facebook.com/people/Jim-Fisher/100000137353589 Jim Fisher

    Ever wonder why the make programs to “detab” code? I have never seen one to do the reverse?  The real problem is that in my line of work, the indentation is dictated for the project by a coding standards document. Many editors allow you to define the tab points but they often revert to their default and use of another editor or printing the code or cutting a snipit to include in a document is sure to be messed up. I have terabyte HDs so no need to sweat the extra space… it prints fine, pastes into a document fine and looks the same in any editor.

  • Anonymous Coward

    I think your point of view is slightly skewed.

    2) Personalizing the layout is a non-issue as long as your repository is semantic. Even if your repository isn’t semantic, most decent repos have pre-submit scripts, which you can can fix the formatting for you, and most decent programming editors/IDEs are able to reformat code to your preferences.

    3) The best thing for collaboration is what makes every participant understand the code easier. Getting everybody to the lowest common denominator is counterproductive. If somebody has a problem with 2 spaces indents, but that’s the norm in the project, his productivity will be lowered for no benefit, at least for some time.

    4) This is a non-issue. Tabs and spaces, when used consistently, provide the same consistent view, regardless of the editor you use.

    5) What editor are you using? Thing is, a newer editor will automatically fix up the mess created by older tools mixing spaces and tabs, so that after saving the file it will look consistent across all editors.

    6) You’re right on this one. Still, making some code look readable to me is more difficult if the code uses an indent of five spaces instead of tabs.

    The purpose of code indentation is readability. Using spaces instead of tabs is like any other sort of hard-coding.

  • Anonymous Coward

    You Python programmers … I agree, the nastiest joke you can do on a Python programmer is mess up his indentation settings. But that is your choice. EVerybody likes it, although it’s obviously fragile.

  • Anonymous

    2) So you are telling me I should force scaffolding on everyone else because tabs are better? I say they’re not precisely because I need *no* management overhead, project/editor/repository settings or anything else for consistency.

    3) I suppose it’s a difference in perspective: You are focused on comprehension while I am focused on contribution, I prefer to let coding standards facilitate the contributors.

    4) Tabs mingled with spaces (for ‘code prettiness’) produces some pretty horrific layouts, if I use spaces I can make the code as pretty as I want without someone using a different tab stop ‘correcting’ this prettiness. then you’re playing repository ping-pong just tempting conflicts the whole time. WHich leads me to:

    5) if you are using an editor that rewrites your code (line-breaks, tab-stops, etc.) on file-save, you are pure evil. This create such a great rate of change in the repository(unless you force semantic repos/precommit hooks on every project you ever touch) you are almost certain to create conflicts and overwrite other’s work. I’ve heard “No, this will be better, you’ll see” only to trace down reverted bugfixes to checkins of their major feature. Sure this sort of flow *should* work, but in practice all I’ve seen is heartache.

    6) Sweet, we agree! :)

  • Statements and Blocks

    This entire argument is based on the assumption that code should sit in a file as it appears on screen. This flawed assumption causes a “broken by design” implementation that we currently have across the board, as no one has looked at the real cause of the problem (the assumption) vs just bandaiding the symptoms (various display preferences, etc), 

  • Statements and Blocks

    The interesting thing is that tabs were designed for alignment. Originally, they were tab stops, and they were at specific “distances” from the margin. 

    So the real problem here is that your editors are all broken wrt tabs, since tabs actually have nothing to do with spaces originally. (Hint, look at any word or desktop publishing program, tabs are not set by # of chars)

  • Statements and Blocks

    >>words <<content

  • Statements and Blocks

    >> words <<content

  • Dale King

    Spaces are 100% portable, tabs are not. End of  discussion.

  • http://leaverou.me Lea Verou

    tl;dr? Poor thing.

  • Anonymous

    No, it’s based on the assumption files should be edited in the format they are written. The next step down this slippery slope of symbolic code storage is being able to substitute equivalent logic structure, or maybe I layout my files or comments differently than you. maybe I like ant as my build script or maven or fabric or I just get crazy in the shell. The point is a project is an accumulation of these decisions, for better or worse and the aggregate of all these standards is a type of culture, within the code itself. To me, all this talk of I want to look at things ‘my way’ seems totally disrespectful to whoever laid down all that code. Maybe you should be pleading for developers to adopt tabs in new code or for editors that allowed you to use your own formats in translative way to minimize file rewrites and thus project collisions.

    I think your assumption is that indention layout is a trivial user preference, while I say it’s part of the project’s dialect and not something up for alteration.

  • JW

    You’ve concentrated on some irrelevant arguments (like saving disk space, who cares these days?), but have missed some of the key advantages of tabs:

    1) Tabs reduce typing effort (Yes, editors will insert groups of spaces automatically when you press tab, but cursor movement and deletions must be done one at a time).

    2) Tabs make typing consistent – inserting/deleting/moving past an indent is always a single keypress. With spaces, one keypress creates an indent, but then you need to press delete an *indeterminate number of times* to remove the indent. That means I have to watch what I’m doing and press delete until my cursor lines up with the previous line, or (3) occurs.

    3) Tabs are indivisible. With spaces it is easy to end up on a non-tab column, and then you have either messy code or waste time tidying up the indentation. Tabs eliminate this problem by “snapping” code to the correct indentation column automatically.

  • http://leaverou.me Lea Verou

    All very good points, but I thought editors mask these issues? I thought only web editors have them, due to their inherent simplicity. I’ve always used tabs all my life so I don’t know how well editors manage to hide the fact that they’re N characters instead of one.

  • ozmosys

    Couldn’t disagree more! Why infect your source code with non-printable characters, when any self respecting IDE can reformat to suit whatever coding standard anyway (we adhere to the language’s published coding standards so this is not an issue).

  • Ar Shui

     For whose who prefer space instead of tab, does you only use one editor or IDE, so you doesn’t know how painful when using different editors/IDEs?  Since NOT ALL EDITORS behave the same when dealing indent with space.

    Don’t Tell me that I should restricted to only ONE EDITOR AT ALL, IT DOES NOT MEAN SENSE TO USE A FULL FEATURED EDITOR FOR SMALL CHANGE (SUCH AS TYPO)

  • barBill

    Agree. Setting up a tab helps to avoid fighting for space at the bar.
     *Hic.

  • http://paycast.ru/paycast-39/ PAYcast 39 » PAYcast

    [...] Табы против пробелов! Холивар в прямом эфире [...]

  • http://www.blog.cyberkinetx.com/2012/02/06/indentation-tabs-or-spaces/ Indentation: tabs or spaces?

    [...] (well.. almost totally), that should convince you as well that tabs are best for indentation http://lea.verou.me/2012/01/why-tabs-are-clearly-superior/ Did you like it? Share [...]

  • Tim

    It’s trivially-easy to replace tabs with spaces. On the other hand, it is a pain in the ass to convert space indentation back the other way into tabs. Tabs are semantically more valuable than hard-coded spaces.

    Nevertheless the “expand/unexpand” unix commands already exist to do those conversions, so you obviously haven’t looked very hard.

  • Tim

    “Spaces always work”? Uhh, no, they don’t. They’re a hard-coded hack that spits on anyone who doesn’t use the author’s preference for indent widths. Spaces “require special consideration” for editors to override the tab key, the backspace key, need to implement their own indent/deindent of spaces, and most don’t even bother fixing the overhead of moving the cursor over these unnecessary characters.

    You obviously paint yourself as an exclusively-Windows user, because CR hasn’t been used by any OS for years now. Mac and Linux use LF. They also have default apps that handle both types of line endings just fine, unlike Notepad.

    Elastic tab size is a feature to allow multiple people to be happy and trade code without conversion, and you label that as a defect with your nonsensical foot analogy?

  • Tim

    When tabs are used for indents (and indents only), it’s safe to search and replace. But spaces are used in data and other whitespace, so a search and replace would destroy that data in horrible ways. Tabs are more semantic!

  • http://twitter.com/24daiting 24daiting

    Excellent article, thank you very much. We must try!

  • Richard Lopes

    At last someone that feels the same way I do about spaces and tabs ! I had to recently switch back to spaces but it cause me more trouble than anything. Will soon go back to tabs only for sure.