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
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
I'm with James. I hate the first method.
Ezzaral
Posting Genius
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
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
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
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073