CONTENTS | PREV | NEXT | Java Code Conventions |
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
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);
If an expression containing a binary operator appears before the ? in the ternary ?: operator, it should be parenthesized. Example:
(x >= 0) ? x : -x
Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.