October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Good Java Style, Revisited

  • April 16, 2008
  • By Rob Lybarger
  • Send Email »
  • More Articles »

Braces

In any given if, while, do, or for loop, you will have braces surrounding that constructs instructions. The exact positioning of where the opening and closing braces goes is another great point to argue about. The main styles are:

... K&R Style ...
if (condition) {
    statement;
    statement;
}

... Allman Style ...
if (condition)
{
    statement;
    statement;
}

...Whitesmiths Style ...
if (condition)
    {
    statement;
    statement;
    }

...GNU Style ...
if (condition)
  {
    statement;
    statement;
  }

This wikipedia page has a longer discussion of these and other brace styles, and includes a few comments about their origins and motivations. My own experience is that the first two styles are the most common. The examples and tutorials that you might find on Sun's own site uses the K&R style almost exclusively, and it is hard to argue with their influence on a language they steer. As I cut my teeth on C code originally and learned Java from Sun's tutorial pages, my fingers do a K&R-style opening-brace-and-return automatically. Naturally, my current workplace uses the Allman style. Fortunately, when they want to beat me up about it, we have an Eclipse formatter configuration that makes the adjustments.

As an aside, because Java allows block statements that are not attached to a control structure, the Allman style does permit simple removal of the condition (by commenting out its line) without the need to readjust the opening brace to accommodate.

... this is syntactically okay, if logically suspicious ...
//if (condition)
{
   statement;
   statement;
}

Statements

Do not put more than one statement on a line. Syntactically this is legal, of course, but is visually distracting and messy, and invites someone later to possibly make a mistake when they edit, copy, or paste in the vicinity of such a line. Statements should occur each on their own line and should be indented property relative to their enclosing brace. (See the sections on Braces and on Continuation Lines for related points.)

Continuation Lines

If you are writing an excessively long line and you want to wrap it, the continuation line should be indented. If your long line will begin a new block that will need a brace and indented lines coming after it, the continuation line should be double-indented to further set it apart from the block statements. (And in this latter case, using the Allman bracing style does help emphasize the difference between the continuation lines and the block's statements.)

if (some really long condition that you need
   to continue here)
{
   statement;
   statement;
}

Wrapping a line to perform a continuation in the middle of a variable declaration with its initialization should be done to keep the variable declaration on one line and the initialization on the other (whenever possible). This might arise due to using a fully qualified class name, notably when you are already indented a few levels already.

...
com.acme.foo.project.data.YourObjectFactory objFactory =
   new com.acme.foo.project.data.YourObjectFactory();
...

Conditional Expressions

Simple conditional expressions are easy enough to express and be comprehended later. Things tend to get a little tricky to follow when multiple sets of grouping parentheses are used in conjunction with booleans "&&" or "||". Although a very lengthy conditional expression should indeed be wrapped into the next line, a better approach is often to declare one or more temporary boolean values such that the complexity of the conditional expression can be divided into more easily digestible chunks.

... instead of this ...
if (value != null && value.length() > 0 && (errCount = 0 ||
   isIgnorable(currentError)))
{
   ... statements ...
}

... try this ...
boolean valueIsGood = (value != null && value.length() > 0);
boolean noErrors = (errCount = 0 || isIgnorable(currentError));
if (valueIsGood && noErrors)
{
   ... statements ...
}

Omitted Braces

From a syntax perspective, it is allowable to omit the braces when only a single statement will be executed in the control structure. My experience is that this is highly dangerous: in a fast-paced, deadline-driven coding marathon, your brain might assume braces are present when in fact all that is present is a single, indented statement; and in this case, you will add what you think is a second statement to the control structure that will actually be applied in all cases. So I highly recommend, as do others, that your control statements always brace, even for a single statement. (When you do add a second statement later, your braces are already in place.)

... don't do this ...
if (condition)
   statement;

... or you might risk this bug later ...
if (condition)
   statement;
   statement;




Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel