-
메소드 정리 : Replace Temp with QueryProgramming/리팩토링 2020. 9. 4. 15:46
참고자료
http://refactoring.guru/replace-temp-with-query
정의
어떤 수식의 결과값을 저장하기 위해서 임시변수를 사용하고 있다면, 수식을 뽑아내서 함수로 만들고
임시변수를 참조하는 곳을 찾아 모두 메서드 호출로 바꾼다.
기존 예시
double calculateTotal() { double basePrice = quantity * itemPrice; if (basePrice > 1000) { return basePrice * 0.95; } else { return basePrice * 0.98; } }
리팩토링 예시
double calculateTotal() { if (basePrice() > 1000) { return basePrice() * 0.95; } else { return basePrice() * 0.98; } } double basePrice() { return quantity * itemPrice; }
반응형'Programming > 리팩토링' 카테고리의 다른 글
메소드 정리 : Remove Assignments to Parameters (0) 2020.09.04 메소드 정리 : Split Temporary Variable (0) 2020.09.04 메소드 정리 : Inline Temp (0) 2020.09.04 메소드 정리 : Inline Method (0) 2020.09.04 메소드 정리 : Extract Method (0) 2020.09.04