I'm curious. Why is so much code in this forum formatted like this

if (something)
{
  do something
}
else
{
  do another thing
}

rather than

if (something) {
  do something
} else {
  do another thing
}

The first version is harder to read and takes up more lines, which means more blocks spanning more than 1 window vertically.
The second version is easier to read, more compact, and is the style consistently used in the Java Language Specification, the Sun/Oracle tutorials, and the source code of the API itself.
So why do people persist in using version 1?

Recommended Answers

All 19 Replies

Member Avatar for hfx642

James, James, James, James, James...

Nope. I've been a professional developer for close to 30 years.
The first method, matches the traditional

if (condition) then
Begin
End
Else
Begin
End

Plus... It makes it SOOOO much easier to match the "}" with the previous "{".

I personnally don't like the second method.
I can't see if my "{}" match!
I see a bunch of "}"s, but I can't (easily) see where the code block starts.

That's my story... and I'm stickin' to it!!

And now ya know.

I'm with James. I hate the first method.

I prefer the second method because when I use the editor's "Find matching brace" function from a } it displays the line with the statement I'm looking for. With the { on an empty line I have to scroll up one to see where I am in the code.
That's probably my editor, I don't know about others.

I am in my second Java class now(I'm a noob!) but our book and professors for the courses at my school use the first method that you have shown. So, that may explain some students doing that method. I like the second method better though.

Thanks everyone (so far!). Of course it's a matter of personal preference, but I wondered why the style used in all the official Java docs and code wasn't the de-facto standard. Maybe javaNooblet's explanation is the real answer? Any maybe hfx642's view is the reason why school professors prefer 1?

@hfx642: do you really get your view of code structure by matching { and }? I always use an automatic code formatter, so I can always rely on the indentation.

ps: One time I really dislike option 1 is when I get confused by code that goes

...
  ...
  ...
}
while (something);
{
  ...
  ...
  ...

as opposed to

...
  ...
  ...
}
while (something)
{
  ...
  ...
  ...

Again I'm a student and your first example is how we have been taught to write our code. It seems easier to read for me, but then its the only format I have ever used.
Glen

most people choose first method for mathcing {}.If these are written in single line,then it is complex to match.

I guess most people who use the first method are those who learned programming in other languages like C/C++ where the common practise is to use the first method.

And once you get used to a way of writing code, one tends to stick to the same method even after changing to a new language. A matter of personal preference as said above.

I myself started off with C/C++ and started writing java with the same method (first method), though i have recently shifted to the second one. :)

I started off with C++ in school but now, as you guys know, I do PHP. I use the first method and, to be perfectly honest, I find the second way incredibly hard to keep track of where brackets are opened and closed. Oddly enough, however, I use the second version for all my Javascript.

Yes.Even i prefer first method whenever i need to read other's code.

Second version all the way. I'm pretty sure the first version is for those who get paid by lines of code written. ;)

Afterall , lines of code doesn't matter. What matters is efficiency of code. Lines of code depends on one's personal view.

What matters is efficiency of code

No, what matters first and foremost is the code is doing the job it set out to do. Everything else (good design, efficiency etc.) comes after that.

No, what matters first and foremost is the code is doing the job it set out to do.

Yeah ofcourse...that is by default..

The best way without is the first method.

If Id write the second method, Id just go all out:

if (something) {  do something} else { do another thing}

If Im going to make code unclear with the second method, I just make it 100% sloppy instead of 50%. The second method is not clear at all. If you are programming PERSONALLY, then whatever you want to use, but if it is a open source project, then the first method is better.

No, what matters first and foremost is the code is doing the job it set out to do. Everything else (good design, efficiency etc.) comes after that.

I disagree. What matter first is that the code is clear to the programmer (or programmers better yet) then second making sure the code is doing the job its suppose to do.

If you do that first, then second is much easier.

If Im going to make code unclear with the second method, I just make it 100% sloppy instead of 50%.

I suppose the guys who wrote the JDK preferred sloppy code as well. Most, if not all, of it is written in the second style.

If Id write the second method, Id just go all out:

> if (something) {  do something} else { do another thing}

What's your point here? - that example is NOT in the second style.
Seriously, how do you explain the fact the Sun, and now Oracle, use style 2 throughout the Java Tutorials and for the source code of the API. Didn't they understand how to write understandable and maintainable Java?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.