Bracing Opinions On Coding Style
IMHO we should allow the left braces to be on their own line, or not, depending on the taste of the programmer -- keeping in mind Bill Shannon's rule #0 about formatting (match the existing style).
Here is an example of what I mean. This comes from the very beginning of the most public class we have in AppServer -- PELaunch
if(args[0].trim().equals(STOP)) {
// check if instance is already running
if (isInstanceRunning()) {
PEMain.shutdown();
return;
} else {
getLogger().log(Level.INFO, "instance.notRunning");
return;
}
}
Why are the "return"s indented out?
Why are two closing braces at the same level of indent (I see this constantly)
I think the way I would formatted it is clearer: (let's not even go there about the else being pointless!). When you line up the braces this way you never have the odd "lined-up closing braces" appear because it would stick out like a sore thumb.
if(args[0].trim().equals(STOP))
{
// check if instance is already running
if (isInstanceRunning())
{
PEMain.shutdown();
return;
}
else
{
getLogger().log(Level.INFO, "instance.notRunning");
return;
}
}
Posted by Toby on October 14, 2006 at 05:47 PM PDT #
Posted by sdwe on October 14, 2006 at 06:30 PM PDT #
Posted by dsfwe on October 14, 2006 at 06:31 PM PDT #
Posted by King Bee on October 14, 2006 at 09:35 PM PDT #
Posted by Volker A. Brandt on October 15, 2006 at 02:03 AM PDT #
The human eye (brain) can detect even tiny differences in asymmetry quite easily. A symmetric style (braces on their own lines, matched in indent levels) ties in with that natural ability, offering a larger scale pattern easily scanned with only a casual glance.
The view that an asymmetric brace style is as good as a symmetric one flies in the face of this natural ability. The pattern formed (space occupied) by matching braces on their own lines, equally indented, is more visually prominent than using asymmetric '{' and '}' characters, which must be painstakingly acquired by scanning the ends of lines (at varying positions for each line).
That perhaps is the most troubling--the non-uniform indentation of the '{', which can be further to the left for *more* heavily-indented lines--a violation of a reasonable expectation that places more heavily indented material further to the right as shown below. There is no placement consistency with such an approach.
if ( very long expression ) { if ( short-expr { if ( s ) { if ( long long long long long expr ) { .... } } } }The above example is pretty goofy as far as patterns go; no placement-consistency and no visual symmetry with the braces; the pattern formed is not reinforced by having a matching brace; only blank space is available to help make the visual scanning easier. Just draw a continous line through the '{' characters and the meandering route is evident. Add to that the often semi-random placement of closing braces (which seem to be encouraged by the semi-random placement of the opening '{' brace, and it's a recipe for tough slogging. Here's a very commonly-seen style (the "mishmash" style) that's found in a lot of code. I'm sure it's encouraged by the lack of placement-consistency of the '{' :
int foo( ... args ... ) { if ( expr1 ) { .... } else { ... } }A better way--one style to suit them all, one style to search visually, one style to bind the code together in a common approach:
if ( very long expression ) { if ( short-expr { if ( s ) { if ( long long long long long expr ) { .... } } } }Compare the prior example to the one above. The example above shows strong placement-consistency, and the visual symmetry is reinfored by the symmetric use of braces.Just as with a photograph, strong compositions are made by strong visual patterns; small details rarely make a photograph strong, unless there are many, and taken as a group, and at that point its pattern recogition on a broad scale--with braces, the issue is matching up individual pairs.
Posted by Lloyd Chambers on October 16, 2006 at 10:46 AM PDT #
Posted by Online Casino on October 25, 2006 at 04:13 AM PDT #
this is cool, this is what we want dude......
Posted by Tiffany Cuff Links on November 13, 2009 at 06:29 PM PST #