tags:

views:

206

answers:

3

I wanted to see if someone could shed some light on this for me. When I was using the Checkstyle plugin for Eclipse, I got a warning message on the following statement (not exactly this, but this form):

if (x != y)
{
   do();
}

Why is this a problem?

Edit: I apologize, I should have been more clear with my question. Thanks very much for your answers; they actually helped me with a completely different problem.

The warning that I got recommended that I not use a test of the form "x != y", but instead use a test of the form "x = y". I'm wondering why one would be better than the other.

A: 

See the Checkstyle's check description + rationale: Standard Checks

+3  A: 

The default setting for the LeftCurly checker is "eol". This means that you are enforcing the rule that if statements (and similar blocks) should look like this:

if (x != y) {
    do();
}

If you prefer the style you showed in the question, use the "nl" setting, described in the lcurly policy section of the Checkstyle manual.

You can modify the setting in Eclipse like this:

  1. From Window, choose Preferences.
  2. If needed, create a new check configuration (usually by copying one of the unmodifiable originals) and make it the default.
  3. Double-click the new configuration, click Blocks, then double-click Left Curly Brace Placement. Modify and save.
Douglas Squirrel
A: 

Is there a "else"?

I think that if you wrote something like that

if (x != y)
{
   do();
}
else 
{
   doSomethingElse();
}

I'd recomend too

if (x == y)
{
   doSomethingElse();
}
else 
{
   do();
}
Peter
If you vote down, please at least tell, what your problem with that answer was. Downvotes can be helpful, but only if one can learn from it.
Peter
This recommendation has to do with how you write if conditions, not brace style.
Douglas Squirrel