Replace Conditional with Polymorphism (แทนที่เงื่อนไขด้วย Polymorphism)
การแสดงในรูปแบบแผนภาพแสดงตรรกะของคำสั่ง conditional ที่ซับซ้อนถูกแทนที่ด้วย polymorphism code จริงอ่านว่า switch (bird.type) { case 'EuropeanSwallow': return "average"; case 'AfricanSwallow': return (bird.numberOfCoconuts > 2) ? "tired" : "average"; case 'NorwegianBlueParrot': return (bird.voltage > 100) ? "scorched" : "beautiful"; default: return "unknown"; code หลังการ refactoring อ่านว่า class EuropeanSwallow { get plumage() { return "average"; } class AfricanSwallow { get plumage() { return (this.numberOfCoconuts > 2) ? "tired" : "average"; } class NorwegianBlueParrot { get plumage() { return (this.voltage > 100) ? "scorched" : "beautiful"; }
Motivation (แรงจูงใจ)
conditional logic ที่ซับซ้อนเป็นหนึ่งในสิ่งที่ใช้เหตุผลได้ยากที่สุดในการเขียนโปรแกรม ผมจึงมองหาวิธีเพิ่มโครงสร้างให้กับ conditional logic อยู่เสมอ บ่อยครั้ง ผมพบว่าผมสามารถแยกตรรกะออกเป็นสถานการณ์ต่าง ๆ—เคสระดับสูง (high-level cases)— เพื่อแบ่งเงื่อนไขออกจากกัน บางครั้งการแบ่งภายในโครงสร้างของเงื่อนไขเองก็เพียงพอแล้ว แต่การใช้ classes และ polymorphism สามารถทำให้การแยกชัดเจนยิ่งขึ้น
กรณีทั่วไปของเรื่องนี้คือตอนที่ผมสามารถจัดกลุ่มเป็นชุดของ types ซึ่งแต่ละแบบจัดการ conditional logic ต่างกัน ผมอาจสังเกตว่าหนังสือ เพลง และอาหาร ถูกจัดการต่างกันเพราะ type ของมัน สิ่งนี้เห็นได้ชัดที่สุดเมื่อมีหลาย function ที่มี switch statement บน type code ในกรณีนั้น ผมลบ duplication ของ switch logic ส่วนกลางโดยการสร้าง classes สำหรับแต่ละกรณี และใช้ polymorphism เพื่อดึงพฤติกรรมเฉพาะ type ออกมา
อีกสถานการณ์หนึ่งคือตอนที่ผมมองตรรกะได้เป็น base case ที่มี variants อยู่รอบข้าง base case อาจเป็นกรณีที่พบบ่อยที่สุดหรือตรงไปตรงมาที่สุด ผมสามารถใส่ตรรกะนี้ลงใน superclass ซึ่งทำให้ผมใช้เหตุผลเกี่ยวกับมันได้โดยไม่ต้องกังวลกับ variants จากนั้นผมใส่แต่ละ variant case ลงใน subclass ซึ่งผมอธิบายด้วย code ที่เน้นความแตกต่างจาก base case
Polymorphism เป็นหนึ่งในคุณสมบัติสำคัญของ object-oriented programming—และเช่นเดียวกับคุณสมบัติที่มีประโยชน์อื่น ๆ มันก็ถูกใช้งานเกินจำเป็นได้ง่าย ผมเคยเจอคนที่เถียงว่าตัวอย่าง conditional logic ทั้งหมดควรถูกแทนที่ด้วย polymorphism ผมไม่เห็นด้วยกับมุมมองนั้น conditional logic ส่วนใหญ่ของผมใช้คำสั่งเงื่อนไขพื้นฐาน—if/else และ switch/case แต่เมื่อผมเห็น conditional logic ที่ซับซ้อนซึ่งสามารถปรับปรุงได้ดังที่กล่าวข้างต้น ผมพบว่า polymorphism เป็นเครื่องมือที่ทรงพลัง
Mechanics (ขั้นตอนการทำ)
-
ถ้ายังไม่มี classes สำหรับพฤติกรรมแบบ polymorphic ให้สร้างมันขึ้นมาพร้อมกับ factory function เพื่อคืน instance ที่ถูกต้อง
-
ใช้ factory function ใน code ฝั่งผู้เรียก
-
ย้าย conditional function ไปยัง superclass
ถ้า conditional logic ไม่ใช่ฟังก์ชันที่สมบูรณ์ในตัวเอง ให้ใช้ Extract Function ( 106 ) เพื่อทำให้มันเป็นเช่นนั้น
-
เลือก subclass หนึ่งตัว สร้าง method ใน subclass ที่ override method ของคำสั่ง conditional คัดลอก body ของแขนงนั้นของคำสั่ง conditional ลงใน method ของ subclass แล้วปรับมันให้เข้ากัน
-
ทำซ้ำกับทุกแขนงของ conditional
-
ปล่อย default case ไว้ใน method ของ superclass หรือถ้า superclass ควรเป็น abstract ให้ประกาศ method นั้นเป็น abstract หรือ throw error เพื่อแสดงว่ามันควรเป็นหน้าที่ของ subclass
Example (ตัวอย่าง)
เพื่อนของผมมี collection ของนก และอยากรู้ว่านกแต่ละตัวบินได้เร็วแค่ไหนและมีขนแบบไหน เราจึงมีโปรแกรมเล็ก ๆ สองสามโปรแกรมเพื่อหาข้อมูลเหล่านี้
function plumages(birds) {
return new Map(birds.map(b => [b.name, plumage(b)]));
}
function speeds(birds) {
return new Map(birds.map(b => [b.name, airSpeedVelocity(b)]));
}
function plumage(bird) {
switch (bird.type) {
case 'EuropeanSwallow':
return "average";
case 'AfricanSwallow':
return (bird.numberOfCoconuts > 2) ? "tired" : "average";
case 'NorwegianBlueParrot':
return (bird.voltage > 100) ? "scorched" : "beautiful";
default:
return "unknown";
}
}
function airSpeedVelocity(bird) {
switch (bird.type) {
case 'EuropeanSwallow':
return 35;
case 'AfricanSwallow':
return 40 - 2 * bird.numberOfCoconuts;
case 'NorwegianBlueParrot':
return (bird.isNailed) ? 0 : 10 + bird.voltage / 10;
default:
return null;
}
}
เรามี operation ต่าง ๆ หลายแบบที่แปรผันตาม type ของนก ดังนั้นการสร้าง classes และใช้ polymorphism กับพฤติกรรมเฉพาะ type จึงสมเหตุสมผล
ผมเริ่มต้นด้วยการใช้ Combine Functions into Class ( 144 ) กับ airSpeedVelocity และ plumage
function plumage(bird) {
return new Bird(bird).plumage;
}
function airSpeedVelocity(bird) {
return new Bird(bird).airSpeedVelocity;
}
class Bird {
constructor(birdObject) {
Object.assign(this, birdObject);
}
get plumage() {
switch (this.type) {
case 'EuropeanSwallow':
return "average";
case 'AfricanSwallow':
return (this.numberOfCoconuts > 2) ? "tired" : "average";
case 'NorwegianBlueParrot':
return (this.voltage > 100) ? "scorched" : "beautiful";
default:
return "unknown";
}
}
get airSpeedVelocity() {
switch (this.type) {
case 'EuropeanSwallow':
return 35;
case 'AfricanSwallow':
return 40 - 2 * this.numberOfCoconuts;
case 'NorwegianBlueParrot':
return (this.isNailed) ? 0 : 10 + this.voltage / 10;
default:
return null;
}
}
}
ตอนนี้ผมเพิ่ม subclass สำหรับนกแต่ละชนิด พร้อมกับ factory function สำหรับสร้าง instance ของ subclass ที่เหมาะสม
function plumage(bird) {
return createBird(bird).plumage;
}
function airSpeedVelocity(bird) {
return createBird(bird).airSpeedVelocity;
}
function createBird(bird) {
switch (bird.type) {
case 'EuropeanSwallow':
return new EuropeanSwallow(bird);
case 'AfricanSwallow':
return new AfricanSwallow(bird);
case 'NorweigianBlueParrot':
return new NorwegianBlueParrot(bird);
default:
return new Bird(bird);
}
}
class EuropeanSwallow extends Bird {
}
class AfricanSwallow extends Bird {
}
class NorwegianBlueParrot extends Bird {
}
เมื่อสร้างโครงสร้าง class ที่ต้องการแล้ว ผมก็เริ่มจัดการกับ method conditional สองตัวได้ ผมจะเริ่มจาก plumage ผมนำแขนงหนึ่งของ switch statement ไป override ใน subclass ที่เหมาะสม
class EuropeanSwallow…
get plumage() {
return "average";
}
class Bird…
get plumage() {
switch (this.type) {
case 'EuropeanSwallow':
throw "oops";
case 'AfricanSwallow':
return (this.numberOfCoconuts > 2) ? "tired" : "average";
case 'NorwegianBlueParrot':
return (this.voltage > 100) ? "scorched" : "beautiful";
default:
return "unknown";
}
}
ผมใส่ throw ไว้เพราะผมเป็นคนขี้ระแวง
ถึงจุดนี้ผมคอมไพล์และ test ได้ จากนั้น ถ้าทุกอย่างเรียบร้อย ผมก็ทำแขนงถัดไป
class AfricanSwallow…
get plumage() {
return (this.numberOfCoconuts > 2) ? "tired" : "average";
}
จากนั้นก็ถึงตาของ Norwegian Blue:
class NorwegianBlueParrot…
get plumage() {
return (this.voltage > 100) ? "scorched" : "beautiful";
}
ผมปล่อย method ของ superclass ไว้สำหรับ default case
class Bird…
get plumage() {
return "unknown";
}
ผมทำกระบวนการเดียวกันซ้ำกับ airSpeedVelocity พอเสร็จสิ้น ผมก็ได้ code ต่อไปนี้ (ผมยัง inline function ระดับบนสุดของ airSpeedVelocity และ plumage ด้วย):
function plumages(birds) {
return new Map(birds
.map(b => createBird(b))
.map(bird => [bird.name, bird.plumage]));
}
function speeds(birds) {
return new Map(birds
.map(b => createBird(b))
.map(bird => [bird.name, bird.airSpeedVelocity]));
}
function createBird(bird) {
switch (bird.type) {
case 'EuropeanSwallow':
return new EuropeanSwallow(bird);
case 'AfricanSwallow':
return new AfricanSwallow(bird);
case 'NorwegianBlueParrot':
return new NorwegianBlueParrot(bird);
default:
return new Bird(bird);
}
}
class Bird {
constructor(birdObject) {
Object.assign(this, birdObject);
}
get plumage() {
return "unknown";
}
get airSpeedVelocity() {
return null;
}
}
class EuropeanSwallow extends Bird {
get plumage() {
return "average";
}
get airSpeedVelocity() {
return 35;
}
}
class AfricanSwallow extends Bird {
get plumage() {
return (this.numberOfCoconuts > 2) ? "tired" : "average";
}
get airSpeedVelocity() {
return 40 - 2 * this.numberOfCoconuts;
}
}
class NorwegianBlueParrot extends Bird {
get plumage() {
return (this.voltage > 100) ? "scorched" : "beautiful";
}
get airSpeedVelocity() {
return (this.isNailed) ? 0 : 10 + this.voltage / 10;
}
}
เมื่อมอง code สุดท้ายนี้ ผมเห็นว่า superclass Bird ไม่ได้จำเป็นจริง ๆ ใน JavaScript ผมไม่จำเป็นต้องมี type hierarchy เพื่อให้ polymorphism ทำงาน ตราบใดที่ object ของผม implement method ที่มีชื่อถูกต้อง ทุกอย่างก็ทำงานได้ดี อย่างไรก็ตาม ในสถานการณ์นี้ ผมอยากเก็บ superclass ที่ไม่จำเป็นนี้ไว้ เพราะมันช่วยอธิบายความสัมพันธ์ของ classes ในโดเมน
Example: Using Polymorphism for Variation (ตัวอย่าง: การใช้ Polymorphism จัดการความแตกต่าง)
ในตัวอย่างนก ผมใช้ลำดับชั้นการ generalisation ที่ชัดเจน นั่นคือวิธีที่ subclassing และ polymorphism มักถูกอธิบายในหนังสือเรียน (รวมถึงของผมเองด้วย)—แต่มันไม่ใช่วิธีเดียวที่ inheritance ถูกใช้ในทางปฏิบัติ ที่จริงแล้ว มันอาจไม่ใช่วิธีที่พบบ่อยที่สุดหรือดีที่สุดก็ได้ อีกกรณีหนึ่งของการใช้ inheritance คือตอนที่ผมต้องการสื่อว่า object หนึ่งส่วนใหญ่คล้ายกับอีก object หนึ่ง แต่มีความแตกต่างอยู่บ้าง
เพื่อเป็นตัวอย่างของกรณีนี้ ลองพิจารณา code ที่หน่วยงานจัดอันดับความน่าเชื่อถือใช้คำนวณ investment rating สำหรับการเดินเรือของเรือใบ หน่วยงานจะให้ rating เป็น "A" หรือ "B" ขึ้นอยู่กับปัจจัยต่าง ๆ ที่เกี่ยวกับความเสี่ยงและศักยภาพในการทำกำไร ความเสี่ยงมาจากการประเมิน ลักษณะของการเดินเรือ รวมถึงประวัติการเดินเรือครั้งก่อน ๆ ของกัปตัน
function rating(voyage, history) {
const vpf = voyageProfitFactor(voyage, history);
const vr = voyageRisk(voyage);
const chr = captainHistoryRisk(voyage, history);
if (vpf * 3 > (vr + chr * 2)) return "A";
else return "B";
}
function voyageRisk(voyage) {
let result = 1;
if (voyage.length > 4) result += 2;
if (voyage.length > 8) result += voyage.length - 8;
if (["china", "east-indies"].includes(voyage.zone)) result += 4;
return Math.max(result, 0);
}
function captainHistoryRisk(voyage, history) {
let result = 1;
if (history.length < 5) result += 4;
result += history.filter(v => v.profit < 0).length;
if (voyage.zone === "china" && hasChina(history)) result -= 2;
return Math.max(result, 0);
}
function hasChina(history) {
return history.some(v => "china" === v.zone);
}
function voyageProfitFactor(voyage, history) {
let result = 2;
if (voyage.zone === "china") result += 1;
if (voyage.zone === "east-indies") result += 1;
if (voyage.zone === "china" && hasChina(history)) {
result += 3;
if (history.length > 10) result += 1;
if (voyage.length > 12) result += 1;
if (voyage.length > 18) result -= 1;
}
else {
if (history.length > 8) result += 1;
if (voyage.length > 14) result -= 1;
}
return result;
}
function voyageRisk และ captainHistoryRisk ให้คะแนนสำหรับความเสี่ยง voyageProfitFactor ให้คะแนนสำหรับศักยภาพในการทำกำไร และ rating นำทั้งหมดมารวมกันเพื่อให้ rating โดยรวมของการเดินเรือครั้งนั้น
code ฝั่งผู้เรียกน่าจะมีหน้าตาประมาณนี้:
const voyage = {zone: "west-indies", length: 10};
const history = [
{zone: "east-indies", profit: 5},
{zone: "west-indies", profit: 15},
{zone: "china", profit: -2},
{zone: "west-africa", profit: 7},
];
const myRating = rating(voyage, history);
สิ่งที่ผมอยากโฟกัสตรงนี้คือการที่โค้ดสองสามจุดใช้ conditional logic เพื่อจัดการกรณีของการเดินเรือไปจีนที่กัปตันเคยไปจีนมาก่อน
function rating(voyage, history) {
const vpf = voyageProfitFactor(voyage, history);
const vr = voyageRisk(voyage);
const chr = captainHistoryRisk(voyage, history);
if (vpf * 3 > (vr + chr * 2)) return "A";
else return "B";
}
function voyageRisk(voyage) {
let result = 1;
if (voyage.length > 4) result += 2;
if (voyage.length > 8) result += voyage.length - 8;
if (["china", "east-indies"].includes(voyage.zone)) result += 4;
return Math.max(result, 0);
}
function captainHistoryRisk(voyage, history) {
let result = 1;
if (history.length < 5) result += 4;
result += history.filter(v => v.profit < 0).length;
if (voyage.zone === "china" && hasChina(history)) result -= 2;
return Math.max(result, 0);
}
function hasChina(history) {
return history.some(v => "china" === v.zone);
}
function voyageProfitFactor(voyage, history) {
let result = 2;
if (voyage.zone === "china") result += 1;
if (voyage.zone === "east-indies") result += 1;
if (voyage.zone === "china" && hasChina(history)) {
result += 3;
if (history.length > 10) result += 1;
if (voyage.length > 12) result += 1;
if (voyage.length > 18) result -= 1;
}
else {
if (history.length > 8) result += 1;
if (voyage.length > 14) result -= 1;
}
return result;
}
ผมจะใช้ inheritance และ polymorphism เพื่อแยกตรรกะสำหรับจัดการกรณีเหล่านี้ออกจากตรรกะพื้นฐาน ซึ่งเป็น refactoring ที่มีประโยชน์เป็นพิเศษ ถ้าผมกำลังจะเพิ่มตรรกะพิเศษสำหรับกรณีนี้มากขึ้น—เพราะตรรกะสำหรับการเดินเรือไปจีนซ้ำ ๆ แบบนี้สามารถทำให้การเข้าใจกรณีพื้นฐานยากขึ้นได้
ผมเริ่มต้นจากชุดของฟังก์ชัน เพื่อนำ polymorphism มาใช้ ผมต้องสร้างโครงสร้าง class ก่อน ดังนั้นผมจึงเริ่มด้วยการประยุกต์ใช้ Combine Functions into Class ( 144 ) ผลลัพธ์คือ code ต่อไปนี้:
function rating(voyage, history) {
return new Rating(voyage, history).value;
}
class Rating {
constructor(voyage, history) {
this.voyage = voyage;
this.history = history;
}
get value() {
const vpf = this.voyageProfitFactor;
const vr = this.voyageRisk;
const chr = this.captainHistoryRisk;
if (vpf * 3 > (vr + chr * 2)) return "A";
else return "B";
}
get voyageRisk() {
let result = 1;
if (this.voyage.length > 4) result += 2;
if (this.voyage.length > 8) result += this.voyage.length - 8;
if (["china", "east-indies"].includes(this.voyage.zone)) result += 4;
return Math.max(result, 0);
}
get captainHistoryRisk() {
let result = 1;
if (this.history.length < 5) result += 4;
result += this.history.filter(v => v.profit < 0).length;
if (this.voyage.zone === "china" && this.hasChinaHistory) result -= 2;
return Math.max(result, 0);
}
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
if (this.voyage.zone === "china" && this.hasChinaHistory) {
result += 3;
if (this.history.length > 10) result += 1;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
}
else {
if (this.history.length > 8) result += 1;
if (this.voyage.length > 14) result -= 1;
}
return result;
}
get hasChinaHistory() {
return this.history.some(v => "china" === v.zone);
}
}
นั่นทำให้ผมได้ class สำหรับกรณีพื้นฐานแล้ว ตอนนี้ผมต้องสร้าง subclass ที่ว่างเปล่าเพื่อรองรับพฤติกรรมรูปแบบต่าง
class ExperiencedChinaRating extends Rating {
}
จากนั้นผมสร้าง factory function เพื่อคืน class รูปแบบต่างเมื่อจำเป็น
function createRating(voyage, history) {
if (voyage.zone === "china" && history.some(v => "china" === v.zone))
return new ExperiencedChinaRating(voyage, history);
else return new Rating(voyage, history);
}
ผมต้องแก้ caller ทั้งหมดให้ใช้ factory function แทนการเรียก constructor โดยตรง ซึ่งในกรณีนี้ก็คือฟังก์ชัน rating นั่นเอง
function rating(voyage, history) {
return createRating(voyage, history).value;
}
มีพฤติกรรมสองส่วนที่ผมต้องย้ายลงไปใน subclass ผมเริ่มด้วยตรรกะใน captainHistoryRisk :
class Rating…
get captainHistoryRisk() {
let result = 1;
if (this.history.length < 5) result += 4;
result += this.history.filter(v => v.profit < 0).length;
if (this.voyage.zone === "china" && this.hasChinaHistory) result -= 2;
return Math.max(result, 0);
}
ผมเขียน method ที่ override ใน subclass:
class ExperiencedChinaRating
get captainHistoryRisk() {
const result = super.captainHistoryRisk - 2;
return Math.max(result, 0);
}
class Rating…
get captainHistoryRisk() {
let result = 1;
if (this.history.length < 5) result += 4;
result += this.history.filter(v => v.profit < 0).length;
if (this.voyage.zone === "china" && this.hasChinaHistory) result -= 2;
return Math.max(result, 0);
}
การแยกพฤติกรรมรูปแบบต่างออกจาก voyageProfitFactor ค่อนข้างยุ่งยากกว่าเล็กน้อย ผมไม่สามารถแค่ลบพฤติกรรมรูปแบบต่างออกแล้วเรียก method ของ superclass ได้ เพราะมันมีเส้นทางเลือกอยู่ตรงนี้ และผมก็ไม่อยากคัดลอก method ทั้งหมดของ superclass ลงไปใน subclass ด้วย
class Rating…
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
if (this.voyage.zone === "china" && this.hasChinaHistory) {
result += 3;
if (this.history.length > 10) result += 1;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
}
else {
if (this.history.length > 8) result += 1;
if (this.voyage.length > 14) result -= 1;
}
return result;
}
ดังนั้นคำตอบของผมคือเริ่มจากการใช้ Extract Function ( 106 ) กับบล็อก conditional ทั้งหมด
class Rating…
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
result += this.voyageAndHistoryLengthFactor;
return result;
}
get voyageAndHistoryLengthFactor() {
let result = 0;
if (this.voyage.zone === "china" && this.hasChinaHistory) {
result += 3;
if (this.history.length > 10) result += 1;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
}
else {
if (this.history.length > 8) result += 1;
if (this.voyage.length > 14) result -= 1;
}
return result;
}
ชื่อ function ที่มีคำว่า "And" อยู่ในนั้นเป็นกลิ่นโค้ดที่แย่พอสมควร แต่ผมจะปล่อยมันไว้ให้เหม็นสักครู่ ขณะที่ผมลงมือทำ subclassing
class Rating…
get voyageAndHistoryLengthFactor() {
let result = 0;
if (this.history.length > 8) result += 1;
if (this.voyage.length > 14) result -= 1;
return result;
}
class ExperiencedChinaRating…
get voyageAndHistoryLengthFactor() {
let result = 0;
result += 3;
if (this.history.length > 10) result += 1;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
อย่างเป็นทางการ นี่คือจุดสิ้นสุดของ refactoring—ผมได้แยกพฤติกรรมรูปแบบต่างออกไปไว้ใน subclass แล้ว ตรรกะของ superclass เข้าใจและใช้งานได้ง่ายขึ้น และผมต้องจัดการกับกรณีรูปแบบต่างก็ต่อเมื่อทำงานกับ code ของ subclass ซึ่งเขียนออกมาในรูปของความแตกต่างจาก superclass
แต่ผมรู้สึกว่าอย่างน้อยผมควรจะร่างสิ่งที่ผมจะทำกับ method ใหม่ที่ดูเก้งก้างนี้ การสร้าง method ขึ้นมาเพื่อให้ subclass override ล้วน ๆ เป็นเรื่องธรรมดาที่ทำกันตอนใช้ inheritance แบบ base-and-variation แบบนี้ แต่ method เก้งก้างแบบนี้กลับบดบังสิ่งที่เกิดขึ้น แทนที่จะช่วยเปิดเผยมัน
คำว่า "And" เผยให้เห็นว่าจริง ๆ แล้วมีการแก้ไขสองอย่างที่แยกจากกันเกิดขึ้นตรงนี้—ดังนั้นผมคิดว่าการแยกพวกมันออกจากกันเป็นเรื่องฉลาด ผมจะทำโดยใช้ Extract Function ( 106 ) กับการแก้ไขเรื่องความยาวของประวัติ ทั้งใน superclass และ subclass ผมเริ่มจาก superclass ก่อน:
class Rating…
get voyageAndHistoryLengthFactor() {
let result = 0;
result += this.historyLengthFactor;
if (this.voyage.length > 14) result -= 1;
return result;
}
get historyLengthFactor() {
return (this.history.length > 8) ? 1 : 0;
}
ผมทำแบบเดียวกันกับ subclass:
class ExperiencedChinaRating…
get voyageAndHistoryLengthFactor() {
let result = 0;
result += 3;
result += this.historyLengthFactor;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
get historyLengthFactor() {
return (this.history.length > 10) ? 1 : 0;
}
จากนั้นผมใช้ Move Statements to Callers ( 217 ) กับกรณีของ superclass
class Rating…
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
result += this.historyLengthFactor;
result += this.voyageAndHistoryLengthFactor;
return result;
}
get voyageAndHistoryLengthFactor() {
let result = 0;
result += this.historyLengthFactor;
if (this.voyage.length > 14) result -= 1;
return result;
}
class ExperiencedChinaRating…
get voyageAndHistoryLengthFactor() {
let result = 0;
result += 3;
result += this.historyLengthFactor;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
จากนั้นผมจะใช้ Rename Function ( 124 )
class Rating…
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
result += this.historyLengthFactor;
result += this.voyageLengthFactor;
return result;
}
get voyageLengthFactor() {
return (this.voyage.length > 14) ? - 1: 0;
}
เปลี่ยนเป็น ternary เพื่อทำให้ voyageLengthFactor ง่ายขึ้น
class ExperiencedChinaRating…
get voyageLengthFactor() {
let result = 0;
result += 3;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
สุดท้ายอีกอย่างหนึ่ง ผมไม่คิดว่าการเพิ่ม 3 คะแนนสมควรเป็นส่วนหนึ่งของ voyage length factor—มันควรถูกเพิ่มเข้าไปในผลลัพธ์โดยรวมมากกว่า
class ExperiencedChinaRating…
get voyageProfitFactor() {
return super.voyageProfitFactor + 3;
}
get voyageLengthFactor() {
let result = 0;
result += 3;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
เมื่อสิ้นสุด refactoring ผมได้ code ต่อไปนี้ อย่างแรก คือ class rating พื้นฐานซึ่งสามารถเพิกเฉยต่อความยุ่งยากใด ๆ ของกรณี Experienced China ได้:
class Rating {
constructor(voyage, history) {
this.voyage = voyage;
this.history = history;
}
get value() {
const vpf = this.voyageProfitFactor;
const vr = this.voyageRisk;
const chr = this.captainHistoryRisk;
if (vpf * 3 > (vr + chr * 2)) return "A";
else return "B";
}
get voyageRisk() {
let result = 1;
if (this.voyage.length > 4) result += 2;
if (this.voyage.length > 8) result += this.voyage.length - 8;
if (["china", "east-indies"].includes(this.voyage.zone)) result += 4;
return Math.max(result, 0);
}
get captainHistoryRisk() {
let result = 1;
if (this.history.length < 5) result += 4;
result += this.history.filter(v => v.profit < 0).length;
return Math.max(result, 0);
}
get voyageProfitFactor() {
let result = 2;
if (this.voyage.zone === "china") result += 1;
if (this.voyage.zone === "east-indies") result += 1;
result += this.historyLengthFactor;
result += this.voyageLengthFactor;
return result;
}
get voyageLengthFactor() {
return (this.voyage.length > 14) ? - 1: 0;
}
get historyLengthFactor() {
return (this.history.length > 8) ? 1 : 0;
}
}
code สำหรับกรณี Experienced China อ่านออกมาเป็นชุดของความแตกต่างจากกรณีพื้นฐาน:
class ExperiencedChinaRating extends Rating {
get captainHistoryRisk() {
const result = super.captainHistoryRisk - 2;
return Math.max(result, 0);
}
get voyageLengthFactor() {
let result = 0;
if (this.voyage.length > 12) result += 1;
if (this.voyage.length > 18) result -= 1;
return result;
}
get historyLengthFactor() {
return (this.history.length > 10) ? 1 : 0;
}
get voyageProfitFactor() {
return super.voyageProfitFactor + 3;
}
}