Code Indentation: Tabs vs. Spaces

One of the discussions that inevitably pop up from time is the spaces versus tabs discussion, for indentation of code. Should you use tabs or three, four or five spaces to indent your code?

The people who use spaces usually give as their reason that your code looks the same in every editor, on every PC. That’s great, but is that an advantage at all? Those same people also have lengthy discussions, after they decided to use spaces for indentation, about how many spaces to use. “Shall we use just 1? I think that’s enough.” “Nah! Are you crazy!? Three is good.” “No, four!”

Wouldn’t it be great to have some kind of magic kind of space whose width was specifyable, so that everybody can use the width they prefer? Hmm, oh wait, that’s a tab!

All editors I know, allow you to set the width of a tab; do you think 1 spacing is enough, or do you prefer 8? Just set it in your editor.

People prefering spaces make no sense. Weird thing, though, is that I use spaces myself too. But that’s just because I’m too lazy to figure out to turn off the turn-tabs-into-spaces option in vim. And to set the tab-width to 3 (three rules!).

  • Dick
    Stylistic code is so lame.

    "My code was hard to write, it should be hard to understand" Plan 9 Kernel
  • 1. How will you control the length of your line if you can't control the width of your tabs? Ideally, a line should not exceed 80 characters (historically but also handy when printing code). If your have 3 tabs on a line and a tab width of 2, that gives you 6 'spaces' and thus 74 characters left. If, however, you use 4 'spaces' for tabs or even '8' as is usual in the *nix community, you get 18 spaces and thus only 62 characters left on that line.

    2. Not all editors allow you to define the width of your spaces. Notepad is a good example. Also, who says you're always working on your own PC? What if you're working on an editor you're not familiar with? Are you going to spend half an hour looking for the tab-setting so you can go back from, say, 4 spaces to your preferred 3?

    I never heard a good argument in favor of tabs. The most common and lame one is 'I have to delete n more characters when editing code'.
  • I use 8 spaces in my tabs. ^_^
  • Joost
    I like the "Tabs to spaces" and "Leading spaces to tabs" functions in EditPlus. When you like tabs you transform the spaces into tabs, when you like spaces you turn tabs into spaces.
    Personally I use tabs with an indent of 2...
  • The only problem with spaces and tabs is, when tabs and spaces are used together. You just need a good coding-standard and all will be fine I guess. Tabs or spaces dont really matter if everybody sticks to the same thing.

    I'm a spaces guy myself. Only problem I get is that I have to use ':s/^/[4 spaces]/g' instead of ':s/^/^I/g' in vi. Old habits die hard
  • I was just converting back to spaces because it doesn't look properly in some editor, but you convinced me. Tabs rule! (I prefer to set tab length to two spaces. :))
  • I prefer tabs. Less backspacing to do when deleting.
  • @Thomas
    The issue isn't having to add spaces, for that is what has to be done, but they won't align anymore when tabs are of different width on another setting ;-)
  • I've always used tabs for indenting and spaces to align code in anything I do. The only exception is in html when coding tables because indenting can get out of hand very quickly, I use 2 spaces for table rows, 1 space if I am using thead/tbody tags.
  • Thomas
    @Bas:
    Ideally, in your case the editor should choose to indent with tabs up to the indentation of the previous line, and then continue with spaces. However, I don't know of any editor that is that clever... if any editor can, it will probably be called emacs ;)
  • The problem with tabs is that they when having a different length do not outline properly...

    int[] a = int[] example{ 1, 2
    3, 4};

    Some people like to have the 1 and 3 in one column for a better layout.. with tabs this can't be done properly
blog comments powered by Disqus