Code Formatting Style

A shout out to Brent Simmons regarding his take on code formatting style.

Personally I tend to agree with Brent that opening braces at the end of lines is easier to read. Perhaps this comes from the fact that he and I both spent lots of time writing UserTalk code in Frontier’s outliner.

Typically code written in the outliner didn’t have curly braces at all – code blocks were defined by indentation, though occasionally we’d use braces on a single line:

Multi-line example:

Single-line example:

But when you take the same code and copy/paste it from the outliner into a text document (or save the script to a text file), you get this from the multi-line example above:

Note the left-brace at the end of the first and third line. (The same text could be pasted back into the outliner to get the braces-free, indented version.)

Another reason I like the brace at the end of the line is that when you collapse a block in Xcode, the whole block lands on a single line in the editor, instead of on two lines. Personally I find this a lot less distracting than having two lines for every collapsed block, and being an outliner guy, I use expand/collapse in Xcode quite a bit.

Anyway, just a thought…

Ps. I totally agree with Brent that consistency is more important than the specific style rules.

2 Comments

  1. Andy said:

    To get collapse working nice its enough to have the opening brace at the end of a line. IMO its best readable that way:

    if myNumber > 100 {
    doSomething ()
    }
    else {
    doSomethingElse ()
    }

    January 12, 2013
    Reply
  2. Jake Savin said:

    Thanks, Andy. I was aware of this, though I wasn’t specific about it in my post. (Brent’s post referred specifically to the opening brace at the end of the line. If you haven’t read it, click over there first.)

    January 13, 2013
    Reply

Leave a Reply to Andy Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.