Quick survey on curly brace positioning
I absolutely don't want to get into a debate over whether it is better to put curly braces on their own line or not, but I do have a question on indentation I'd like to throw at the masses (ok - all 7 of you).
Generally, the curly brace debate boils down to these two examples:
if (var < max) {
DoSomething();
}
vs.
if (var < max)
{
DoSomething();
}
I recently came across a body of code that uses a variation on the second method – but to me is far less readable. Am I missing something? Does anyone else favor this format?
if (var < max)
{
DoSomething();
}
Comments
- Anonymous
February 12, 2005
You forgot the standard:
if (var < max) {
DoSomthing();
}
with the last curly brace matching the column of the conditional. - Anonymous
February 12, 2005
agreed. I see that as often as the first - but I don't care about those today as much as the last two. - Anonymous
February 12, 2005
Whatever the convention, it must have indenting to be readable and more important than the bracketing rules, it must be done consistently. - Anonymous
February 12, 2005
Looks like emacs-style brace formatting. (Blech) - Anonymous
February 12, 2005
I prefer this (compact and readable):
if (var < max) {
DoSomething();
} - Anonymous
February 12, 2005
I am firmly in the
if (var < max)
{
DoSomething();
}
camp. - Anonymous
February 12, 2005
Sure you can also do:
if (var < max) {DoSomething();}
or
if (var < max)
DoSomething();
but I prefer:
if (var < max)
{
DoSomething();
} - Anonymous
February 12, 2005
Always indent, if you don't indent you can't follow the structure, so I don't like Anon's example (which, barring spelling, is the same as Jesse's) - but I supect this is a problem of the HTML rendering.
As long as you indent, go with whatever feels best at the time, and try to be consistent in a single file, but otherwise don't worry about it. - Anonymous
February 12, 2005
Our team's coding standard is the last one you mentioned (braces and code both indented one tab from the previous indentation). It took a little bit to get used to but I find it at least as readable as the style without the braces indented. Unfortunately, VS 2003 doesn't support it and it's been irritating enough that I'm looking around for other editors that do. - Anonymous
February 12, 2005
There is a huge treatment of brace positioning in McConnell's Code Complete, with pros and cons for all common styles. I think the conclusion was that this style
if (var < max) {
__DoSomething();
}
had the most advantages. At least I switched to that style after reading Code Complete... - Anonymous
February 12, 2005
The comment has been removed - Anonymous
February 12, 2005
I like:
if (var < max) {
__DoSomething();
} - Anonymous
February 12, 2005
The comment has been removed - Anonymous
February 13, 2005
I had some indenting, but the blog editor took it all away :^( Sorry, I didn't mean for all of my examples to be squashed left. - Anonymous
February 13, 2005
If var < max Then
DoSomething()
End If
Clearly more readable.
Sadly the genuises who came up with the C# compiler don't allow it.
VB ain't so bad afterall. - Anonymous
February 13, 2005
The comment has been removed - Anonymous
February 14, 2005
I prefer
|if (var < max) {
| DoSomething();
|} - Anonymous
February 24, 2005
How can you make emacs's auto-indent do this,
if (var < max)
{
DoSomething();
}
instead of this,
if (var < max)
{
DoSomething();
} - Anonymous
February 24, 2005
the last is supposed to look like the third example of bracing on this page at the top.