Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At the company I work for, we're trying to standardize our development practices, and we want to make the right call regarding indentation.

Is there any empirical evidence (preferably research articles in HCI or something) which demonstrates the effect that whitespace can have on code readability?

Alternatively, I'd be interested in a study which evaluates various styles of indentation in code for use in a team environment, across editors, screen resolutions, and operating systems.

share|improve this question

closed as not constructive by Walter, haylem, Bryan Oakley, Bernard, Robert Harvey Jul 19 '12 at 20:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

Why don't you just ask all your developers what they find easier to read? It seems like you're looking for research to convince yourselves about something. Just form an opinion one way or the other and vote on it. – Kris Harper Jul 19 '12 at 12:36
The only fair thing is to use a random number: :) – StuperUser Jul 19 '12 at 12:38
Is there any evidence that stawberries taste better than blueberries? – Bryan Oakley Jul 19 '12 at 12:41
I've always used 3, which sounds like a fair compromise in this case – Drake Clarris Jul 19 '12 at 13:01
+1 for the question asking for "empirical evidence". -1 for anyone avoiding that point and merely furthering the already well-established eternal war about the size of the indent and tabs vs. spaces. – minnow Jul 19 '12 at 15:11

A similar question was asked in this question where the asker is looking for a citation for a supposed study done by Rice University declaring 4 to be the best. Though I couldn't find the study, I did find another article referring to that same supposed study.

The poster with a similar question created a poll. This poll can be found at:

At the time of this posting These are the standings:

4 spaces   52%   13 votes
2 spaces   20%    5 votes
tabs       24%    6 votes
Other      4%     1 vote

I did, however, find a study where they explain a lot about readability but never say how many spaces is preferred. They do mention how programming defects and other problems may arise when no whitespace is used.

share|improve this answer
It wouldn't let me put the other references as links because I'm a noob. if someone wants to edit it so they show as links I would appreciate it. – CEPA Jul 19 '12 at 13:20
+1 for trying to find empirical evidence. -1 for creating a non-empirical opinion poll. Net 0. – minnow Jul 19 '12 at 15:13
Thanks for the reference to the article. I'm hoping to find a few more sources, but for now, this is the best answer we've got (although I agree with @minnow regarding the poll). – Glen Balliet Jul 19 '12 at 15:27
I didn't create the poll, I just found it and thought it was relevant (though, true, it isn't empirical). – CEPA Jul 19 '12 at 18:55

Code Complete 1st ed. cites an article which summarizes several studies on this. Apparently

  • 2-4 spaces was found to be optimal. The implication is that there's no measurable difference between 2 and 4.
  • 6 is measurably worse than 2 - 4.
  • Code without indentation is measurably awful. You probably could have guessed that.

Conclusion: flip a coin and get back to work :)

Program Indentation and Comprehensibility by Miaria et. al Communications of the ACM 26, no 11 (November 1983) p861-67.

share|improve this answer
Seems like 3 spaces is the optimal then since it's in the middle of the interval :) – HelloGoodbye Dec 20 '15 at 19:25
Readability is definitely better if scrolling is required lesser. I prefer 2 spaces due to compactness and with 100 character max column width I need not have to do line wrapping more often, also reducing the number of lines of code in a file. – tarun_telang Jul 16 at 3:55

From what Jeff Atwood thinks, it doesn't matter as long as your whole team uses the same style. The study he quotes is from Code Complete, an excellent book.

share|improve this answer
He does cite some studies from Code Complete. But, the studies he cites are not about indentation size, and they don't support Jeff's conclusion either! They say it does matter how code is laid out. But there is a study about indentation size cited in Code Complete, an excellent book which I actually bought and have here in front of me. See my answer. – MarkJ Jul 19 '12 at 20:52
-1 for the Jeff-Atwood effect, and I actually don't think it's important for everyone to use the same style. Coding style variations can even be a clue for who wrote the code, and therefore how they think, which can help readability. The style problems are with the outliers, not the normal range of variation. – Steve314 Jul 20 '12 at 7:06

Avoid the issue: standardize on tabs for indentation at the start of a line. Then everyone can be happy. Each developer just sets their editor to display a tab size that they find the most readable.

I would question the value of any research because so much depends on IDE, display, font style and font size. Also on the language you are using: some languages tend to be more tabular (table-like), others tend to have deeper indentation. And individuals vary too. For me, the optimum for C++ is Cambria (proportional font), tab size set to 9 or 12 depending on whether I'm on laptop or desktop.

share|improve this answer
-1. I have never seen this end well. – Daenyth Jul 19 '12 at 14:43
Using tabs is problematic too - or rather the inevitable mix of tabs and spaces. Don't forget vertical alignment, though if you use a proportional font I guess it's not something you worry about. IMO, its very easy to adapt to quite a wide variation of indent sizes - at least as easy as adapting to other style variations. My point here is not to say you should do this instead, of course - just that you're not going to make everyone happy whatever you do. – Steve314 Jul 19 '12 at 15:11
We have decided not to standardize on tabs for a couple of reasons, mostly because, given the developers, operating systems, and editors in play, it's impossible to know how tabs will be rendered when opening a source file. Here's an article which discusses the interoperability issue in greater detail. – Glen Balliet Jul 19 '12 at 15:25
Tabs (and proportional fonts) don't work well with more sophisticated alignment of continued lines. – kevin cline Jul 19 '12 at 15:28
I find it curious in some ways that there isn't any "standard" means of using a rich-markup text editor to write source code and then only the executable parts fed to a compiler. That would allow tabs to be used in semantically meaningful fashion to line things up mid-line. Given that does not exist, though, I find tabs to be more trouble than they're worth. – supercat Jul 19 '12 at 16:07

I've never seen anything except personal opinions on the number of spaces and the spaces vs tabs question. I've also never worked in a team where every team member agrees with the implemented choices. And I've seen just about every variation of them.

My personal preference is to use tabs for two reasons:

  1. It can mean a lot less characters typed when entering and moving text around.
  2. It means that each individual developer can then set their own personal choice for the number of space characters that a tab represents.

Of course, a lot of this has to do with the IDE being used as well.

share|improve this answer
I'll just quote Joachim Sauer's comment on perdian's answer: "While I tend to be of the same opinion, the problem is that lack of evidence is hard to prove ;-) Making this answer an opinion more than anything else." – minnow Jul 19 '12 at 15:19

I don't think there are any hard facts available at all.

It seems to be more of a religious thing than anything measurable.

share|improve this answer
While I tend to be of the same opinion, the problem is that lack of evidence is hard to prove ;-) Making this answer an opinion more than anything else. – Joachim Sauer Jul 19 '12 at 11:57

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