I always write if statements like this:

if (...) {
    /* do something */
}

When I create topics on stackoverflow, sometimes people change this code to:

if (...)
{
    /* do something */
}

So the first { goes to a new line.

Does it make sense, what's the true way?

Comments

Both are fine. It's just a style point, and people shouldn't really be altering your style.

Written by Mike Burton

Some will swear blind that the first style is the best, others that the second is the best - neither are right - they're both fine!

Written by Will A

there is no "true way". Its totally a personal choice for the programmer....(though programmers often argue one is better over other).

Written by loxxy

@Happy I couldn't find any of your questions where the code style had been edited by someone else. Can you give an example?

Written by bzlm

I personally prefer the first. Others prefer the second. And Mike Burton's right: no one should alter your style in your sample code.

Written by TRiG

Asking "what's the true way" with respect to bracketting style is the kind of issue developers become highly emotive about and can frequently bring about "confrontation and argument". We'll see how this one pans out...

Written by spender

possible duplicate of Is there a best coding style for indentations (same line, next line)?

Written by KennyTM

I'm tempted to change his first example, due to the irony of it. :)

Written by Nelson Rothermel

@bzlm there is one or more, don't remember.

Written by Messi

@Happy Come on, you don't have that many questions. Make an effort. :)

Written by bzlm

@Nelson, yup, one of them is definitely wrong ;)

Written by spender

I like 2nd approach. In other news, I like turtles.

Written by shamittomar

1TBS! 1TBS! 1TBS! .... uh, wait. I'm not supposed to encourage holy wars, am I?

Written by dmckee

This is not "subjective and argumentative". Happy is asking whether brace style makes a difference. That's a genuine question with a clear, objective answer: No, it doesn't make any real difference, it's just a personal preference. This question should not have been closed.

Written by TRiG

@TRiG: How do you square that with " what's the true way? " where the emphasis is in the original? People shouldn't be changing his code to conform to their stylistic expectation, but he asked for the best way. That's the brace style holy way in a nutshell.

Written by dmckee

@dmckee: Yes, he "asked for the best way" under the impression (a natural enough impression, for a newcomer, especially if people have been changing his code) that it made a difference. He was told it didn't make a difference. A simple answer to a simple question.

Written by TRiG

Accepted Answer

I'd suggest reading Code Complete. He goes into quite depth on the subject (Devoting practically an entire chapter on it), and gives what I see as a convincing argument for #1.

He actually says not to use the second one explicitly since it fails the black box test:

if (blah) 
{
    //Foo
}

When black boxed turns into:

XXXXXXXXX
X
    XXX
X

So the opening brace is seeming like the start of the block, wheras it's not (it's just the delimiter).

Instead, he recommends either the first option (opening brace on the same line), or:

if (blah)
    {
    //Foo
}

Since it passes the black box test:

XXXXXXXXXX
    X
    XXXXXXX
X

Note: I can't remember if the closing delimiter is supposed to be inside the block or not... I don't have my copy handy, so it might be:

if (blah) 
    {
    //Foo
    }

However, it all boils down to consistency. In order for your code to be readable, you need to be consistent in how you format it. Pick a method, and then stick to it. Don't switch from file to file or routine to routine. So long as you're consistent and actually format it, don't worry about it...

Written by ircmaxell
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki