PDA

View Full Version : the openening brace - a trivial C question


Azrael
March 28th, 2006, 08:13 AM
I am a (really) novice C programmer and I see two conflicting recommendations about C formatting. Some texts recommend putting the opening brace at the end of an expression. As shown by K&R:

construct (condition) {
...
}

Many others (me included) prefer it like this:

construct (condition)
{
...
}

The second I think looks a lot 'clearer' and makes missing/misplaced braces quicker to spot. So what is the advantage of the first method? Have people changed opinion about this (it seems more prevalent in the olden days?)? What do you prefer? Or do you want to flame me for wasting your time with this? 8)

bored2k
March 28th, 2006, 08:31 AM
construct (condition)
{
...
}Is the way I prefer it. It's a lot clearer and cleaner to me, and unlike my college C teacher, I'd rather have a clear and clean code that one with less spaces in it. It's even the prefered format in one of the books I like the most (http://www.amazon.com/gp/product/0672327112/qid=1143548950/sr=2-1/ref=pd_bbs_b_2_1/103-0018156-8439876?s=books&v=glance&n=283155).

Buffalo Soldier
March 28th, 2006, 08:36 AM
construct (condition) {
...
}

That sytle is called the One True Brace Style (1TBS) or K&R style. I prefer this style.

But my lecturer at college taught us the other style, the one that you (and most other people at the moment) seem to prefer.

Further reading on indent style (http://en.wikipedia.org/wiki/Indent_style).

Azrael
March 28th, 2006, 09:32 AM
Further reading on indent style (http://en.wikipedia.org/wiki/Indent_style).
Excellent! That wiki has all the information I was looking for.

So the "One True Brace Style" is actually only motivated by preservation of screen real estate, which I think has become a bit of a deprecated reason, considering most high resolution 21 century screens. Yeah, I'll stick to the "BSD/Allman style".

LordHunter317
March 28th, 2006, 10:38 AM
So the "One True Brace Style" is actually only motivated by preservation of screen real estate, which I think has become a bit of a deprecated reason, considering most high resolution 21 century screens. Yeah, I'll stick to the "BSD/Allman style".Except it uses the second form for functions, where it is most appropriate. For block expressions, I find having the brace on the second line less readable, especially when you have many levels of indentation. Too easy to loose it.

Azrael
March 28th, 2006, 12:21 PM
Except it uses the second form for functions, where it is most appropriate. Functions always start unindented on the first column, unlike block expressions, so I feel the two are sufficiently distinguished anyway.
For block expressions, I find having the brace on the second line less readable, especially when you have many levels of indentation. Too easy to loose it. The way I see it, the BSD style causes an opening brace to look like an exact mirror image of the closing brace, making it less easy to loose. We're all wearing slightly differently colored glasses I guess..

LordHunter317
March 28th, 2006, 01:47 PM
Functions always start unindented on the first column, unlike block expressions, so I feel the two are sufficiently distinguished anyway.In C sure. But not in most Algol-derivatives: Java, C#, C++, etc.

rplantz
March 28th, 2006, 06:36 PM
In his C++ books, Cay Horstmann uses the style:

int main()
{ cout << "How many pennies do you have? ";
int pennies:
cin >> pennies;
---
}


I don't like the way this looks.

After 20+ years of reading/trying to read student programs, I got used to seeing many different styles. When I first started teaching, I tried to enforce my own style. I then realized that some students' styles looked better than mine. I became much more flexible.

I believe that documentation is one of the most important facets of a program. You can implicitly document your code by (1) being consistent in you indenting and braces placement and (2) using descriptive variable names.

#2 is where I have the biggest problem with K&R. I've always assumed that they used keyboards that gave an electric shock every time they pressed a key. :)

red_Marvin
March 29th, 2006, 06:11 AM
Looking at the wikipedia page I find that the I use Whitesmith's style,
but GNU style seems good too, especially since modern IDE's let you specify
if the tab key is to represent a tab or a user specified number of spaces.