To brace, or not to brace

Pages: 12
For one-liners, which do you prefer? (ignore the placement of braces)
1
2
3
4
5
6
7
8
if(stuffdota())
{
    stuffdotb();
}
else
{
    stuffdotc();
}
1
2
3
4
if(stuffdota())
    stuffdotb();
else
    stuffdotc();
1
2
if(stuffdota()) stuffdotb();
else stuffdotc();
1
2
if(stuffdota()) stuffdotb();
else            stuffdotc();
Personally, I prefer the first one, but if the length is short and not likely to change (e.g. "else ++x;") then I prefer the third/fourth.

The way I see it, it's not bad to put braces and it saves time and annoyance in the future if you need to add more statements.
Last edited on
closed account (Dy7SLyTq)
i like the second but thats cause i have python blood in me
Obviously Python has the most ideal scope mechanics of any language, but we're talking C++ here :)
Last edited on
closed account (Dy7SLyTq)
im saying if it have one command under an if, while, for, etc, i like the second because i got used to that style in python
The second one, I don't brace things unnecessarily but I don't like putting multiple statements on one line. I do for switches though because otherwise they can be really long. Example:
1
2
3
4
5
6
7
switch (expression) {
    case one:   statement; break;
    case two:   statement; break;
    case three: statement; break;
    case four:  statement; break;
    default:    break;
}
Yeah, for repetitive code that can't be abstracted, I prefer to align things nicely.

http://github.com/SortaCore/Lacewing-Relay/blob/master/Relay.hh#L422
Last edited on
closed account (3qX21hU5)
Second one for sure, first just seems like a waste of space to me and the third makes me claustrophobic. Though I do like chris's switch example.
I added a fourth, a variant on the third. It is really surprising me how many people are preferring the second - I would have though the first would be picked most since I learned that that was better from these very forums.
closed account (3qX21hU5)
Not sure how the first would be better unless your talking about style wise, which probably is true because I see more people use that style then any other.

I'm still quite new compared to most but the only advantage I see from the first one is it minimizes the risk of forgetting to make it a compound/block statement when you add more statements to it. Which is kind of trivial and a easy fix, but I am sure it happens.
Last edited on
That is exactly why the first is preferred. While it is indeed a trivial and easy fix, do you think it is trivial and easy to find the cause of a bug as a result of that? ;)
closed account (3qX21hU5)
That is true, I could imaging it would be a pain in the ass in a large project.
closed account (N36fSL3A)
Ew, coders who put the curly brackets on the same line as the function. I better run before their misguided ways rub off on me. :P
The only case I ever do that:~MyClass(){} (and even then I generally put each brace on its own line out of habit)
Second one for me.

Though it's often the case that I need to trace out a debug statement and end up putting braces in anyway.
closed account (3qX21hU5)
I do that all the time for inline functions in a class, it makes no sense to have it on a serperate line to me.

Like for getters and setters I will do

int getInt() {return myInt;}

which to me looks much better then, and is only 1 line compared to 4 which saves space.

1
2
3
4
int getInt()
{
    return myInt;
}


Or other variations.
Last edited on
@Zereo though obviously you don't actually have getters and setters, that was just an example. Right? :)
closed account (3qX21hU5)
;p true don't use getters and setter kids, and stay off drugs. Anyways ya it was just for example. Though I do see a large amount of people use them even though they defeat the purpose of encapsulation and OOP in general. Though I can't blame them because I still use them sometimes when the project is just something trivial that I am doing.
Second.
You wouldn't have issues with new statements, if you don't indent your code yourself.

> I need to trace out a debug statement and end up putting braces in anyway.
I don't follow. ¿why your debugger need those braces?


By the way, ¿what do you use to maintain that alignment?
1
2
void onChannelMessage(ChannelMessageHandler &handler); //Client requests to send message to channel
void onPeerMessage   (   PeerMessageHandler &handler); //Client requests to send mssage to channel peer 
ne555 wrote:
By the way, ¿what do you use to maintain that alignment?
I just manually space it - I haven't found an IDE that knows how to align it properly and I don't feel like writing a script for it myself because it's a rare task. The other issue is that it would be difficult for an IDE or script to align the related parts so things visually line up rather than just identifiers and syntax elements lining up.

Also, thanks for pointing out that typo (which was made obvious by the alignment, lol)
Last edited on
Second (Python Blood :D )
I usually do this:
1
2
3
void func() {
//my code
}
Pages: 12