2021.10.18
목적과 구현을 분리
하는 것.어떻게
가 아닌 무엇을
하는지가 드러나야 한다 )before
function printOwing(invoice) {let outstanding = 0;console.log('*****************');console.log('**** 고객 채무 ****');console.log('*****************');// 미해결 채무(outstanding)를 계산합니다.for (const o of invoice.orders) {outstanding += o.amount;}// 마감일(dueDate)을 기록합니다.const today = Clock.today;invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);// 세부 사항을 출력합니다.console.log(`고객명: ${invoice.customer}`);console.log(`채무액: ${outstanding}`);console.log(`마감일: ${invoice.dueDate.toLocaleDateString()}`);}
after
function printOwing(invoice) {printBanner();let outstanding = calculateOutStanding(invoice);recordDueDate(invoice);printDetails(invoice, outstanding);}function printBanner() {console.log('*****************');console.log('**** 고객 채무 ****');console.log('*****************');}function calculateOutStanding(invoice) {let result = 0;for (const o of invoice.orders) {result += o.amount;}return result;}function recordDueDate(invoice) {// 마감일(dueDate)을 기록합니다.const today = Clock.today;invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);}function printDetails(invoice, outstanding) {// 세부 사항을 출력합니다.console.log(`고객명: ${invoice.customer}`);console.log(`채무액: ${outstanding}`);console.log(`마감일: ${invoice.dueDate.toLocaleDateString()}`);}
before
function getRating(driver) {return moreThanFiveLateDeliveries(driver) ? 2 : 1;}function moreThanFiveLateDeliveries(driver) {return driver.numberOfLateDeliveries > 5;}
after
function getRating(driver) {return driver.numberOfLateDeliveries > 5 ? 2 : 1;}
before
return (order.quantity * order.itemPrice -Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +Math.min(order.quantity * order.itemPrice * 0.1, 100));
after
const basePrice = order.quantity * order.itemPrice;const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;const shipping = Math.min(order.quantity * order.itemPrice * 0.1, 100);return basePrice - quantityDiscount + shipping;
before
let basePrice = anOrder.basePrice;return basePrice > 1000;
after
return anOrder.basePrice > 1000;
간단한 절차
호출하는 곳이 많거나, 호출 과정이 복잡하거나, 호출 대상이 다형 메서드거나 선언을 복잡하게 변경할 때 사용
마이그레이션 절차
변경할 것이 둘 이상이면 나눠서 처리할 때가 나을 때가 있으며, 이름 변경과 매개변수 추가를 모두 하고 싶다면 각각을 독립적으로 처리. (이후 문제가 생길 시 돌리기 위해서)
간단한 절차
를 따라 추가한다.before
function circum(radius) {return 2 * Math.PI * radius;}
after
function circumference(radius) {return 2 * Math.PI * radius;}
before
let defaultOwner = { firstName: '마틴', lastName: '파울러' };
after
class Person {constructor(data) {this._lastName = data.lastName;this._firstName = data.firstName;}get lastName() {return this._lastName;}get firstName() {return this._firstName;}}
before
let a = height * width;
after
let area = height * width;
before
function amountInvoiced(startDate, endDate) {}function amountReceived(startDate, endDate) {}function amountOverdue(startDate, endDate) {}
after
function amountInvoiced(aDateRange) {}function amountReceived(aDateRange) {}function amountOverdue(aDateRange) {}
before
function base(aReading) {}function taxableCharge(aReading) {}function calculateBaseCharge(aReading) {}
after
class Reading {base() {}taxableCharge() {}calculateBaseCharge() {}}
before
function base(aReading) {}function taxableCharge(aReading) {}
after
function enrichReading(argReading) {const aReading = _.cloneDeep(argReading);aReading.baseCharge = base(aReading);aReading.taxableCharge = taxableCharge(aReading);return aReading;}
before
const orderDate = orderString.split(/\s+/);const productPrice = priceList[orderDate[0].split('-')[1]];const orderPrice = parseInt(orderDate[1]) * productPrice;
after
const orderRecord = parseOrder(order);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];}