계속해서 Recommended Sequence for Refactoring[1] 의 가장 첫 단계인 Remove duplications 에 적용할 수 있는 리펙토링 기법 하나를 더 소개한다. 먼저 소개한 God Method[2] 와는 얼핏 유사한 상황 같지만 분명한 차이가 있다.
Problems & Constraints
하나의 마스터 함수만 구현은 유지하고, 다른 함수들은 받은 입력의 타입만 변화시켜 마스터 메소드를 호출한다.
다음의 두 함수는 다른 입력을 받지만 목적은 같다.
toDate(long timestamp) { .. // tens of lines }
toDate(DateTime dateTime) { .. // tens of lines }
long 타입의 timestamp 를 DateTime 으로 변환시켜 (convert) Date 용 함수를 호출한다.
toDate(long timestamp) { DateTime dateTime = new DateTime(timestamp); // convert return ConvertDateTimeToServerDate(dateTime); // then, redirect // no more duplicated lines }
toDate(DateTime dateTime) // master method { .. // tens of lines (unchanged) }