티스토리 뷰

대분류 - Coding Conventions [CODSTA]

규칙명 - Avoid using negative logic in if-else statement [CODSTA.ANL-5]
- 부정(not)을 이용해서 if-else 류의 비교문을 작성하지 말라는 뜻이다.
- negative logic 은 제어흐름의 이해를 어렵게 하기 때문에 부정을 먼저 비교하기 보다는 긍정을 먼저 비교하도록 권장하고 있다.

DESCRIPTION

This rule identifies negative logic in 'if-else' control flow. 'if-else' control flow includes the patterns below:

-if (cond)
    expr
 else
    expr
 
-cond ? expr1 : expr2

An error is reported for each occurrence.

CODSTA.ANL-5 규칙은 negative logic 을 이용한 "if-else" 제어흐름을 찾아낸다.

BENEFITS

Using negative logic could sometimes lead to complicated control flow. The logic could easily be modified to avoid negation and provide a more intuitive flow.

- negative logic 은 제어흐름의 이해를 어렵게한다. negative logic 을 피하면 더 직관적인 제어흐름이 된다.

EXAMPLE

package examples.rules.codsta;

public class ANL {
    public int specialAdd(int i, int j) {
        if (i!= j) //VIOLATION
            return i+ j;
        else
            return i;
    }
}

REPAIR

package examples.rules.codsta;

public class ANLFixed {
    public int specialAdd(int i, int j) {
        if (i== j) //FIXED
            return i;
        else
            return i+ j;
    }
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함