Brace styles

This is a constant argument among my friends and I, and we’re split down the middle on it. So how do you guys normally code?

public foo(){

}

or

public foo()
{

}

I use #2

You argue about this? Jeez, do you guys have alot of time… :wink:

#2 is just plain cleaner. Using #2, you can visually line up blocks of code even in potentially confusing spots. Plus, the brace in #1 can get lost if the “if/for/while” statement is wider than the screen.

Anything but #2 is crap :slight_smile:

#1, without a shadow of doubt

Claiming that “braces get lost” is just a matter of a crappy editor!

[quote]if () {
} else {
}
[/quote]
is just SOOO more cleaner than

[quote]if ()
{
}
else
{
}
[/quote]
But at the end of the day - just choose one and make sure that everybody follow the the same rule!

Yep, consistency is what you should aim for. Any large project with multiple people working on the same files should come up with a coding standards document before doing anything else. Otherwise things like CVS diff become useless as different people format their code or get their IDE to format it. Another classic argument is “spaces or tabs?”

That said, number 2 is easily the best, and people who use number 1 should be forced to eat platefuls of gravel until they change their ways. ;D

I like gravel* :stuck_out_tongue:

Cas :slight_smile:

*bikers tend to have an affinity with the stuff anyway

Claiming that “braces get lost” is just a matter of a crappy editor!

coughbullshtcough*

Nearly all editors are able to point out where the previous brace is. The problem is that if you have a long line, the brace will be beyond scroll range. Like this:


if(thisIsAVariable > myVariable || !myVariableCache.contains(thisIsAVariable) || thisIsAVariable < 0)
{
}

Formatting it on the same line is troublesome:


if(thisIsAVariable > myVariable || !myVariableCache.contains(thisIsAVariable) || thisIsAVariable < 0) {
}

Of course, many will argue that you’re supposed to do this:


if(thisIsAVariable > myVariable ||
    !myVariableCache.contains(thisIsAVariable) ||
    thisIsAVariable < 0) {
}

Except that is not cleaner. The brace gets lost in the jumble of code and a quick visual scan will often fail to locate it.

In real life though, I don’t worry too much about it. I’m usually the code lead, so I simply come in and tell people how they’re going to do it. If they don’t like it, I reformat their code on them. (My quick reformat skillz with IDEs make this less painful for me than for them.) Just call me the BCLFH*. BWHAHAHAHA!!!

  • (Bastard Code Lead From Hell)

No… you’ve put your ||'s on the wrong side !


if( thisIsAVariable > myVariable
 || !myVariableCache.contains(thisIsAVariable)
 || thisIsAVariable < 0)
{ 
} 

That’s the way to do it… then your conditional logic doesn’t get lost off the side of the screen with the brace.

Aligned braces just make much more sense. The blocks are more easily recognized. It is much clearer to read. The awkward zig-zagging of braces is looney :slight_smile:

In all seriousness it really only matters what you are used to… and you can get used to almost anything. (I still prefer aligning braces though).

It is frustrating though dealing with code that is formatted the ‘wrong’ way. but most good IDEs have built-in code formatters… so just make the code look the way you want it… If you are working on a team it is probably best to establish one format for your source code repository, but if you have good tools you can work locally any way you want.

No… you’ve put your ||'s on the wrong side !

Sorry, my bad. If I do it, I usually put the || on the left as well. Still, my point holds. The brace gets lost in the code and will be difficult to find.

Thanks guys. We argue about it because when we help each other debug, it’s hard to read each other’s code. I’ll be sure to show this to all of them. I love being right. ;D

I am with #2 as well, although I often felt that #1 is kind of a Java standard. Dunno why.

In the end, this doesn’t make the quality of the source code. Can be automatically converted.

Nr 1 as it is the java standard ;D

http://java.sun.com/docs/codeconv/

I used to mildly prefer #1 but now I strongly do. The main reason for that change is the way IDEA handles code folding:

http://leknor.com/g/braces-expanded.png
and
http://leknor.com/g/braces-collapsed.png

I’ve enabled what IDEA calls “highlight current scope” so if the braces are off the end of the screen it is sort of a non-issue:

http://leknor.com/g/highlight-scope.png

I also feel a very long line of code is something to be avoided and instead of accommodating long lines I try to restructure into more readable code when possible. I don’t want to encourage a bad practice by making it less painful.

In the end the real reason is the Java Coding Convention from Sun. I don’t have the hubris to think “my personal way” is better. My personal convention would be a preference and an opinion, not a fact, and would be weak support for an argument.

Does anybody here use


while ( something )
  {
    statements;
  }

? This is style proposed by GNU, and while I can accept both #1 and #2, I would start to cry if I would have to use GNU formatting. I like the quote from Linus - he told that everybody should print out GNU coding style guide and then burn it, as a sign :slight_smile:

[quote] I like the quote from Linus - he told that everybody should print out GNU coding style guide and then burn it, as a sign
[/quote]
Linus is right. Oh, my aching eyes! I’ve seen this style before and I see no advantage to it. It ends up making your code look amaturish (like someone who doesn’t understand spacing), and wastes horizontal space. I’ve seen code like this with the code lined up with the indented braces, but that looks even worse. It strikes me that this is simply part of the "RMS World Domination Plan"™ of getting everyone to do things his way without any real benefit.

Yep, the GNU style is just plain brain dead. At least the non-aligned style has the one advantage of saving a little vertical space (at the expense of readability). This business of indenting braces is simply dumb.

With most code editors now allowing for “folding code” (that is to say, a little plus-sign twiddle that allows you to hide the code between the braces at your discretion) I find there is no reason to worry about vertical space.

For myself I always have used

public static void main( String args[] )
{
// blah
// blah
// blah
}

But that’s me. I could get really evil and ask what you guys do about line and parenthesis spacing. ;D

[quote]Yep, consistency is what you should aim for. Any large project with multiple people working on the same files should come up with a coding standards document before doing anything else.
[/quote]
Except that, nowadays, if your IDE can’t automatically “convert-on-open” (and possibly - if you’re feeling generous towards people without IDE’s - “de-convert-on-close”) then you need to get a new IDE.

There’s no longer any need for a coding standards doc for formatting purposes; fire anyone who refuses to use a decent IDE (even VI/VIM can do this kind of thing automatically). Of course, variable names etc still need a standards doc.