George said:
How do you feel about "hiding" semicolons at the end of lines?
No problem with it as long as you are following other practices that
make individual statements easy to see. A single statement is a single
unit. If you do things like keep one statement per line and use visual
cues to indicate when you have to use multiple lines (e.g. indenting the
rest of the statement and separating it from surrounding statements)
then there is little information carried by the semicolon itself.
But we were discussing compound statements. The same logic does not
apply. There we have at least to parts to the statement. For example in
the case of a while you have the loop condition and you have the
statements over which you are looping. In that case there is value in
making that separation distinct. Using braces on their own lines does
this very well. It forms a visual frame around the body but is still
attached to the statement at the top to make it a cohesive unit. This is
enhanced by always using braces and add whitespace above and below to
separate it from other statements (if the thing above or below is a
brace then no blank line as the brace is already a visual separator.
A mental test I like to do is the defocus test. Consider looking at the
code but try not to focus your eyes so that you can't make out the
individual letters and see if you can get a semblance of the structure
of the code without reading the actual letters. The brace on the new
line makes it easier to see.
It seems obvious to me that you have a balanced pair of braces. Why do
you not want to keep them balanced. If Sun wanted to hide the opening
brace then they should have used something else like
if( sdfsa )
asdfas
endif
I believe that most of the best-selling Java books
"hide" the opening brace at the end of the line.
(Just where it belongs.) : o)
Which bears little weight with me.
Would you care to elucidate them?
The style that should be used is the one that makes code easier to read
for the largest number of readers. I and others who have posted in these
groups find that the hidden brace style is harder to read. I am willing
to concede that there are those that can read both equally. For the
hidden brace style to be the one to use there must be a significant
number of people that find it *easier* to read than using the brace to
provide visual separation. I do not concede that such people exist in
significant numbers. I have seen no evidence of their existence.
One could due a scientific study to test programmers speed of
comprehension (and note that this test would not be testing personal
preference. I'm fairly confident of what the results would be.
And as a final comment consider this piece of code from an actual post
to these groups many years ago:
This code will result in auto shutdown immediately after startup. Yet,
if I comment out the call to dispose(), it works fine. That makes no
sense to me because exit() should not be called without the user
calling for the frame to shut down. Why does it self-destruct?
public void processWindowEvent(WindowEvent wE) {
super.processWindowEvent(wE);
if (wE.getID() == WindowEvent.WINDOW_CLOSING)
dispose();
System.exit(0);
}
How long did it take you to find the error? There is no opening brace on
the if so what looks like the closing brace of the if is actually the
closing brace of the method. If you follow the convention that opening
braces are, without exception, on their own line the error leaps off the
page at you.