#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.

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.

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!!!

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

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.

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

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: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.

? 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

I like the quote from Linus - he told that everybody should print out GNU coding style guide and then burn it, as a sign

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"(TM) 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.

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.

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.

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.

The most ancient of the few C++ gurus I know used to agree that the main reason for style 1 was back in the day of poor IDE's, where it was a distinct time-saver to reduce the number of lines in your source code, and save you all that scrolling!

Back when C++ was pretty new, things like this became a de-facto standard because they were most appropriate for the tools people were using. Similarly, it was back in those Dark Ages that super-short variable names were invented, which included FULL type information for the variable, using one-letter codes.

E.g.:

1

publicstaticfinalboolean[] pSfbAmyArray;

When you can't do syntax-highlighting, and your compiler isn't too hot at "invalid type conversions", things like that can be a life saver. But for normal people nowadays it's just silly.

Equally, the fear that some people still have of writing any variable name more than about 6 characters long used to be valid. But really - is it that hard to learn to hit the TAB key? (or ctrl-space if you've been trained by MSVC)?

This is an never ending subject, because everyone, including me, has strong preferences how code should look like. Which is quite understandable if you work with code every day of your life. That these preferences can change over time does not hinder people to promote their current ones with religious fever

I used to think that the brain was the most wonderful organ in my body. Then I realized who was telling me this.-- Emo Phillips

style... the reason I code this way is that is the default way Eclipse formats code, and I'm waaay to lazy to change it.

But as has been mentioned, I don't think it matters as long as it is consistent through the file(s), and across the dev team. There is nothing worse than reading Joe Smith's code and then reading John Jone's code which is a completely different style.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org