324

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.

21
  • 4
    The only reason this would matter would be if your IDE/editor doesn't support matching curly bracket recognition.
    – leeand00
    Sep 26, 2010 at 23:51
  • 7
    @leeand00: some of us still print out complex / unfamiliar code in order to study / annotate it. A good pretty-printer mitigates most of the problems though.
    – Shog9
    Sep 27, 2010 at 0:20
  • 3
    @leeand00: oh yes. Doesn't matter how big your monitor is, it can't beat crawling over a conference-room floor covered in taped-together printouts with a Sharpie.
    – Shog9
    Sep 29, 2010 at 19:15
  • 3
    sad the question is closed. After some time of indent based syntax usage I switched to (maybe weird) another braces structure. Like your first but closing brace in the last line of block. (after code line)
    – cnd
    Mar 16, 2012 at 10:05
  • 4
    Irony: "Please be constructive!" and "closed as not constructive"
    – Calmarius
    Jan 17, 2013 at 10:10

36 Answers 36

1
2
0

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.

-1

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

1
  • My exception on this one is that else and elseif do go on a newline so they aren't prefixed with a closing brace.
    – Htbaa
    Apr 6, 2011 at 14:43
-1

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.

1
-2

The main reason to use the 2nd method is that it keeps the if and the else on the same tab, which greatly improves readability.

-2

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)

-3

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.

3
  • That is pretty confusing if you ask me.
    – Toby
    Nov 2, 2010 at 20:49
  • 2
    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. Nov 5, 2010 at 18:57
  • this is like completely inconsistent :)
    – L-Four
    Mar 15, 2013 at 10:26
1
2

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