Split Phase - Refactoring skills(11)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
13/22

11. Split Phase(단계 쪼개기)

Split code which do different things into separate modules
(서로 다른 두 대상을 한꺼번에 다루는 코드를 각각 별개 모듈로 나눈다)

const orderData = orderString.split(/\s+/)
const productPrice = priceList[orderData[0].split("-")[1]]
const orderPrice = parseInt(orderData[1]) * productPrice

to

const orderRecord = parseOrder(orderString)
const orderPrice = price(orderRecord, priceList)

function parseOrder(aString) {
  const values = aString.split(/\s+/)
  return {
    productID: values[0].split("-")[1],
    quantity: parseInt(values[1])
  }
}
function price(order, priceList) {
  return order.quantity * priceList[order.productID]
}

Motivation

  1. Make the different explicit, revealing the different in the code
    코드들을 별도 모듈로 분리하면 그 차이를 훨씬 분명하게 드러낼 수 있다.

  2. Be able to deal with each module separately
    각 모듈마다 다루기 쉬워진다.

Procedure

  1. Extract the code corresponding to the second step as an independent function.
    두 번째 단계에 해당하는 코드를 독립 함수로 추출한다.

  2. Test it.
    테스트한다.

  3. Create an intermediate data structure and add it as an argument for the previously extracted function.
    중간 데이터 구조를 만들어서 앞에서 추출한 함수의 인수로 추가한다.

  4. Test it.
    테스트한다.

  5. Review the parameters of the extracted second step function one by one. What is used in the first step of this process is transferred to an intermediate data structure. Test each move one by one.
    추출한 두 번째 단계 함수의 매개변수를 하나씩 검토한다. 그중 첫 번째 단계에서 사용되는 것은 중간 데이터 구조로 옮긴다. 하나씩 옮길 때마다 테스트한다.

  6. Extract the first step code as a function and make it return an intermediate data structure.
    첫 번째 단계 코드를 함수로 추출하면서 중간 데이터 구조를 반환하도록 만든다.

profile
Tea and Dessert

0개의 댓글