Tap My Mind

A Blog by Scott Isaacs

Curly Brace Placement

I’ve been writing code with curly braces for a dozen years or so, starting with C++ in college, then JavaScript for many, many years, and for the last few years, C#.  In all that time, I have been a fan of putting the opening curly brace on the same line as the declaration or control statement.  For example:

private void DoSomething() {    // ...    if (somecondition) {        myval = true;    }    // ...}

At work, though, I have been working on someone else’s code, and I always try as much as possible change my coding style to match the existing code.  In this case, that means opening curly braces get their own line, like so:

private void DoSomething() {    // ...    if (somecondition)     {        myval = true;    }    // ...}

I find that I’m actually starting to prefer the "new line" option.  But I don’t want to prefer it!  I guess maybe I’m stubborn.

I agree that it is easier to visually match opening and closing braces this way, but VS2008 does that for me anyway with clever highlighting.  Also, I don’t like how much extra room is taken in my editor window.  I like whitespace, but this seems to put it in the wrong place for me.

Sigh.  What’s a poor developer to do?

What’s your preference and why?

Previous

GiveCamp MKE

Next

RTLB #1: Always Assume Good Intentions

2 Comments

  1. I’m a pretty “green” developer, but I have a pretty strong preference towards having the curly braces start on a new line. It just looks cleaner to me. The cost in screen real-estate is worth it to satisfy my borderline OCD tendency.

    I find that carrying over to my PowerShell scripts too.

    Also, it really helps me visually inspect the level of nesting, without having to highlight one of the braces.

  2. I think indentation is enough to designate code hierarchy, so I keep my curlies on the declaring line. You save lines for actual code this way, and therefore can see more code on your screen, and need to scroll a little less often.

Comments are closed.

Powered by WordPress & Theme by Anders Norén