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.

Are there any eye tracking case studies, or similar research done to verify what code format is easiest to read, maintain and work with.

I always format my code this way.

function thing()
{
    if(something)
    {
      // do something
    }
    else
    {
      // do something else
    }
}

But, I never see this followed elsewhere. In most APIs or open source projects it's done like this.

function thing() {
    if(something) {
      // do something
    } else {
      // do something else
    }
}

Can you provide an answer that is not based upon your personal preference, but is supported by research or some kind of fact?

Has there been any visual eye scanning studies done to confirm which format is best?

share|improve this question
1  
As stated this question is verging on holy-war grounds, which is not constructive. Your final bullet point might work to rewrite the question in a more constructive way though. –  Daenyth May 7 '12 at 16:12
2  
This Wikipedia article lists all the indent styles and their names: en.wikipedia.org/wiki/Indent_style –  Mark Ransom May 7 '12 at 16:18
1  
I'm afraid that even if you do find a case study, individual variation plays too large a part so the average is useless. That said, picking a single style within a group and sticking to it is important - you'll eventually get used to it, and it cuts down all the fighting. –  Mark Ransom May 7 '12 at 16:23
1  
I think, by reading the wiki reference that it does provide the "reasons" for each format style. At least I can understand in part why some people go one way rather then another. I know, we should just all program in Python. That will end the arguments, lol. –  Mathew Foscarini May 7 '12 at 16:29
2  
As a wise old relatively once told me, whichever way you choose must be the path you were meant to follow, otherwise you would have chosen the other. –  Mike Brown May 7 '12 at 16:48
show 6 more comments

closed as not constructive by Oded, Walter, Matthew Flynn, Caleb, gnat May 7 '12 at 19:21

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.

2 Answers

up vote 2 down vote accepted

"Best" is relative.

Unfortunately, I can't remember the source. The studies I have seen or were referenced indicate that consistent visual style is the key for a project. Which particular house of style you worship at is less important than consistency. See Atwood's article for some additional references, especially to Code Complete: http://www.codinghorror.com/blog/2009/04/death-to-the-space-infidels.html

The premise is that if you're performing a code review, updating a new / foreign area, or bug hunting then the consistent presentation allows you, the developer, to more rapidly perform your task. A consistent style minimizes the time spent retraining / adapting while moving around in the code tree.

Here's an article from Joel Spolsky with some additional, and I believe researched, references: http://www.joelonsoftware.com/articles/Wrong.html

Either Atwood or Spolsky will lead you down the route to Martin Fowler and Refactoring, where there are more studies / references.

This appears to be an article related to your question and "readability statistics", but after skimming it, I can't fully vouch for the quality of research. It appears they borrowed the readability concept from regular writing and tried to apply it to code writing. http://www.comtor.org/wiki/images/4/4a/2009-Spring-Breese-A-499-PD.pdf

share|improve this answer
    
Since this Q is now closed - I'll take the opportunity to tease / mock myself for pulling off separate references to both founders of the StackExchange sites (yes, I know SO was first). There ought to be a badge for the dual reference. Teasing aside, it doesn't change the relevance of my answer, and to be honest, it was Jeff's reference back to Code Complete that triggered some of the references. –  GlenH7 May 7 '12 at 20:01
add comment

Whatever format a programmer is most used to reading is the format they will read fastest in. However, the minor differences between formatting matter little compared to how clear and concise the code and design is. In other words, I would not worry about newlines vs no-newlines and worry about terrible vs good code.

But to answer your question I prefer the second method, particularly when the // do something is one or two lines. If they are large blocks then you can refactor them into methods with clear names.

share|improve this answer
add comment

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