Combine Functions Into Transform - Refactoring skills(10)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
12/22

10. Combine Functions Into Transform(여러 함수를 변환 함수로 묶기)

Takes the source data as input and calculates all the derivations, putting each derived value as a field in the output data
(원본 데이터를 입력받아서 필요한 정보를 모두 도출한 뒤, 각각을 출력 데이터의 필드에 넣어 반환한다. 이렇게 해두면 도출 과정을 검토할 일이 생겼을 때 변환 함수만 살펴보면 된다.)

function base(aReading) {}
function taxableCharge(aReading) {}

to

function enrichReading(argReading) {
  const aReading = _.cloneDeep(argReading)
  aReading.baseCharge = base(aReading)
  aReading.taxableCharge = taxableCharge(aReading)
  return aReading
}

Motivation

  1. Avoid duplication of logic
    중복되는 로직을 막을 수 있고, 검색과 갱신을 일관된 장소에서 처리 가능함.

Procedure

  1. Create a transform function that takes the input of the record to convert and returns the value.
    변환할 레코드를 입력받아서 값을 그대로 반환하는 변환 함수를 만든다.

  2. Pick one function to group, transfer the body code to the transform function, and record the processing result as a new field in the record. The client code is then modified to use this field.
    묶을 함수 중 함수 하나를 골라서 본문 코드를 변환 함수로 옮기고, 처리 결과를 레코드에 새 필드로 기록한다. 그런 다음 클라이언트 코드가 이 필드를 사용하도록 수정한다.

  3. Test it.
    테스트한다.

  4. The remaining relevant functions are also handled according to the above process.
    나머지 관련 함수도 위 과정에 따라 처리한다.

profile
Tea and Dessert

0개의 댓글