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.

56  
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
19  
@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 use;

if ($test) {
    //something
} else {
    //something else
}

Because this is the way the phpcs (PHP Codesniffer) likes you to do things when running under it's default setting (which is the Zend standard).

The only other thing to note is the space between 'if' and '('

share|improve this answer

I use the first style usually just to get more code into one screen view. I never use the last it is just to easy to forget to add the {} if you add a second line of code.

Also these have a 'common' names the first one with the {} on the same line is K&R style and the second one with the {} on the next line is Allman or BDS style. (wikipedia indent style)

share|improve this answer

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

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'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.