function printOwing(invoice) {
let outstanding = 0;
// banner
console.log("***********************");
console.log("**** Customer Owes ****");
console.log("***********************");
// calculate outstanding
for (const o of invoice.orders) {
outstanding += o.amount;
}
// record due date
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
//print details
console.log(`name: ${invoice.customer}`);
console.log(`amount: ${outstanding}`);
console.log(`due: ${invoice.dueDate.toLocaleDateString()}`);
}
无局部变量
直接提取代码成一个函数,再调用此函数
代码:banner函数
function printOwing(invoice) {
let outstanding = 0;
printBanner();
// calculate outstanding
for (const o of invoice.orders) {
outstanding += o.amount;
}
// record due date
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
//print details
console.log(`name: ${invoice.customer}`);
console.log(`amount: ${outstanding}`);
console.log(`due: ${invoice.dueDate.toLocaleDateString()}`);
}
function printBanner() {
console.log("***********************");
console.log("**** Customer Owes ****");
console.log("***********************");
}
有局部变量
被提炼代码段只是读取这些变量的值,并不修改它们。这种情况下我可以简单地将它们当作参数传给目标函数
代码:打印详情函数,加日期函数
function printOwing(invoice) {
let outstanding = 0;
printBanner();
// calculate outstanding
for (const o of invoice.orders) {
outstanding += o.amount;
}
// record due date
recordDueDate(invoice);
// print Details
printDetails(invoice, outstanding);
}
function recordDueDate(invoice) {
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
}
function printOwing(invoice) {
printBanner();
// calculate outstanding
let outstanding = calculateOutstanding(invoice);
// record due date
recordDueDate(invoice);
// print Details
printDetails(invoice, outstanding);
}
function recordDueDate(invoice) {
const today = Clock.today;
invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
}
第一组重构
提炼函数
动机
做法
示例
无局部变量
有局部变量
function recordDueDate(invoice) { const today = Clock.today; invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30); }
function printDetails(invoice, outstanding) { console.log(
name: ${invoice.customer}
); console.log(amount: ${outstanding}
); console.log(due: ${invoice.dueDate.toLocaleDateString()}
); }function printOwing(invoice) { printBanner(); // calculate outstanding let outstanding = calculateOutstanding(invoice); // record due date recordDueDate(invoice); // print Details printDetails(invoice, outstanding); }
function recordDueDate(invoice) { const today = Clock.today; invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30); }
function printDetails(invoice, outstanding) { console.log(
name: ${invoice.customer}
); console.log(amount: ${outstanding}
); console.log(due: ${invoice.dueDate.toLocaleDateString()}
); }function calculateOutstanding(invoice) { let outstanding = 0; for (const o of invoice.orders) { outstanding += o.amount; } return outstanding; }
function reportLines(aCustomer) { const lines = []; gatherCustomerData(lines, aCustomer); return lines; }
function gatherCustomerData(out, aCustomer) { out.push(["name", aCustomer.name]); out.push(["location", aCustomer.location]); }
function reportLines(aCustomer) { const lines = []; lines.push(["name", aCustomer.name]); lines.push(["location", aCustomer.location]); return lines; }
class Order { constructor(aRecord) { this._data = aRecord; }
}
class Order { constructor(aRecord) { this._data = aRecord; }
}