복잡한 조건문의 조건식과 각 분기 본문에 의도를 드러내는 이름을 붙이는 리팩터링(분해, Decompose Conditional), 그리고 결과가 같은 여러 조건 검사를 하나로 묶는 리팩터링(통합, Consolidate Conditional Expression). 조건 코드는 무엇이 일어나는지는 말하지만 왜 그런지는 잘 말하지 않는데, 이름이 그 "왜"를 채운다.
// 분해
if (isSummer(date)) charge = summerCharge(quantity)
else charge = regularCharge(quantity)
// 통합
if (isNotEligibleForDisability(employee)) return 0
// 원래: seniority < 2 이면 0, monthsDisabled > 12 이면 0, isPartTime 이면 0- 통합하면 하려는 일이 한 문장으로 드러나고, 대개 함수 추출로 이어진다
- 하지만 정말 서로 독립된 검사라면 합치지 않는다. 하나의 규칙이라고 말할 수 있을 때만 통합한다
- 모든 분기에 똑같이 들어 있는 문장은 조건문 밖으로 뺀다. 1판의 Consolidate Duplicate Conditional Fragments이고, 2판에서는 문장 슬라이드로 다룬다
- 정상 흐름과 예외 상황이 섞여 있다면 보호 구문이, 타입마다 갈라진다면 다형성이 다음 단계다
출처: 『리팩터링 2판』 마틴 파울러 (원서 Refactoring, 2nd Edition) · refactoring.com: Decompose Conditional · refactoring.com: Consolidate Conditional Expression · Refactoring.Guru: Decompose Conditional · Refactoring.Guru: Consolidate Conditional Expression