Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Tabs versus spaces—what is the proper indentation character for everything, in every situation, ever?

I have always used tabs in all my code (C, C++, python etc.) but I was just going through the Google C++ coding guidelines(1) and I found they they say that you must use spaces and not tabs in your code.

I couldn't think of a reason why they ask that. Is it because tabs show inconsistent results across different editors or something similar?

Does anyone know what the reason is or might be?

share|improve this question

migrated from stackoverflow.com Oct 16 '11 at 5:05

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Caleb, Mark Trapp Oct 16 '11 at 5:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
1  
I does not really matter. As long as you do not mix them. So by putting it in the coding standard you force everybody to go one way. As long as everybody is the same then it works. –  Loki Astari Oct 16 '11 at 7:34
    
It does matter. If you use spaces for indentation in your makefile (you do have a makefile for your C++ code, right?) everything will break. –  Ben Voigt Oct 16 '11 at 15:06

6 Answers 6

up vote 4 down vote accepted

This is a question related to all programming languages, markup languages, etc.

It's a style choice. Period.

The upsides to TAB:

  1. Whoever views/edits it can adjust their TAB width to their preference without altering the actual whitespace.

The downsides to TAB:

  1. You can't guarantee the visual indention in all cases.
  2. If someone uses spaces in their editor as it will muck with your source.
  3. It can mess with compilers/parsers and report that something is wrong on the wrong column (because it sees TAB as a single column instead of multiple).

The upsides to SPACE:

  1. It guarantees visual indention.

The downsides to SPACE:

  1. You can't guarantee space settings are the same for all particapents (it will muck with your source).
  2. You can't guarantee that someone won't insert tabs (it will muck with your source).

Simple as that. You don't have to follow their choices. Just pick the one that fits your circumstance.

share|improve this answer
1  
Another downside to spaces: fonts vary, the spacing of spaces may be too small to visually distinguish indent levels. This can get quite bad with very small space indents (like 2) mixed with proportional fonts. –  edA-qa mort-ora-y Oct 16 '11 at 12:17

It's their code guidelines...doesn't mean you have to follow it, unless you work there. The most common justification for spaces over tabs is that it will indent the same in most editors, whereas tabs can have ambiguous spacing.

share|improve this answer

I thought that the TABS vs SPACES wars were settled some 10 years ago, say around 1999. Rather then writing a long story, let me tell you that over the years all the teams that I worked on and all the IDEs that I worked with settled on SPACES. Either as absolute requirement, or the default/recommendation. In the end it does not matter why, unless you work completely solo, you need to adjust to what you colleagues will use.

share|improve this answer
    
Is there any proof for your claims that everyone agreed to use spaces? I know its not a big deal but the claim is a big deal. I am asking because I have never heard of such an agreement. –  mtahmed Oct 16 '11 at 5:21

It may be one person's preference, or it may be because alignment is the same regardless of your editor's tab width.

(I'm a tab guy, with 2 for my editors' width)

share|improve this answer

If you use tabs, then it is up to each editor to decide how to present the source code. This would not be so bad if the code had only tabs, but eventually, a programmer in the team will work with spaces and now you have a mixture of tabs and spaces in the code. That's a big problem, the indentation will look awful in all editors that render tabs differently than the editor used by the one programmer.

Since the size of a tab varies across editors, the only way to standardize is to define indentation in terms of spaces.

share|improve this answer
2  
I don't see the logic in your last sentence. Mixing tabs and spaces is bad. Standardizing on either one is good. –  Don Reba Oct 16 '11 at 5:12

In Windows programmers' editors (in particular Visual Studio) there is a tab stop every 4th character position. In *nix programmers' editors there is a tab stop every 8th character position. The *nix tab convention is wholly impractical except for very small toy programs, and it wreaks havoc with properly formatted Windows source code.

Thus, the *nix tab convention effectively forces everybody to use spaces instead of tabs. At least for source code that may be worked on by more than one person. For example, the Boost library's coding guidelines require spaces instead of tabs, as I recall.

The advantage of tabs (with the Windows convention) is that you can add or remove small amounts of text without fixing up indentation. For example, for argument declarations. Spaces have no inherent advantages except fixing the problem of *nix tab convention, but that single lack of disadvantage is so great that one should always use spaces.

Practical: most editors have the ability to convert spaces to tabs and vice versa. In Microsoft's Visual Studio, however, only leading tabs can be so converted via the menus. Reportedly general conversion functionality is present in the beast, but by default not in the menus.

However, even Visual Studio can be configured to automatically insert spaces instead of directly using the typed Tab. It's not ideal, you don't get the "flex" behavior of real tabs. But it reduces the annoyance factor of spaces by some orders of magnitude, making it possible to live with the all spaces alternative – as is now de facto standard.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.