views:

454

answers:

8

Source: http://blogs.msdn.com/brada/articles/361363.aspx

Tab characters (\0x09) should not be used in code. All indentation should be done with 4 space characters.

Can someone give me an argument as to why this would matter? Shouldn't compilers just ignore this tabs? I always use tabs, just because it's easier and indented code looks nicer.

EDIT: Right, I just thought of: Python. Doesn't the Python compiler use indentation? Do tabs/spaces matter in Python?

+17  A: 

Compilers ignore it, but it matters when programmers with different tab settings for their editors view the file.

Use of spaces for indentation ensures a consistent appearance for everyone who reads the file. Use of tabs leads to inconsistent appearance (but some think this can be desirable).

For more about this issue, see Tabs vs. Spaces: An Eternal Holy War

Kristopher Johnson
Also, I would like to add that you can have the tab key output 4 spaces instead of the tab character.
Jonathan
If you only use tabs for leading indentation then those problems don't exist. Sure, someone who sets their tab with at 2 spaces will get a different view to someone with 8 space tabs, but that's the point of using tabs. The users can keep editing alternately and still see the same indentation.
DisgruntledGoat
+2  A: 

Different text editors interpret the tab differently. Using spaces guarantees that everyone sees the same thing.

bmargulies
+1  A: 

It's not for the compiler, but rather for stylistic reasons. Alignment can get confusing if you use tabs, or mix tabs and spaces. For example, most editors let you set the width of a tab (e.g., 2 spaces, 4 spaces, 8 spaces); this isn't a problem for indentation, but if a programmer uses tabs for alignment (a big no-no) then the alignment will look different depending on the tab-width setting. Some style guideline authors think its easier to just mandate that spaces be used instead of tabs, rather than trying to convey a "use tabs for indentation, spaces for alignment" rule.

mipadi
+3  A: 

The biggest issue with tabs vs. spaces is during diffs in a Version Control System. It's VERY hard to find the real changes, when the diff file is flipping out about the white space.

MarkPowell
I think you mean diff, not VCS (since the latter doesn't necessarily do diffs, and the former is independent of the latter); also, what diff (or VCS, if you insist) actually cares about tabs vs spaces in indentation?
Pavel Minaev
The common counterargument to this is that code should be run through some sort of reformatter before being committed to the repository, to ensure consistent whitespace. However, I've never personally seen anyone actually do this. Better to have a standard for everyone to follow.
Kristopher Johnson
Any semi-competent diff program can be set to ignore whitespace.
Martha
+12  A: 

The first point is that if you use tabs, tabs and spaces often get mixed up, and it may be hard to tell one from the other - but when someone else opens the file with a different tab width setting, it's completely messed up. However, the obvious question then is, "why not all tabs?".

The second point is more subtle. Sure, all-tabs vs all-spaces doesn't make a difference for regular indentation of blocks - but there are many other kinds! For example:

while (one_very_long_function_name() &&
       another_very_long_function_name())
{
}

Or:

var customer = from c in customers
               where ...
               select c;

You can't align those with tabs unless your coding style also mandates the precise tab width - and at that point, there are absolutely no benefits in using tabs (well, okay, there is still one - it takes a single Delete/Backspace to unindent the line in Notepad or other primitive text editor). And if you publish code, you'll have to publish tab width as well, since it's not encoded in the file itself.

Pavel Minaev
+1  A: 

Different text editors display tabs using different lengths (in space units, e.g. some editor shows 2, the other 4, some even 8 spaces for 1 tab).

In VIM editor I use:

set tabstop=4
psihodelia
A: 

some editors handle tabs differently so it's worth just using the spaces so as not to come across error when moving code to a different editor

harryovers
+1  A: 

The reason why it matters, in the sense of being specified in coding guidelines instead of left to individual discretion, is that although indentation style is an eternal holy war among programmers, nearly everyone agrees that any consistent style within a project is better than mix-and-match.

camccann