954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java code formatting

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?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

javaNooblet
Junior Poster
133 posts since Sep 2011
Reputation Points: 28
Solved Threads: 2
 

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)
{
  ...
  ...
  ...
JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: