알고보면 당연한 내용인데 코딩하다가 자주 하는 실수, 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.






Tag
  1. 2008/03/03 13:10 [Edit/Del] [Reply]
    이런 건 알면서도 경험이 없으면 지나치기 쉬운 부분이네요..
    멋져요.
  2. 2008/03/03 17:16 [Edit/Del] [Reply]
    precedence 문제는 참.. 사용할수록 골치가 아파지더라구요 ㅎ
    좋은글 잘보고 갑니다 ^^
  3. 2008/03/04 18:19 [Edit/Del] [Reply]
    10.5.2 보고 '3항 연산자는 condition 구하는 수식이 복잡하면 코드가 더 난잡해지는데...' 라고 생각하고 적을라치니까 10.5.3에 괄호로 묶으라고 나오네요. ㅎㅎ;;
    축약해서 라인 수를 줄이는 게 가독성에 나쁜 경우도 있는 거니까... 상황 따라 판단해야 할 것들도 있을 것 같아요. ^^

    잘 봤어요~
  4. iolo
    2008/03/05 10:31 [Edit/Del] [Reply]
    대부분의 return 문에선 장황한 if else 보다 ? : 가 더 눈에 잘 들어오죠~
    아님 말구=3=3=3=33

댓글을 남겨주세요

Name *

Password *

Link (Your Homepage or Blog)

Comment

Secret