Change Function Declaration - Refactoring skills(5)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
7/22

5. Change Function Declaration(함수 선언 바꾸기)

Rename a function, change list of parameters
함수 이름을 바꾸기, 파라미터의 개수추가 혹은 제거하기.

function circum(radius) {...}

to

function circumference(radius) {...}

Motivation

  1. Easier to understand
    이해하기 쉽다.
  2. Easier to reuse, sometime better encapsulation
    재사용하기 쉽고, 캡슐화 수준이 높아진다

Simple Procedure

  1. If you want to remove a parameter, first make sure that the body of the function does not reference the parameter to be removed
    매개변수를 제거하려거든 먼저 함수 본문에서 제거 대상 매개변수를 참조하는 곳은 없는지 확인한다.

  2. Change the method declaration to form you wanted.
    메소드 선언을 원하는 형태로 바꾼다.

  3. Find all the parts that refer to the existing method declaration and modify them to the changed form.
    기존 메소드 선언을 참조하는 부분을 모두 찾아서 바뀐 형태로 수정한다.

  4. Test it
    테스트한다.

Migration Procedure

  1. If you need to make the following extraction steps easier, refactors the body of the function properly.
    이어지는 추출 단계를 수월하게 만들어야 한다면 함수의 본문을 적절히 리팩터링한다.

  2. Extract the function body as a new function.
    함수 본문을 새로운 함수로 추출한다.

  3. If you need to add parameters to the extracted function, follow the 'Simple Procedure'.
    추출한 함수에 매개변수를 추가해야 한다면 '간단한 절차'를 따라 추가한다.

  4. Test it
    테스트한다.

  5. Inline the existing function.
    기존 함수를 인라인한다.

  6. If the name is temporarily attached, change the function declaration one more time and revert to the original name
    이름을 임시로 붙여뒀다면 함수 선언 바꾸기를 한 번 더 적용해서 원래 이름으로 되돌린다.

  7. Test it.
    테스트한다.

profile
Tea and Dessert

0개의 댓글