CONTENTS | PREV | NEXT Java Code Conventions


8.5 Miscellaneous Practices


8.5.1 Parentheses

It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others--you shouldn't assume that other programmers know precedence as well as you do.

if (a == b && c == d)     // AVOID!
if ((a == b) && (c == d)) // RIGHT

8.5.2 Returning Values

Try to make the structure of your program match the intent. Example:

if (booleanExpression) {
return TRUE;
} else {
return FALSE;
}
should instead be written as

return booleanExpression;
Similarly,

if (condition) {
return x;
}
return y;
should be written as

return (condition ? x : y);

8.5.3 Expressions before `?' in the Conditional Operator

If an expression containing a binary operator appears before the ? in the ternary ?: operator, it should be parenthesized. Example:

(x >= 0) ? x : -x

8.5.4 Special Comments

Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.



CONTENTS | PREV | NEXT
Copyright © 1997 Sun Microsystems, Inc. All Rights Reserved.