return order.quantity * order.itemPrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.max(order.quantity * order.itemPrice * 0.1, 100);
const basePrice order.quantity * ordeer.itemPrice;
const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
const shipping = Math.max(order.quantity * order.itemPrice * 0.1, 100);
return basePrice - quantityDiscount + shipping;
표현식이 너무 복잡해서 이해하기 어려운 경우 지역변수를 활용하여 표현식을 쪼개 관리하기 쉽게 만들 수 있다. 이 과정에서 추가한 변수는 디버깅에 도움이 된다.
function price(order) {
// 가격(price) = 기본 가격 - 수량 할인 + 배송비
return order.quantity * order.itemPrice - Math.max(0, order.quantity - 500)
* order.itemPrice * 0.05
+ Math.min(order.quantity * order.itemPrice * 0.1, 100);
}
function price(order) {
// 가격(price) = 기본 가격 - 수량 할인 + 배송비
return order.quantity * order.itemPrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.min(order.quantity * order.itemPrice * 0.1, 100);
}
function price(order) {
// 가격(price) = 기본 가격 - 수량 할인 + 배송비
const basePrice = order.quantity * order.itemPrice;
return basePrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.min(basePrice * 0.1, 100);
}
function price(order) {
const basePrice = order.quantity * order.itemPrice;
const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
const shipping = Math.min(basePrice * 0.1, 100);
return basePrice - quantityDiscount + shipping;
}
주석을 지워준다. 코드로 드러나기 때문이다.