[리팩토링 cheat sheet] 10.2 조건문 통합하기(Consolidate Conditional Expression)
# Before
if employee.seniority < 2:
return 0
if employee.month_disabled > 12:
return 0
if employee.is_part_time:
return 0
# After
if is_not_eligible_for_disability():
return 0
def is_not_eligible_for_disability():
return (employee.seniority < 2
or employee.month_disabled > 12
or employee.is_part_time)
- 비교조건이 다르지만 그 결과로 수행하는 동작은 똑같은 코드들이 있다 어차피 같은 일을 할 것 이라면 조건 검사도 하나로 통합하는게 낫다
- 이유
- 여러 조각으로 나뉜 조건들을 하나로 통합함으로써 내가 하려는 일이 더 명확해짐
- 작업이 함수 추출하기 까지 이어질 가능성이 높다
- 복잡한 조건식을 함수로 추출하면 코드의 의도가 훨씬 분명하게 드러나는 경우가 많다