Add a name to an expression
지역 변수를 활용하여 표현식을 쪼개 관리하기 더 쉽게 만들기.
//price is base price quantity discount + shipping
return order.quantity * order.itemPrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.min(order.quantity * order.itemPrice * 0.1, 100)
to
const basePrice = order.quantity * order.itemPrice
const quantityDiscount = Math.max(0, order.quantity 500) * order.itemP
const shipping = Math.min(basePrice * 0.1, 100)
return basePrice - quantityDiscount + shipping;
Break down and name a part of a more complex piece of logic
복잡한 로직을 구성하는 단계마다 이름을 붙이고 쪼갤 수 있어서 코드의 목적을 명확하게 드러냄.
Easier for debugging(You can make breakpoint or add statement for printing out that condition.
디버깅에도 도움이됨.(디버거에 중단점을 지정하거나 상태를 출력하는 문장을 추가할 수 있음)
Check that the expression you want to extract has no side effects.
추출하려는 표현식에 부작용은 없는지 확인한다.
Declare an invariant variable and insert a duplicate of the expression to be named.
불변 변수를 하나 선언하고 이름을 붙일 표현식의 복제본을 대입한다.
Replace the original expression with a newly created variable.
원본 표현식을 새로 만든 변수로 교체한다.
Test
테스트한다.
If the expression is used in multiple places, replace each with a newly created variable. Test each time one is replaced.
표현식을 여러 곳에서 사용한다면 각각을 새로 만든 변수로 교체한다. 하나 교체할 때마다 테스트한다.