블로그 이미지
암초보

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

Tag

04-25 23:14
2011. 9. 19. 04:04 프로그래밍/Refactoring

※ 알고리즘을 보다 명확한 것으로 바꾸고 싶을 때는, 메소드의 몸체를 새로운 알고리즘으로 바꾼다.

※ 절차
1. 대체 알고리즘을 준비하여 적용후 컴파일
2. 알고리즘 테스트. 만약 결과가 같다면 작업은 끝
3. 만약 결과가 같지 않다면, 테스트에서 비교하기 위해 예전의 알고리즘을 사용하여 디버깅

Before
String findPerson(String[] people) {
  for (int i=0; i<people.length; i++) {
    if(people[i].equals("Don")) {
      return "Don";
    }
    if(people[i].equals("John")) {
      return "John";
    }
    if(people[i].equals("Kent")) {
      return "Kent";
    }
  }
  return " ";
}


After
String findPerson(String[] people) {
  List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});
  for (int i=0; i<people.length; i++) {
    if(candidates.contains(people[i]))
      return people[i];
  }
  return " ";
}



//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리펙토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Replace Method with Method Object  (0) 2011.09.19
Remove Assignments to Parameters  (0) 2011.09.19
Split Temporary Variable  (0) 2011.09.19
Introduce Explaining Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
posted by 암초보