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?

jub0bs
  • 109
  • 5
Reactgular
  • 13,040
  • 4
  • 48
  • 81
  • OK, I found another thread on the same subject. http://programmers.stackexchange.com/questions/2715/should-curly-braces-appear-on-their-own-line Before you close this, I think this question is valid cause it impacts the daily lives of so many programmers. ;) – Reactgular May 07 '12 at 16:12
  • 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 07 '12 at 16:12
  • 2
    This Wikipedia article lists all the indent styles and their names: http://en.wikipedia.org/wiki/Indent_style – Mark Ransom May 07 '12 at 16:18
  • Changed the question to be more constructive (I think?). @MarkRansom thanks. – Reactgular May 07 '12 at 16:19
  • 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 07 '12 at 16:23
  • To rephrase you'll never get everybody to agree what's best, even with empirical evidence, because it's not necessarily best *for them*. – Mark Ransom May 07 '12 at 16:25
  • 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. – Reactgular May 07 '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. – Michael Brown May 07 '12 at 16:48
  • 2
    White space is a good thing, it enchances readiblity and comprehension. Your code style is superior. http://www.amazon.com/White-Space-Your-Enemy-ebook/dp/B003L783VW http://psychology.wichita.edu/surl/usabilitynews/62/whitespace.htm – Jim In Texas May 07 '12 at 19:27
  • I should get a badge for my first closed question. – Reactgular May 07 '12 at 20:26
  • From what I have read about human interfaces, a group (code block) should be connected, white spaces are important keeping groups separated, but the first is too loose, it does not apply strong sense of attachment. But the most important aspect is to keep proper indentation, if you have poor indentation does't matter where you put the braces. – Vitim.us Oct 10 '13 at 16:18

2 Answers2

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

  • 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. –  May 07 '12 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.

Garrett Hall
  • 2,182
  • 1
  • 15
  • 16