개발방법론/모델링/Refactoring

Replace Nested Conditional with Guard Clauses

아름프로 2003. 8. 3. 11:57

Replace Nested Conditional with Guard Clauses


A method has conditional behavior that does not make clear what the normal path of execution is

Use Guard Clauses for all the special cases


double getPayAmount() {
        double result;
        if (_isDead) result = deadAmount();
        else {
                if (_isSeparated) result = separatedAmount();
                else {
                        if (_isRetired) result = retiredAmount();
                        else result = normalPayAmount();
                };
        }
return result;
};        



double getPayAmount() {
        if (_isDead) return deadAmount();
        if (_isSeparated) return separatedAmount();
        if (_isRetired) return retiredAmount();
        return normalPayAmount();
};        


For more information see page 250 of Refactoring




***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)