Remove Inline Function - Refactoring skills(2)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
4/22

2. Inline Function(함수 인라인 하기)

Get rid of the function when the body of the code is just as clear as the name.
함수 본문이 이름만큼 명확한 경우, 함수를 제거하고 인라인하기.

function rating(aDriver) {
  return moreThanFiveLateDeliveries(aDriver) ? 2 : 1
}
function moreThanFiveLateDeliveries(aDriver) {
  return aDriver.numberOfLateDeliveries > 5
}

to

function rating(aDriver) {
  return aDriver.numberOfLateDeliveries > 5 ? 2 : 1
}

Motivation

  1. When Indirection is needless (simple delegation) becomes irritating.
    쓸데없는 간접 호출은 거슬린다.

  2. If group of methods are badly factored and grouping them makes it clearer.잘못 추출된 메소드들이 있을 때, 다시 그룹화할 경우 더 명료해진다.

Procedure

  1. Check it is polymorphic method or not.
    다형메소드인지 확인한다.(서브클래스에서 오버라이드하는 메소드는 인라인하면 안 된다.)

  2. Find all the places to call functions to inline.
    인라인할 함수를 호출하는 곳을 모두 찾는다.

  3. Replace each call with a functional body.
    각 호출문을 함수 본문으로 교체한다.

  4. Test one by one when you change.
    하나씩 교체할 때마다 테스트한다.

  5. Delete the definition(original function).
    함수 정의(원래 함수)를 삭제한다.

profile
Tea and Dessert

0개의 댓글