Tabs or Spaces?

Which do you use when coding, and why? I’ve always considered spaces to be “correct”, but I can’t really say why. Tabs certainly seem simpler: one character rather than four, even if you have your IDE configured to insert spaces for tabs. File size is another issue. Files are obviously smaller when you use tabs rather than spaces. In fact, I found they are significantly smaller. I took a ColdFusion page of what I consider to be average size (10,038 bytes), and when I replaced every occurrence of four consecutive spaces with a single tab, the file was only 6,855 bytes (it still had some unnecessary spaces in it, too, so I could probably have even gotten rid of another 100 bytes if I wanted to screw around with some regular expressions). But why does file size really matter? Although it could have a minuscule affect on the amount of time it takes to compile a CFML page, it wouldn’t affect runtime speed. Perhaps having smaller files would help your IDE to perform better, or at the very least, use less RAM. And reducing the size of source files by 1/3 might also make checking large amounts of code out from a remote CVS server much faster. What are your thoughts on the matter?

30 Responses to Tabs or Spaces?

  1. Interesting question. Here’s what WTG’s coding standard says:”General File FormatMake sure you use Unix Linefeeds if you are deploying to Unix! Dreamweaver MX and ColdFusion Studio can both easily do this. All indentation should be based on 4-space tabs – actual TAB characters are preferably to multiple SPACE characters to reduce file size and make editing easier.”

  2. darron says:

    I’m a tab person. The reason is that you can covert tabs to spaces easier than converting spaces to tabs. I like my code indenting to be 3 spaces, but some people like 4. With using tabs, I can set tabs to be 3 spaces in my IDE while other people can have 4 spaces. It allows the viewer to customize the file to their liking.

  3. Matt Liotta says:

    Many people have different preferences as to how many spaces their code is indented. With a tab, it is easy to use the same code in different environments each with our own tab spacing thus avoiding code reformating.

  4. Tabs, all the way. Darron said it best – you can make a tab look the way you like it to, without modifying the code. I literally go bonkers if I have to edit code that used spaces instead of tabs.

  5. Dave Carabetta says:

    I’m a tab guy personally. Simple, clean, no confusion.Building on your reasoning for reducing file size, this could also have a noticeable impact on your monthly bandwitch costs as well. Using Macromedia.com, which is something like 6 million hits per day (I think), 6K file size * 6,000,000 hits (vs. 10K file size) translates to a nice savings, especially in these lean times. Heck, the savings might even mean my job doesn’t get outsourced to India!! 😉

  6. I actually like 2 space indents, but every 4 space indent is a tab. so it’s a combo. Lately, though, with company standards, I’ve been into tabs, which really is too bad because it makes the code span across for miles.

  7. If datatransfers are definitely becoming an issue, there are a whole lot smarter solutions to implement, sending gzip content or local caching are just two that come to mind.back ontopic, i set all my tabstops at four characters apart, for the simple reason that there is no consistent cross-platform solution of displaying (files containing) tabs.

  8. Greg Cerveny says:

    Hey,Whats the deal with ripping off my question? You’re too late, I’ve already asked all the top cf dogs. :pAnd sean, I am eternally hurt that you answer his question without making him go search for your coding guidelines like you did to me.From MY research it appears that only really cool people use 2 space tabs, the strange mach-ii makers fanatically use 3, and old school wienies (while still technically efficant) use 4 space tabs. And only really silly people (like those who use dreamweaver without modifying the prefrences) use actual spaces.In conclusion it was not the size of the spaces that really mattered, but the motion of the tab key.

  9. Gyrus says:

    I’m not a Unix guy, but this piece by Jamie Zawinski – incomprehensible though it is to me – looks like maybe a starting point for understanding why spaces might have been given an aura of ‘rightness’ about them (even when, as we’ve seen, most people here use tabs):http://www.jwz.org/doc/tabs-vs-spaces.htmlI use tabs, for the (comprehensible, to me!) reasons stated here.

  10. Gyrus says:

    I’m not a Unix guy, but this piece by Jamie Zawinski – incomprehensible though it is to me – looks like maybe a starting point for understanding why spaces might have been given an aura of ‘rightness’ about them (even when, as we’ve seen, most people here use tabs).I use tabs, for the (comprehensible, to me!) reasons stated here.

  11. Stacy Young says:

    Unix line feeds (got burned many times 😉 and tabs.

  12. Tom Lane says:

    I am also a tab person. Glad to see such level-headedness prevail in this forum. :-)One minor drawback: They limit your ability to align multiline parameters, like so:function foo(arg1, arg2………………..arg3, arg4);IntelliJ IDEA has a nice solution to this called “smart tabs”. It uses tabs as usual but then fills in the rest of the way with spaces.By the way, what do you think of hard-wrapping (line breaks) vs. soft-wrapping (IDE wraps text to fit the view)?

  13. Bill Brown says:

    Tabs all the way. While they may not add much to a CFML page, most CFML pages have HTML in them and those do get transferred. I’ve saved a lot of space and bandwidth by changing spaces to tabs.My preference, though, is for TAB = 3 spaces.

  14. Definately TABS for me. Remember the original typewriters? Even the “glass typewriters”? (sort of computerised typewriters). Less effort – same effect.TABS for indenting – spaces for everything else. I’ve only been using CF for 9 years so I might be wrong with the web app solution.

  15. Lola Lee says:

    Two spaces, and only two spaces. Here’s where my pet peeve lies . . . say you work on cfm templates and you use 2 space (especially when this is supposed to be the standard at an company). Someone else does work on the templates to add/change stuff. That person just loves to sue TAB, and saves pages as such.Then I go into these templates and I go “whaaa . . . ” – formatting is terrible, things don’t line up perfectly. So, I turn on the hidden symbols and find that tabs were used. Muttering to myself, I detab the templates.Go back to the beginning and repeat . . .

  16. Tom Lane says:

    Lola – Setting your tab display width to 2 will align everything in a mixed file such as that.Where you really get screwed is when people who indent with different numbers of spaces edit the same doc. E.g. you use 2, but the next person uses 3.Tabs, on the other hand, allow each person to set their own personal display width freely. Thus, if you’re going to clean up a doc, I’d suggest converting in the other direction — all to tabs. Then you don’t need to mandate “2”.

  17. Tom Lane says:

    Lola – Setting your tab display width to 2 will align everything in a mixed file such as that.Where you really get screwed is when people who indent with different numbers of spaces edit the same doc. E.g. you use 2, but the next person uses 3.Tabs, on the other hand, allow each person to set their own personal display width freely. Thus, if you’re going to clean up a doc, I’d suggest converting in the other direction — all to tabs. Then you don’t need to mandate “2”.

  18. Marc Wallace says:

    Tabs.Used to do spaces (4 spaces for code, 2 for HTML). Spaces felt purer, and you didn’t have to worry when piping code to lpr that it might default your tabs to something weird like 6 spaces. There’s nothing worse than printing code that has some lines tabs, some spaces.I converted to tabs when I was on a team where we disagreed about spacing. I wrote a quickie Perl script to replace leading spaces with tabs (and purge trailing spaces), and we worked with tabs — two of us with four space indentation, two with two spaces. And public chiding for anyone who introduces leading spaces.In terms of aligning multi-line parameters, I find it’s (a) more trouble than it’s worth (if the method changes you need to alter several lines), and (b) semantically wrong. Unless the parameters have parallel semantics, they shouldn’t line up. For continued lines, I just indent two levels (to differentiate from normal blocks).

  19. Steve Bryant says:

    I use tabs for the reasons listed here already. Most important to me is the savings in bandwidth.But I am so nuerotic that I actually check the hidden characters for wasted space in most of my files (I am not saying it is a good practice, I just can’t help it). =)

  20. Marc Wallace says:

    Steve: not to worry, you’re not alone. I actually wrote a Perl script to go through source and remove extra white space on blank lines (and regularly backspace to delete it). Just think of it as a way to be kind to diff tools. 😉

  21. gene says:

    PEAR says to use spaces and it is one of the dominant coding standards out there. Here is why.”Using spaces and avoiding tabs is the only way to ensure that a piece of code is rendered consistently in all editors and viewers. Many editors render tabs as 4 spaces, and a lot of editors, terminal programs and utilities render tabs as 8 spaces…. In a community like PEAR where people use lots of different systems and editors, using tabs simply doesn’t work. People will end up doing whitespace commits fixing rendering in their editor, while breaking it for others. With only spaces it will look the same to everyone.”http://pb12.pair.com/manual/en/faq.tabs-vs-spaces.php

  22. Dave says:

    Im a tab person, its a force of habit. As mentioned above though they do limit your ability to align multiline parameters

  23. SEO Dave says:

    You forgot neither as an option.Sometimes I’ll strip all the tabs out so I can edit the code easier. thene there will be neither tabs or spaces.

  24. Buy.com says:

    I’m a tab person. The reason is that you can covert tabs to spaces easier than converting spaces to tabs. I like my code indenting to be 3 spaces, but some people like 4. With using tabs, I can set tabs to be 3 spaces in my IDE while other people can have 4 spaces. It allows the viewer to customize the file to their liking.

  25. Connor says:

    I like spaces. When other people edit my files, I don’t like re-aligning multiline parameters. It’s especially annoying for long variables and long comparisons which happen everywhere in the code and end up screwing up indenting all over the place. Also, if someone uses 4 space tabs, they’ll end their lines earlier because once they’re 5 tabs in, their 20 spaces will look like 10 spaces to me (2 space tabs). That just makes the code hard to read and ugly when printing.I work on intranet sites, so bandwidth is not a huge concern. Maybe thousands of hits a day to a site. I also do more application coding where code file size doesn’t matter at all.Also, does anyone know how to make Dreamweaver insert spaces when tab is pressed? I have it indenting with spaces, but tab still inserts a tab.

  26. Connor says:

    I like spaces. When other people edit my files, I don’t like re-aligning multiline parameters. It’s especially annoying for long variables and long comparisons which happen everywhere in the code and end up screwing up indenting all over the place. Also, if someone uses 4 space tabs, they’ll end their lines earlier because once they’re 5 tabs in, their 20 spaces will look like 10 spaces to me (2 space tabs). That just makes the code hard to read and ugly when printing.I work on intranet sites, so bandwidth is not a huge concern. Maybe thousands of hits a day to a site. I also do more application coding where code file size doesn’t matter at all.Also, does anyone know how to make Dreamweaver insert spaces when tab is pressed? I have it indenting with spaces, but tab still inserts a tab.

  27. Dave says:

    I like spaces. When other people edit my files, I don’t like re-aligning multiline parameters. It’s especially annoying for long variables and long comparisons which happen everywhere in the code and end up screwing up indenting all over the place. Also, if someone uses 4 space tabs, they’ll end their lines earlier because once they’re 5 tabs in, their 20 spaces will look like 10 spaces to me (2 space tabs). That just makes the code hard to read and ugly when printing.I work on intranet sites, so bandwidth is not a huge concern. Maybe thousands of hits a day to a site. I also do more application coding where code file size doesn’t matter at all.Also, does anyone know how to make Dreamweaver insert spaces when tab is pressed? I have it indenting with spaces, but tab still inserts a tab.

  28. I also like spaces when doing that type of design work.

  29. daveg says:

    Tabs for HTML/CF/stuff thats in HTML content.Spaces for compiled stuff.Today I spent a bunch of time converting a bunch of 25K 4-space-indent HTML files to tabs, and they wound up about 7K a piece. That outweighs any “line-up-pretty” things for me.