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.

I was reading a Vim tutorial ( http://www.oualline.com/vim-cook.html#drawing ), and came across this:

This is very useful if you use a 4 space indentation for your C or C++ programs. (Studies at Rice University have shown this to be the best indentation size.)

Is there any truth in these studies?

Note-- i didn't mean for a flame war in indentation -- just whether anyone else has come across tis study before? EDIT: @MaR I made a poll http://poll.fm/3d5kg

share

migration rejected from stackoverflow.com Nov 7 '13 at 9:43

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as primarily opinion-based by MichaelT, Michael Kohne, gnat, Kilian Foth, Bart van Ingen Schenau Nov 7 '13 at 9:43

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
I've been using 2 spaces for a long time, so no, 4 space is definitely not the best for me as it requires adaptation –  stijn Nov 8 '11 at 7:32
4  
@stijn, it would save you time in the long run: xkcd.com/974 –  avakar Nov 8 '11 at 7:33
4  
Now if only the studies at Rice could also tell us if tabs or spaces is better, we'd be all set… –  Thanatos Nov 8 '11 at 7:36
3  
It seems to me that 4 space indentation combined with 80 column limits is a conspiracy to create uncomfortable code. –  Pubby Nov 8 '11 at 7:51
2  
We really need the actual paper produced at Rice (if there ever was one) to make a proper answer, otherwise we are just guessing, stating preferences –  jk. Nov 8 '11 at 8:12

7 Answers 7

up vote 16 down vote accepted

There is no single best tab size, period.

Larger tab sizes means better visual separation, but you'll be using more horizontal screen space. Conversely, smaller tabs reduce visual separation, but allow for more code per line, and produce smaller 'jumps' that could otherwise disturb the reading flow.

Personally, I find that 3 or 4 spaces is the minimum size that produces sufficient visual separation; 4 is a safe bet, 3 works for some fonts, but not all. Others might disagree; some people use 2 spaces and are perfectly happy with it.

If the tab size is too high, visual separation doesn't improve, but the longer jump reduces readability. 8 spaces is probably the maximum that can be read comfortably, and I have never seen anyone use more than that.

So anything between 2 and 8 is fair game. If you work on code that needs deep nesting (say, C++ - a simple inline method with two levels of nested loops in a namespace uses five to six levels of indentation already), you'll want to lean toward smaller indentation; if you have a large monitor and can afford long lines, you may want to go large; etc. Generally speaking, 4 and 8 seems to be the most common choices (probably because programmers like powers of 2), with 4 being more popular than 8. (On a side note, the official Python style according to PEP-8 prescribes 4 spaces).

Just find something that works and use it.

share
1  
Tab size and indentation level are not necessarily the same thing. If you use only spaces for indentation, tabstop settings are irrelevant. –  Keith Thompson Nov 8 '11 at 8:28
2  
... probably because programmers like powers of 2... +1! –  Valmond Nov 8 '11 at 8:49
    
@KeithThompson: Obviously I'm using the terms 'tab size' and 'indentation' interchangeably in this context, 'tab' referring to the general concept of whitespace at the beginning of a line, rather than the actual ASCII tab character. Whether you use actual tabs or series of spaces for indentation is an entirely different discussion. –  tdammers Nov 8 '11 at 10:28
    
@tdammers: Ok -- but it's not obvious. I've worked on code that only looks right with 4-column tabstops, for example. –  Keith Thompson Nov 8 '11 at 10:40
    
@KeithThompson: I know what that's like; mixing tabs and spaces, or using tabs inside lines, is plain out evil. But since this is not the topic of this question, I still think it's obvious what I mean in the given context. –  tdammers Nov 8 '11 at 10:55

Without finding a paper published from these studies at Rice we can't really comment on the claim (shame the vim cookbook doesn't reference it). After several searches I can find a few more casual remarks about this study, but no proper citations and no paper.

For modern C++ I'd certainly use indent of 2 as the long lines now common from template parameters benefit from the extra horizontal space, it's possible the Rice study wouldn't have taken this into account (but without seeing a paper we will never know). xml I also indent by 2 for similar reasons.

For other languages I tend to use the editors default, which mostly is 4 (which lends some anecdotal evidence to 4 being good)

share

Programming style is like "taste": very subjective.

Studies are based on "statistics". They keep a number of programmer and ask how do they code, then keep another number of programmers and ask them how do they feel reading some code.

Although it can be true that the majority of them feel more comfortable with 4 spaces it doesn't mean that you have to be comfortable with it.

Find yourself your comfortable style: something that look "pretty" to your eyes, and makes you able to focalize the "important things", like where something begins and ends, and is "resilient" to mistakes (intending that makes coding mistakes to look "odd", hence easy to be found) and be coherent with it.

There are lot of editors that allows code reformatting with various styles and parameters. If I have to rework your code and I find it hard to read, it's not a problem form me to reformat in another way, more suitable to my eyes.

share
    
Reformatting makes "blaming" useless. It becomes much harder to know when a line was introduced when trying to understand why it's there. –  Matthieu M. Nov 8 '11 at 7:51
    
The problem with reworking the code would be that it is very hard to track changes. Every programmer changing the formatting - go figure in the source control diffs why lines moved around, and what exactly changed. It's not only the white spaces, its also the brackets conventions, the order of appearance, comment styles, etc. I find it much more comfortable for me and others to have the style defined, at least on some basic level, as part of the coding standards, and enforced (even by automatic beatufy on checkin). –  littleadv Nov 8 '11 at 7:51
1  
@littleadv: That was exactly my point. If you are in an "organized environment" (where it is clear who are all "you and the others") there can be an a-priori defined standard everyone must agree (and hence makes no sense to discuss it after word. You can negotiate it when enstablishing, but not anymore further). If you are not in an organized environment, then you don't kow who and where the "oters" are. You are just the one that come after them (and may be before somebody else you'll never know). So makes more sense "understanding the flow", that "understanding when this or that was done" –  Emilio Garavaglia Nov 8 '11 at 8:46

Together with indentation, comes the complexity of the program.

From my own experience, whenever I find myself indenting too far, I know that it is time to break things in smaller parts, possibly by changing the design.

About indentation itself, usually I work on projects where it is specified in coding rules document. I have worked with 2, 3 and 4 spaces and I have no rational argument in favour one any of these values.

Anyway, I never indent with tabs because in a team of, say, 3 developers, at least three editors are used and at most one developer knows how to configure its editor for tabs and indentations. The result is a code that is correctly indented on one workspace and is unreadable on the others.

share

I find 4 spaces indentation the most comfortable.

But it really depends on the person - your preferences, what you're used to, and also on the size of the letters on the screen and the size of the monitor.

I wouldn't claim that there's a single value that is perfect for everyone, but 4-spaces seems to be a rather convenient compromise.

Keep in mind that you're writing a code that other people would be reading. People like me for example, the four-eyes:-)

And as I commented on the other question: I find it much more comfortable for me and others to have the style defined, at least on some basic level, as part of the coding standards, and enforced (even by automatic beatufy on check-in).

share

Space size is going to be pretty simple. More spaces makes the program harder to read by virtue of consuming more space on the screen. They also make the program easier to read by making the indentation differences more exaggerated. Therefore, the optimum space size is going to be the minimum space that most, if not all, programmers can clearly distinguish in a small amount of time. That sounds fairly measurable to me.

I've never actually see any study on this, though.

share

For a while I have, in C++ I have used 3 spaces as my coding style.

2 spaces doesn't seem obvious enough to me when looking at large screens of code.

On the other hand I find 4 spaces to be too much indentation, you are wasting horizontal space which is already fairly limited due to 80 column limits most coding styles also use.

Then again you shouldn't be using too many levels of indentation and if you are things like loops and if-blocks should be extracted out into smaller functions. Having 4 spaces might make it more obvious which code should get the snip.

Some of it will depend on other factors such as how horizontally noisy your language is and if it uses a lot of indentation. C++ can use a lot with namespaces, classes, functions, if/while blocks and so on. C not having much scoping would use less.

For example your brace style. If you put braces on the same line or you give braces their own line:

With same-line braces and 3 spaces the extra indentation helps show the nesting.

if(isValid) {
   doSomething();
}

With braces and 2 spaces, the braces already make the nesting obvious:

if(isValid)
{
  doSomething();
}

I really don't like braces-with-their-own-lines. It seems like a massive waste and is ugly. I actually use a more complex rule. It's "Braces should be on the same line UNLESS the line was split due to column width.". This is because it helps with indentation soup but it might be to complex a rule to enforce and could be non-obvious to people jumping on a project:

inline std::vector<std::string> getTheVectorOfStrings(const std::string& query,
                                                      const std::string& identifier,
                                                      const int max) const
{
   std::vector<std::string> result;
   result.emplace_back(getQuery(query, max));
   removeIdentifier(result, identifier);
   return result;
}

Having said that, personally I'm thinking about switching to 2 spaces as this seems to be the norm for C++. Google, Chromium, Mozilla, all use it, LLVM standards just says be consistent but seem to be defaulting to 2 spaces, Webkit does prefer 4 though.

2 isn't too bad visually compared to 3 spaces and saves more on horizontal indenting. I haven't seen 3 space indents used in the real world.

share

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