Braces Controversy
Attention coders!
In the past few days there has been much controversy stirring up here on Bamburgh Circle regarding the proper use of braces while programming.
Ever since I can remember, whenever I use braces, I always leave the opening brace on the same line:
for(int i = 0; i < 10; i++) {
//do something
}
However, some people insist on bringing the opening brace down to it’s own line:
for(int i = 0; i < 10; i++)
{
//do something
}
So, my question to you other programmers who read this: Which style do you prefer?
Personally, I find leaving the opening brace on the same line makes the code look nicer. The main opposing argument is that bringing the brace to it’s own line allows the programmer to see what’s inclosed in the given block. But I figure that if you are using a good enough IDE, this shouldn’t cause too much of a problem.
Oh yeah, Happy Friday the 13th!
The first is classic C K+R style, the second is the more modern C++ / Java style. At work, we did a survey, and all the pure C guys used the first, where as all us C++ guys used the second.
I find the problem becomes function definitions.
because, in c, you’d have
void foo(int a, int b, int c) {
}
but in C++ you could have
void MyClass::foo(int a, int b, int c) const throw std::logic_error, MyOwnException
{
}
And it makes it look cleaner tossing the { down one more line
Same as java
public void foo(int a, int b, int c) throws IOException, ClassCastException
{
}
Or at least, thats how I feel about it. I always use the latter. Whitespace is a good thing.
Comment by Derek — February 13, 2004 @ 9:43 amWell, I’m kinda a pseudo-coder, but when h4×0r1ng somebody else’s code, I tend to like the second approach… it’s easier to see where a code block starts and ends, especially if you have a situation where you have nested brackets.
Comment by Andrew — February 13, 2004 @ 12:07 pmI don’t know what this is about, but I think the second one looks better
but what I really want to ask is: I go to the page www.tvor.net/~erin but it’s blank, dude, where’s my blog?
Comment by Erin — February 13, 2004 @ 4:45 pmI guess I’m with Andrew on my status as being more of a “pseudo-coder”, but for me it depends on what I’m doing, and whether or not whitespace is an issue. A good example is Java vs. JavaScript - in the former, the more white space the merrier, because it’s all going to get compiled away in the distributable copy, but in the latter, I feel more efficient if I use the brace-on-the-starting-line approach, because that’s one less cr/lf that people have to download.. Hmm - this all sounded alot more rational before I wrote it down.
Comment by The Peruvian Llama — February 13, 2004 @ 6:03 pmI noticed that it was blank too. Seems like Movable Type messed up when it rebuilt the page after Felicia added her comment. It’s back now.
Comment by Trevor — February 13, 2004 @ 6:03 pmErin, although you have no CS background, you hit my point on the head. I like to be able to match myt brackets up…I like to be able to look at some code and see where things are. I like the asthetics of the second example. And, come on, can Cay Horstmann be wrong?
Comment by Omallley — February 16, 2004 @ 12:12 amSorry Trev,
I still think your neat
Comment by Omallley — February 16, 2004 @ 12:13 am*sniff, sniff* Don’t bother with me, guys…I’ll just sit in my corner and code my own way.. *sniff*
Comment by Trevor — February 16, 2004 @ 9:29 amDon’t worry, Trevor, I prefer the first way too.
Comment by Andrew G. — February 17, 2004 @ 9:51 amSorry Trevor, I gotta go with the crowd - braces have gotta be on their own lines. =/
Comment by MEAT — February 18, 2004 @ 4:25 pmNote that in an old school new school battle, someone who uses links as a prefered broswer may not be a great help *cough* andrew g *cough*
Comment by Derek — February 19, 2004 @ 7:29 pmLinks? Wouldn’t even dream of putting it on a machine! Now, Lynx on the other hand…
Comment by Andrew G — February 21, 2004 @ 12:20 am(I suppose I probably shouldn’t dig myself a deeper whole by mentioning what I learnt assembler on…)
Comment by Andrew G — February 21, 2004 @ 12:21 am