Introduce Parameter Object - Refactoring skills(8)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
10/22

8. Introduce Parameter Object(매개변수 객체 만들기)

Replace groups of data items that regularly travel together with a single data structure
(산재 되어있는 데이터 항목들을 하나의 데이터 객체로 구조화)

function amountInvoiced(startDate, endDate) {}
function amountReceived(startDate, endDate) {}
function amountOverdue(startDate, endDate) {}

to

function amountInvoiced(aDateRange) {}
function amountReceived(aDateRange) {}
function amountOverdue(aDateRange) {}

Motivation

  1. Make explicit the relationship between the data items
    데이터 항목간의 명확한 관계설정이 가능
  2. Reduce the size of parameter list
    파라미터 갯수를 줄일 수 있다.
  3. Make code more consistent
    같은 데이터 구조를 사용하는 모든 함수가 원소를 참조할 때 항상 똑같은 이름을 사용하기 때문에 일관성도 높여준다.
  4. Enable deeper changes to the code
    리팩터링 할 시 코드를 더 근본적으로 바꿔준다.

Procedure

  1. If there is no appropriate data structure, make new one.
    적당한 데이터 구조가 아직 마련되어 있지 않다면 새로 만든다.

  2. Test it.
    테스트한다.

  3. Add the new data structure with function name change
    함수 선언 바꾸기로 새 데이터 구조를 매개변수로 추가한다.

  4. Test it.
    테스트

  5. Modify to pass a new data structure instance when calling a function. Test every modification.
    함수 호출 시 새로운 데이터 구조 인스턴스를 넘기도록 수정한다. 하나씩 수정할 때마다 테스트한다.

  6. Replace previous parameters to new data structrue one.
    기존 매개변수를 사용하던 코드를 새 데이터 구조의 원소를 사용하도록 바꾼다.

  7. If you finish to replace, remove previous parameters and test them.
    다 바꿨다면 기존 매개변수를 제거하고 테스트한다.

profile
Tea and Dessert

0개의 댓글