Replace Derived Variable with Query (แทน Derived Variable ด้วย Query)
การแสดงในรูปแบบแผนภาพแสดงตัวแปรสองสามตัว โดย derived variable ถูกแทนที่ด้วยชุดของ query code จริงอ่านว่า get discountedTotal () {return this._discountedTotal;} set discount (aNumber) { const old = this._discount; this.discount = aNumber; this._discountedTotal += old - aNumber; } code หลังการ refactoring อ่านว่า get discountedTotal () {return this._baseTotal - this._discount;} set discount (aNumber) {this._discount = aNumber;}
Motivation (แรงจูงใจ)
หนึ่งในแหล่งปัญหาที่ใหญ่ที่สุดใน software คือ mutable data การเปลี่ยนแปลงของข้อมูลมักทำให้ code หลายส่วนผูกพันกันอย่างไม่เป็นธรรมชาติ โดยการเปลี่ยนส่วนหนึ่งนำไปสู่ผลกระทบลูกโซ่ที่ยากจะมองเห็น ในหลายสถานการณ์ การกำจัด mutable data ออกให้หมดนั้นไม่สมจริง แต่ผมสนับสนุนให้ลด scope ของ mutable data ให้มากที่สุดเท่าที่จะทำได้
วิธีหนึ่งที่สร้างผลกระทบใหญ่หลวงได้คือการลบ variable ที่เราสามารถคำนวณค่าได้โดยง่ายออกไป การคำนวณมักช่วยให้ความหมายของข้อมูลชัดเจนขึ้น และข้อมูลจะปลอดภัยจากการถูกทำให้เสียหายเมื่อคุณลืมอัปเดต variable ตามข้อมูลต้นทางที่เปลี่ยนไป
ข้อยกเว้นที่สมเหตุสมผลสำหรับแนวคิดนี้คือเมื่อข้อมูลต้นทางของการคำนวณเป็น immutable และเราสามารถบังคับให้ผลลัพธ์เป็น immutable ด้วยเช่นกัน ดังนั้น operation แบบ transformation ที่สร้าง data structure ใหม่จึงสมเหตุสมผลที่จะเก็บไว้ แม้จะสามารถแทนที่ด้วยการคำนวณก็ตาม จริง ๆ แล้วตรงนี้มีความเป็นคู่ (duality) ระหว่าง object ที่ห่อหุ้ม data structure ด้วยชุดของ calculated properties กับ function ที่ transform data structure หนึ่งไปเป็นอีก data structure หนึ่ง แนวทางแบบ object ดีกว่าอย่างชัดเจนเมื่อข้อมูลต้นทางมีการเปลี่ยนแปลง เพราะคุณจะต้องจัดการอายุขัยของ derived data structures แต่ถ้าข้อมูลต้นทางเป็น immutable หรือ derived data มีอายุสั้นมาก ทั้งสองแนวทางก็มีประสิทธิภาพเท่า ๆ กัน
Mechanics (ขั้นตอนการทำ)
-
ระบุจุดอัปเดตทั้งหมดของตัวแปร ถ้าจำเป็น ให้ใช้ Split Variable ( 240 ) เพื่อแยกจุดอัปเดตแต่ละจุดออกจากกัน
-
สร้าง function ที่คำนวณค่าของ variable
-
ใช้ Introduce Assertion ( 302 ) เพื่อยืนยันว่า variable และการคำนวณให้ผลลัพธ์เดียวกันทุกครั้งที่มีการใช้ variable
ถ้าจำเป็น ให้ใช้ Encapsulate Variable ( 132 ) เพื่อสร้างที่อยู่ให้กับ assertion
-
test
-
แทนที่การอ่านค่า variable ทุกจุดด้วยการเรียก function ใหม่
-
test
-
ใช้ Remove Dead Code ( 237 ) กับ declaration และการอัปเดต variable
Example (ตัวอย่าง)
นี่คือตัวอย่างความน่าเกลียดเล็ก ๆ แต่สมบูรณ์แบบ:
class ProductionPlan…
get production() {return this._production;}
applyAdjustment(anAdjustment) {
this._adjustments.push(anAdjustment);
this._production += anAdjustment.amount;
}
ความน่าเกลียดอยู่ในสายตาของผู้มอง สำหรับผม ความน่าเกลียดที่เห็นตรงนี้คือ duplication—ไม่ใช่ duplication ของ code แบบทั่วไป แต่เป็น duplication ของข้อมูล เมื่อผม apply adjustment ผมไม่ได้แค่เก็บ adjustment นั้นไว้ แต่ยังใช้มันปรับค่า accumulator ด้วย ซึ่งจริง ๆ แล้วผมแค่คำนวณค่านั้นได้โดยไม่ต้องอัปเดตมัน
แต่ผมเป็นคนระมัดระวัง สมมติฐานของผมคือเราสามารถคำนวณมันได้—และผมสามารถ test สมมติฐานนั้นได้โดยใช้ Introduce Assertion ( 302 ) :
class ProductionPlan…
get production() {
assert(this._production === this.calculatedProduction);
return this._production;
}
get calculatedProduction() {
return this._adjustments
.reduce((sum, a) => sum + a.amount, 0);
}
เมื่อใส่ assertion แล้ว ผมก็รัน test ถ้า assertion ไม่ล้มเหลว ผมก็สามารถเปลี่ยนจากการ return field เป็นการ return ค่าที่คำนวณได้:
class ProductionPlan…
get production() {
assert(this._production === this.calculatedProduction);
return this.calculatedProduction;
}
จากนั้นก็ Inline Function ( 115 ) :
class ProductionPlan…
get production() {
return this._adjustments
.reduce((sum, a) => sum + a.amount, 0);
}
ผมทำความสะอาดการอ้างอิงทั้งหมดไปยัง variable เก่าด้วย Remove Dead Code ( 237 ) :
class ProductionPlan…
applyAdjustment(anAdjustment) {
this._adjustments.push(anAdjustment);
this._production += anAdjustment.amount;
}
Example: More Than One Source (ตัวอย่าง: แหล่งข้อมูลมากกว่าหนึ่งแหล่ง)
ตัวอย่างข้างต้นง่ายและสะดวก เพราะมีแหล่งข้อมูลเดียวที่ชัดเจนสำหรับค่าของ production แต่บางครั้ง มีองค์ประกอบมากกว่าหนึ่งอย่างที่มารวมกันใน accumulator
class ProductionPlan…
constructor (production) {
this._production = production;
this._adjustments = [];
}
get production() {return this._production;}
applyAdjustment(anAdjustment) {
this._adjustments.push(anAdjustment);
this._production += anAdjustment.amount;
}
ถ้าผมทำ Introduce Assertion ( 302 ) แบบเดียวกับที่ทำข้างต้น มันจะล้มเหลวในทุกกรณีที่ค่าเริ่มต้นของ production ไม่ใช่ศูนย์
แต่ผมยังสามารถแทนที่ derived data ได้ ข้อแตกต่างเพียงอย่างเดียวคือผมต้องใช้ Split Variable ( 240 ) ก่อน
constructor (production) {
this._initialProduction = production;
this._productionAccumulator = 0;
this._adjustments = [];
}
get production() {
return this._initialProduction + this._productionAccumulator;
}
ตอนนี้ผมสามารถ Introduce Assertion ( 302 ) ได้:
class ProductionPlan…
get production() {
assert(this._productionAccumulator === this.calculatedProductionAccumulator);
return this._initialProduction + this._productionAccumulator;
}
get calculatedProductionAccumulator() {
return this._adjustments
.reduce((sum, a) => sum + a.amount, 0);
}
แล้วก็ทำต่อไปเหมือนเดิมเป็นส่วนใหญ่ อย่างไรก็ตาม ผมมักจะปล่อยให้ totalProductionAjustments เป็น property ของตัวเอง โดยไม่ inline มัน