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.

Should curly braces be on their own line or not? What do you think about it?

if (you.hasAnswer()) {
    you.postAnswer();
} else {
    you.doSomething();
}

or should it be

if (you.hasAnswer())
{
    you.postAnswer();
}
else
{
    you.doSomething();
}

or even

if (you.hasAnswer())
    you.postAnswer();
else
    you.doSomething();

Please be constructive! Explain why, share experiences, back it up with facts and references.

share|improve this question

closed as not constructive by Jeff Atwood Sep 26 '11 at 3:19

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.

54  
I find the "== true" more distracting than the choice of brace placement. –  Dan Dyer Sep 10 '10 at 22:36
6  
@Dan: I think that always explicating the conditional expression greatly helps in clearness. –  Lorenzo Sep 10 '10 at 23:12
18  
@Lorenzo: I don't need to see you explicating. –  Robert Harvey Sep 10 '10 at 23:13
5  
Tremendous. I was wondering when someone would ask this! –  Paddyslacker Sep 11 '10 at 5:02
7  
So tempted to down vote everyone posting on this one just because it's tempting to call EVERYONE wrong. But then that's going to be a lot of rep I'd be giving up. –  MIA Sep 21 '10 at 3:54

37 Answers 37

I would prefer 2nd and 3rd method.

1st Method are usually from veteran programmers who learn the old stuff and get used to it. I find it very hard to read the codes last time. Luckily VS2010, make it more easily to read but what happen if you open up other editors? There will be problems.

2nd Method are more clearly define and its more easy on your eyes. You will not have much difficulties looking for the ending braces compared to the first one.

3rd Method will save more space and its clearly define its only reading one line of statement. I disagree with people about programmer taking over and making mistake. It was a careless mistake if they overlooked on this.

share|improve this answer

The first method for 3 simple reasons.

  1. It is the correct way in C, UNIX was written in that style, and I use C.
  2. It is more readable. } else { is a very recognizable shape and it nicely ties the related blocks together.
  3. I have to look after code which looks a lot like:

    if ( a < b )
    {
       a *= 2;
    }
    //else if ( ( b + 100) < a)     rev 10103
    else
    /* This adds b to a and stores the result in a */
    {
       a -= b; // rev 10129
    }
    #if 0
    else
    #endif
    {
        a--;
    }
    
share|improve this answer

Simple answer: what is easier to debug ?

// Case 1:
void dummyFunction() {
  for (i = 0; i != 10; ++i) {
    if (i <= 10)
      std::cout << "i is: " << i << "\n";
      std::cout << 10 - i << " steps remaining\n";

      // Some hard work here
      // which is really hard
      // and does take some screen estate
    }
    else
      std::cout << "We'll never get there";
  }
} // COMPILER ERROR HERE


// Case 2:
void dummyFunction()
{
  for (i = 0; i != 10; ++i)

    if (i <= 10)
    {
      std::cout << "i is: " << i << "\n";
      std::cout << 10 - i << " steps remaining\n";

      // Some hard work here
      // which is really hard
      // and does take some screen estate
    }
    else
      std::cout << "We'll never get there\n";
  }
} // COMPILER ERROR HERE

In which case did you diagnose the issue first ?

I don't care much for personal preferences (there are many other styles, including whitesmith and al.) and I don't care much... as long as it doesn't hamper my ability to read the code and debug it.

As to the "waste space" argument, I don't buy it: I tend to add blank lines between logical groups anyway to make the program clearer...

share|improve this answer

It depends on the platform/language/conventions

In Java:

void someMethod() { 
     if (you.hasAnswer()) {
         you.postAnswer();
     } else {
       you.doSomething();
     }
}

In C#

void someMethod() 
{ 
     if (you.hasAnswer()) 
     {
         you.postAnswer();
     } 
     else 
     {
       you.doSomething();
     }
}

In C:

void someMethod() 
{ 
     if (you_hasAnswer()) {
         you.postAnswer();
     } else {
       you_doSomething();
     }
}

I hate when Java guys use their style in C# code and vice versa.

share|improve this answer
1  
The C style always annoyed me. Be consistent! –  Christian Mann Nov 6 '10 at 7:51

I find the first option more readable. Never do the third as it can lead to bugs.

As with arguments over underscored_variable_names and camelCase though, the actual choice does not matter much.

Whatever the team or programming language convention is, go along with the stream and follow that convention.

If you vary from the convention it will make reading your code slightly harder, as your brain notices the difference instead of skimming along easily.

share|improve this answer

I'm surprised this hasn't been raised yet. I prefer the second approach because it allows you to select the block more easily.

When the braces begin and end on the same column and on their own line, You can select from the margin or with the cursor on column 0. This generally amounts to a more generous area with mouse selection or fewer keystrokes with keyboard selection.

I originally worked with braces on the same line as the conditional, but when I switched I found it accelerated the rate at which I worked. It's not night and day of course, but its something that will slow you down slightly working with braces next to your conditionals.

share|improve this answer

I've always gone for a middle ground:

if ( i<= 10){
    printf("hello");
    i++;
}
else
    printf("goodbye");

I find it the easiest way to recognize code blocks at a glance while not taking up too much space or making the {} stick out.

share|improve this answer
1  
really? to me the left curly brace after an if block seems visually redundant. I know that there is a block of code following the if and don't see it necessary to assign a separate line to the curly brace. The right brace at the end of the block is a little more useful as what follows won't always break the white space as you would like it to ( for instance if you have a loop within a loop). This is the best form I've found for making the conditional visually clear to me without wasting extra space. –  DanLeaningphp Nov 5 '10 at 18:57

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