7

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 the following way:

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

However, 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?

11
  • 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
    Commented May 7, 2012 at 16:12
  • 2
    This Wikipedia article lists all the indent styles and their names: en.wikipedia.org/wiki/Indent_style Commented May 7, 2012 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. Commented May 7, 2012 at 16:23
  • 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. Commented May 7, 2012 at 16:48
  • 2
    White space is a good thing, it enchances readiblity and comprehension. Your code style is superior. amazon.com/White-Space-Your-Enemy-ebook/dp/B003L783VW psychology.wichita.edu/surl/usabilitynews/62/whitespace.htm Commented May 7, 2012 at 19:27

2 Answers 2

4

"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

1
  • 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.
    – user53019
    Commented May 7, 2012 at 20:01
2

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.

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