Re: Coding Standards
Martin Gregorie <martin@see.sig.for.address> writes:
Sure, but its only two lines more than
if (a) {
For completeness, my style:
I write short if-statements on a single line,
preferably without unnecessary braces:
if( a )b();
This might be influenced by my general style rule:
?When two notations have the same meaning,
in doubt, choose the one that is shorter.?
Line breaks are added so as to respect the
syntactical structure. The if-statement's syntax is:
?if ( Expression ) Statement?
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.9
The following line break would not respect this:
.----------- break between ?if ( Expression )? and ?Statement?
v
if( a ) {
^
'--------- line break
b(); ...
This disrespects ?well-structured programming?
according to my definition of that term:
?A formal text is well-structured
if its surface structure matches its deep structure
as close as possible.?
Above, the surface structure (line breaks) is misleading
about the deep structure (syntactical breaks).
Therefore, it looks ugly to me, and I break like:
if( a ) // if ( Expression )
{ b(); } // Statement
When there are multiple lines in the block:
if( a )
{ b();
c(); }
Sometimes, I want to put the braces on a line for
themselves to add additional vertical space for
readability. If I have to force each braces on a line,
I write:
if( a )
{
b();
c();
}
Here, one sees, that I do not align matching braces.
I feel the first brace ?{? to be still part of the outer block,
because it transfers the control to the inner block like a
method call, which is still part of the calling block.
Conversely, the last brace ?}? transfers control back
to the caller, like a return statement. So it is indented
with the inner block.
This is also consistent with a one-pass indenter, that can
change indentation only /after/ it has read a character. The
?{? changes the indentation by 2, the ?} by -2, but only after
it was already encountered. So the change becomes visible at
the line break following the indentation-changing symbol for
the first time.