- 6.10 여러 함수를 변환 함수로 묶기
- 소프트웨어는 데이터를 입력 받아서 여러 가지 정보를 도출
- 도출된 정보는 여러 곳에서 사용될 수 있는데, 그러다 보면 정보가 사용되는 곳마다 같은 도출 로직이 반복 되기도 한다.
- 이 떄 도출 작업을 한 군데로 모아두면 검색과 갱신을 일관된 장소에서 처리 가능, 로직 중복도 막기 가능
- 로직을 함수로 추출하는 것만으로 같은 효과를 볼 수 있지만, 데이터 구조와 이를 사용하는 함수가 근처에 있지 않으면 함수를 발견하기 어렵다.
- 변환 함수 : 데이터를 입력 받아서 필요한 정보를 모두 도출한 뒤, 각각을 출력 데이터의 필드에 넣어 반환
- 원본 데이터가 코드 안에서 갱신될 때는 클래스로 묶기
- 변환 함수로 묶으면 가공한 데이터를 새로운 레코드에 저장하므로, 원본 데이터가 수정되면 일관성이 깨질 수 있기 떄문
- 원본
```jsx
const acquireReading = () => ({
customer: 'ivan',
quantity: 10,
month: 5,
year: 2017,
})
const client1 = () => {
const aReading = acquireReading()
const baseCharge = baseRate(aReading.month, aReading.year) * aReading.quantity
return baseCharge
}
const client2 = () => {
const taxThreshold = year => (year - 2000) * 0.1
const aReading = acquireReading()
const base = baseRate(aReading.month, aReading.year) * aReading.quantity
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year))
return taxableCharge
}
const client3 = () => {
const aReading = acquireReading()
const calculateBaseCharge = aReading => baseRate(aReading.month, aReading.year) * aReading.quantity
const basicChargeAmount = calculateBaseCharge(aReading)
return basicChargeAmount
}
```
1.
```jsx
const enrichReading = original => {
const result = cloneDeep(original)
return result
}
```
2. 변경 하려는 계산 로직 중 하나를 고르고 이 계산 로직에 측정값을 전달하기 전에 부가 정보를 덧붙이도록 수정
```jsx
const client3 = () => {
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
const calculateBaseCharge = aReading => baseRate(aReading.month, aReading.year) * aReading.quantity
const basicChargeAmount = calculateBaseCharge(aReading)
return basicChargeAmount
}
```
3. calculateBaseCharge블 부가 정보블 덧붙이는 코드 근처로 옮긴다(함수 옮기기"』,
```jsx
const calculateBaseCharge = aReading => baseRate(aReading.month, aReading.year) * aReading.quantity
const enrichReading = original => {
const result = cloneDeep(original)
result.baseCharge = calculateBaseCharge(result)
return result
}
```
1. 해당 클라이언트가 부가 정보를 담은 필드를 사용하도록 수정
```jsx
const client3 = () => {
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
return aReading.baseCharge
}
```
1. 이상이 없는지 테스트
2. 클라이언트1도 똑같이 수정
```jsx
const client1 = () => {
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
return aReading.baseCharge // 변수 인라인하기
}
```
1. 먼저 변환 함수부터 끼워 넣는다.
```jsx
const client2 = () => {
const taxThreshold = year => (year - 2000) * 0.1
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
const base = baseRate(aReading.month, aReading.year) * aReading.quantity
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year))
return taxableCharge
}
```
1. 앞에서 새로 만든 필드로 교체
```jsx
const client2 = () => {
const taxThreshold = year => (year - 2000) * 0.1
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
const base = aReaging.baseCharge
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year))
return taxableCharge
}
```
1. 테스트해서 문제가 없다면 base 변수를 인라인
```jsx
const client2 = () => {
const taxThreshold = year => (year - 2000) * 0.1
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
const taxableCharge = Math.max(0, aReaging.baseCharge
- taxThreshold(aReading.year))
return taxableCharge
}
```
1. 계산 코드를 변환 함수로 옮긴다.
```jsx
const enrichReading = original => {
const result = cloneDeep(original)
result.baseCharge = calculateBaseCharge(result)
result.taxableCharge = Math.max(0, result.baseCharge - taxThreshold(result.year))
return result
}
```
1. 새로 만든 필드들 사용하도록 원본 코드 수정
```jsx
const client2 = () => {
const rawReading = acquireReading()
const aReading = enrichReading(rawReading)
return aReading.taxableCharge
}
```