backend/Java

[Java] 자바 코딩하다가 실수하는 부분들

버리야 2008. 3. 3. 10:12
반응형

알고보면 당연한 내용인데 코딩하다가 자주 하는 실수, Java Code Conventions 내용중에 있군요.

써놓고나면 덜 실수하려나...


출처 : http://java.sun.com/docs/codeconv/

Miscellaneous Practices

10.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

10.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);

10.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;

10.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.






반응형