Status: Separated into Two Files (and Phases) (สถานะ: แยกออกเป็นสองไฟล์ (และสองเฟส))
นี่เป็นจังหวะดีที่จะประเมินสถานการณ์อีกครั้งและคิดว่า code ตอนนี้อยู่ตรงไหน ตอนนี้ผมมี code สองไฟล์
statement.js
import createStatementData from './createStatementData.js';
function statement (invoice, plays) {
return renderPlainText(createStatementData(invoice, plays));
}
function renderPlainText(data, plays) {
let result = `Statement for ${data.customer}\n`;
for (let perf of data.performances) {
result += ` ${perf.play.name}: ${usd(perf.amount)} (${perf.audience} seats)\n`;
}
result += `Amount owed is ${usd(data.totalAmount)}\n`;
result += `You earned ${data.totalVolumeCredits} credits\n`;
return result;
}
function htmlStatement (invoice, plays) {
return renderHtml(createStatementData(invoice, plays));
}
function renderHtml (data) {
let result = `<h1>Statement for ${data.customer}</h1>\n`;
result += "<table>\n";
result += "<tr><th>play</th><th>seats</th><th>cost</th></tr>";
for (let perf of data.performances) {
result += ` <tr><td>${perf.play.name}</td><td>${perf.audience}</td>`;
result += `<td>${usd(perf.amount)}</td></tr>\n`;
}
result += "</table>\n";
result += `<p>Amount owed is <em>${usd(data.totalAmount)}</em></p>\n`;
result += `<p>You earned <em>${data.totalVolumeCredits}</em> credits</p>\n`;
return result;
}
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
createStatementData.js
export default function createStatementData(invoice, plays) {
const result = {};
result.customer = invoice.customer;
result.performances = invoice.performances.map(enrichPerformance);
result.totalAmount = totalAmount(result);
result.totalVolumeCredits = totalVolumeCredits(result);
return result;
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result);
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID]
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.play.type) {
case "tragedy":
result = 40000;
if (aPerformance.audience > 30) {
result += 1000 * (aPerformance.audience - 30);
}
break;
case "comedy":
result = 30000;
if (aPerformance.audience > 20) {
result += 10000 + 500 * (aPerformance.audience - 20);
}
result += 300 * aPerformance.audience;
break;
default:
throw new Error(`unknown type: ${aPerformance.play.type}`);
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === aPerformance.play.type) result += Math.floor(aPerformance.audience / 5);
return result;
}
function totalAmount(data) {
return data.performances
.reduce((total, p) => total + p.amount, 0);
}
function totalVolumeCredits(data) {
return data.performances
.reduce((total, p) => total + p.volumeCredits, 0);
}
ตอนนี้ผมมี code มากกว่าตอนเริ่มต้น: 70 บรรทัด (ไม่นับ htmlStatement ) เทียบกับ 44 บรรทัด สาเหตุหลักมาจากการห่อหุ้มเพิ่มเติมที่จำเป็นเมื่อย้ายอะไร ๆ เข้าไปไว้ใน function ถ้าทุกอย่างเท่ากัน code ที่มากกว่าคือสิ่งที่ไม่ดี—แต่แทบไม่มีครั้งไหนที่ทุกอย่างเท่ากัน code ที่เพิ่มขึ้นนี้แบ่งตรรกะออกเป็นส่วนที่ระบุตัวตนได้ โดยแยกการคำนวณของ statement ออกจากส่วน layout modularity นี้ทำให้ผมเข้าใจส่วนต่าง ๆ ของ code และการประกอบเข้าด้วยกันได้ง่ายขึ้น Brevity คือหัวใจของปัญญา แต่ความชัดเจนคือหัวใจของ software ที่วิวัฒนาการได้ การเพิ่ม modularity นี้ทำให้ผมรองรับเวอร์ชัน HTML ของ code ได้โดยไม่มี duplication ของการคำนวณเลย
เวลาเขียนโปรแกรม จงทำตามกฎแคมป์ปิ้ง: ทิ้ง code base ให้แข็งแรงกว่าเดิมเสมอ อย่าทิ้งให้แย่กว่าที่เจอมา
ยังมีอีกหลายอย่างที่ผมทำได้เพื่อทำให้ตรรกะการพิมพ์ง่ายขึ้น แต่แค่นี้ก็พอสำหรับตอนนี้ ผมต้องหาสมดุลระหว่าง refactoring ทั้งหมดที่ทำได้กับการเพิ่ม feature ใหม่เสมอ ในตอนนี้ คนส่วนใหญ่ให้ความสำคัญกับ refactoring ต่ำเกินไป—แต่ก็ยังต้องมีสมดุลอยู่ดี กฎของผมคือกฎแคมป์ปิ้งฉบับปรับปรุง: ทิ้ง code base ให้แข็งแรงกว่าเดิมเสมอ มันจะไม่มีวันสมบูรณ์แบบ แต่มันควรดีขึ้น