32

Possible Duplicate:
Are spaces preferred over tabs for indentation?

Why do most coding standards recommend the use of spaces instead of tabs? Tabs can be configured to be as many characters wide as needed, but spaces can't.

example:

Pear manual:

This helps to avoid problems with diffs, patches, SVN history and annotations.

How could tabs cause problems?

14 accepted

I know the biggest reason I prefer spaces to tabs is because, with spaces, you use the same character for both indentation and alignment. Take the following piece of code:

void do_something(void)
{
    int x = get_an_int(var1,
                       var2,
                       var3);
}

If you're using tabs for indentation, you have to lean on the spacebar in order to align the function parameters -- if you use tabs to line items up, then your alignment is dependent on your tab setting (i.e., if you use 8 spaces per tab but I use 4, then things won't be aligned properly).

However, if you use spaces for indentation, then you don't have to think about it -- you can use spaces for both indentation and alignment. And any good text editor will insert {2,4,8,whatever} spaces whenever you hit the Tab key.

10

How could tabs cause problems?

Neither tabs nor spaces cause problems, per se (or at least, neither causes more problems than its alternative). What does cause problems is inconsistent usage of tabs and spaces. By picking one and sticking to it, you eliminate all such issues.

The real solution that would solve everything is something like elastic tabstops, but this has its own unique set of problems.

6

Tabs wouldn't be bad if used only for code indentation. The problem comes when you start to use them to align things around, e.g. comments to pieces of statements, comments with comments on another line, etc, so, when someone reads your code in an editor with a different tab width settings all the code becomes messed up. Moreover, if some people use tabs and some other use spaces (maybe inadvertently) what looks correctly indented and aligned to someone will be a mess for another.

So spaces are more used because they look the same every where, e.g. if a source is indented and aligned with spaces it will look the same everywhere.

5

Tabs, when used incorrectly, cause a lot of trouble. In an ideal world people would use them correctly and all editors would be configured correctly, but unfortunately indentation is usually not taught on programming courses and many editors have problematic default settings. Some editors cannot be configured to behave correctly at all (in presence of tabs for indentation and spaces for alignment) because they insist on converting any sequence of 8 spaces into a tab character.

It is easier to simply disable tabs and only deal with one type of whitespace, avoiding those invisible problems.

Still, I would not say that the majority of projects deny tabs. Many do use tabs incorrectly (most GNU projects) and a lot also use them properly (e.g. most projects made with Visual Studio, as that seems to smart indent properly by default).

The big benefit of using tabs is that the width is fully configurable and coders do not need to agree on a particular width. For space indent there are styles with 2, 4, 8 and other indentation widths, which disallow simply copy&pasting code from one project to another.

3

Tabs are problematic when you are viewing the code from multiple applications. For example, you may look at code in an IDE but also from a version control system's DIFF application. Also spaces are always indented at the same level, whereas each application may indent the tabs differently - although as you said, you can configure this.

2

While tabs may be configured to any number of spaces, each developer may select a different number. To align text a variable number of tabs would thus be needed. If you use spaces the result is the same for everyone and you don't have to agree on a common setting for tabs. One less issue to worry about.

EDIT example to address comment

lets say I want to align some code like this (I want to strings to align)

var texts = new [] { "first",
                     "second"
                   };

then all need to share the same settings for tabs for that to align (and you probably have to mix tabs with spaces). Using spaces the code above will look alike for all devs in all tools.

Fortunately most editors can be configured to automatically expand tabs to spaces, so it is easy to avoid tabs.

2

The biggest problem with using tabs instead of spaces is that whitespace is invisible so it's very hard to tell whether or not you accidentally used spaces instead of tabs at the beginning of certain lines. When you are doing a lot of cutting and pasting and rearranging text in the editor, it is easy to accidentally use spaces instead. You will never notice the problem, but someone using a different tab size will have a hard time reading your code.

The only way to really use tabs consistently is to run astyle on your code occasionally or have it run automatically by your version control system. This can fix up your tab/space problems when you accidentally mess it up.

Alternatively, if you still really want to use tabs instead of spaces and you don't want to run astyle, consider turning on the "show tab character" feature of your editor so that you can tell when you accidentally use spaces instead of tabs.

2

This link has an extensive discussion of the issues: http://www.jwz.org/doc/tabs-vs-spaces.html

Conclusion?

So go forth and untabify!

Of course, a loooong time ago, I tried to implement this philosophy (of canonicalizing everything to spaces) on an existing project, and just about gave the project manager a heart attack when the size of the code in question nearly quadrupled. :)

0

I would say that you should choose to use one or the other. I prefer spaces as when you copy and paste code around (especially from web browsers) the tabs often end up getting replaced with spaces. Then you end up with a mixture in your code and it may not be obvious. This causes the diff issues you mentioned.

0

For me, I use spaces because what you see is what you get. If it looks like it's indented 4 characters, it's indented 4 characters and that will never change no matter what settings you play with or what you look at the file with. If you use tabs, you don't really know what's a tab and what's spaces. If you simply don't have any tabs in your files at all, you never have to worry about what your whitespace is made of.

0

IMO it's preference issue, and arguing about one or other superiority is asking for a flame war, it's like with where to put {} braces.

I like spaces because I do not have to configure every editor/viewer I use, it looks the same in notepad, VS, beyond compare, etc.

0

Using spaces gives me more control.

Also, different editors display tabs differently - for example Notepad uses 8 spaces to display a tab, while VS uses 4 (by default, but it can be changed).

0

By far the most damaging issue is when tabs and spaces are both used - usually inadvertently. The result almost always looks terrible. My first move often on viewing a file in vim?

set list

My second

:%s/\t/    /g

(OK a bit generalist but it usually doesn't let me down.)

This usually straightens things out. One thing to watch for is when you do this in a version controlled environment and then check in - you'll often end up masking your true changes with whitespace. True, you can get round this with diff options but not everyone will pick up on this and you'll get a lot of puzzled faces.

The best approach I've come across is to use tool like astyle to do the formatting along agreed lines. That way, you just agree a certain style, implement it as a resource file and just run it on your files. No-one has to set there lining things up etc. It works a treat.

0

The tab character is an invention of the devil. One day there will be a Punchtape Letter about it. Right, Neil?