Replace Function with Command (แทน Function ด้วย Command)
เดิมเรียกว่า: Replace Method with Method Object
เป็นการย้อนกลับของ: Replace Command with Function ( 344 )
การแสดงในรูปแบบแผนภาพแสดง method พร้อม object ของมันถูกแทนที่ด้วย command โค้ดจริงอ่านว่า function score(candidate, medicalExam, scoringGuide) { let result = 0; let healthLevel = 0; // long body code } โค้ดหลังการ refactoring อ่านว่า class Scorer { constructor(candidate, medicalExam, scoringGuide) { this._candidate = candidate; this._medicalExam = medicalExam; this._scoringGuide = scoringGuide; } execute() { this._result = 0; this._healthLevel = 0; // long body code } }
Motivation (แรงจูงใจ)
Function—ไม่ว่าจะลอยอิสระหรือผูกกับ object ในฐานะ method—เป็นหนึ่งในส่วนประกอบพื้นฐานของการเขียนโปรแกรม แต่ก็มีบางเวลาที่การห่อหุ้ม function ไว้ใน object ของมันเองนั้นมีประโยชน์ ซึ่งผมเรียกมันว่า "command object" หรือเรียกสั้น ๆ ว่า command object แบบนี้ส่วนใหญ่สร้างขึ้นรอบ method เดียว ซึ่งคำขอและการประมวลผลของมันคือจุดประสงค์ของ object
command ให้ความยืดหยุ่นในการควบคุมและแสดงออกของ function มากกว่ากลไก function ทั่วไป command สามารถมี operation เสริมได้ เช่น undo ผมสามารถสร้าง method ที่ค่อย ๆ ประกอบ parameter ของมันขึ้นมาเพื่อ รองรับ lifecycle ที่สมบูรณ์ขึ้น ผมสามารถต่อเติม customization ได้ด้วย inheritance และ hooks ถ้าผมทำงานในภาษาที่มี object แต่ไม่มี first-class functions ผมก็สามารถให้ความสามารถส่วนใหญ่เหล่านั้นได้โดยใช้ command แทน ในทำนองเดียวกัน ผมสามารถใช้ method และ field ช่วยย่อยสลาย function ที่ซับซ้อน ได้แม้ในภาษาที่ไม่มี nested functions และผมสามารถเรียก method เหล่านั้นโดยตรงระหว่าง test และ debugging
ทั้งหมดนี้เป็นเหตุผลที่ดีในการใช้ command และผมต้องพร้อมที่จะ refactor function ให้เป็น command เมื่อจำเป็น แต่เราต้องไม่ลืมว่าความยืดหยุ่นนี้ก็มาพร้อมราคาที่ต้องจ่ายในรูปของความซับซ้อนเช่นเคย ดังนั้น เมื่อมีตัวเลือกระหว่าง first-class function กับ command ผมจะเลือก function 95 เปอร์เซ็นต์ของเวลาทั้งหมด ผมใช้ command ก็ต่อเมื่อต้องการสิ่งอำนวยความสะดวกที่วิธีที่ง่ายกว่าไม่สามารถให้ได้โดยเฉพาะเจาะจง
เช่นเดียวกับคำหลายคำในการพัฒนา software "command" ค่อนข้างมีความหมายทับซ้อนหลายอย่าง ในบริบทที่ผมใช้มันตรงนี้ มันคือ object ที่ห่อหุ้มคำขอ ตาม command pattern ใน Design Patterns [ gof ] . เมื่อผมใช้ "command" ในความหมายนี้ ผมจะใช้ "command object" เพื่อกำหนดบริบท แล้วจึงใช้ "command" ตามหลัง คำว่า "command" ยังถูกใช้ในหลักการ command-query separation [ mf-cqs ] ด้วย โดยที่ command คือ object method ที่เปลี่ยนสถานะที่สังเกตเห็นได้ ผมพยายามหลีกเลี่ยงการใช้ command ในความหมายนั้นมาโดยตลอด โดยเลือกใช้ "modifier" หรือ "mutator" แทน
Mechanics (ขั้นตอนการทำ)
-
สร้าง class ว่างสำหรับ function โดยตั้งชื่อ class ตาม function
-
ใช้ Move Function ( 198 ) เพื่อย้าย function ไปยัง class ว่างนั้น
เก็บ function เดิมไว้เป็น forwarding function จนถึงอย่างน้อยตอนจบของการ refactoring
ทำตามธรรมเนียมที่ภาษานั้น ๆ มีสำหรับการตั้งชื่อ command ถ้าไม่มีธรรมเนียม ให้เลือกชื่อทั่ว ๆ ไป สำหรับ execute function ของ command เช่น "execute" หรือ "call"
-
ลองพิจารณาสร้าง field สำหรับแต่ละ argument และย้าย argument เหล่านี้ไปที่ constructor
Example (ตัวอย่าง)
ภาษา JavaScript มีข้อบกพร่องหลายอย่าง แต่หนึ่งในการตัดสินใจที่ยอดเยี่ยมของมันคือการทำให้ function เป็นพลเมืองชั้นหนึ่ง (first-class) ดังนั้นผมจึงไม่ต้องฝ่าฟันอุปสรรคทั้งหมดของการสร้าง command สำหรับงานทั่ว ๆ ไปที่ผมต้องทำในภาษาที่ไม่มีความสามารถนี้ แต่ก็ยังมีบางเวลาที่ command คือเครื่องมือที่เหมาะสมกับงาน
หนึ่งในกรณีเหล่านั้นคือการย่อยสลาย function ที่ซับซ้อนเพื่อให้ผมเข้าใจและแก้ไขมันได้ดีขึ้น เพื่อแสดงคุณค่าของการ refactoring นี้จริง ๆ ผมต้องมี function ที่ยาวและ ซับซ้อน—แต่นั่นคงใช้เวลานานเกินไปในการเขียน แถมคุณก็คงไม่อยากอ่าน ผมจึงเลือกใช้ function ที่สั้นพอจนไม่จำเป็นต้องใช้มัน ฟังก์ชันนี้ให้คะแนนสำหรับใบสมัครประกัน:
function score(candidate, medicalExam, scoringGuide) {
let result = 0;
let healthLevel = 0;
let highMedicalRiskFlag = false;
if (medicalExam.isSmoker) {
healthLevel += 10;
highMedicalRiskFlag = true;
}
let certificationGrade = "regular";
if (scoringGuide.stateWithLowCertification(candidate.originState)) {
certificationGrade = "low";
result -= 5;
}
// lots more code like this
result -= Math.max(healthLevel - 5, 0);
return result;
}
ผมเริ่มต้นด้วยการสร้าง class ว่าง จากนั้นใช้ Move Function ( 198 ) เพื่อย้าย function เข้าไปใน class นั้น
function score(candidate, medicalExam, scoringGuide) {
return new Scorer().execute(candidate, medicalExam, scoringGuide);
}
class Scorer {
execute (candidate, medicalExam, scoringGuide) {
let result = 0;
let healthLevel = 0;
let highMedicalRiskFlag = false;
if (medicalExam.isSmoker) {
healthLevel += 10;
highMedicalRiskFlag = true;
}
let certificationGrade = "regular";
if (scoringGuide.stateWithLowCertification(candidate.originState)) {
certificationGrade = "low";
result -= 5;
}
// lots more code like this
result -= Math.max(healthLevel - 5, 0);
return result;
}
}
บ่อยครั้งที่ผมชอบส่ง argument ให้ command ที่ constructor และให้ method execute ไม่รับ parameter แม้จะไม่สำคัญนักสำหรับการย่อยสลายแบบง่าย ๆ อย่างกรณีนี้ แต่มันมีประโยชน์มากเมื่อผมต้องการจัดการ command ด้วย lifecycle การตั้งค่า parameter ที่ซับซ้อนขึ้นหรือ customization class command ที่ต่างกันสามารถมี parameter ต่างกันได้ แต่สามารถปะปนกันได้เมื่อถูกจัดคิวเพื่อประมวลผล
ผมสามารถทำ parameter เหล่านี้ทีละตัวได้
function score(candidate, medicalExam, scoringGuide) {
return new Scorer(candidate).execute(candidate, medicalExam, scoringGuide);
}
class Scorer…
constructor(candidate){
this._candidate = candidate;
}
execute (candidate, medicalExam, scoringGuide) {
let result = 0;
let healthLevel = 0;
let highMedicalRiskFlag = false;
if (medicalExam.isSmoker) {
healthLevel += 10;
highMedicalRiskFlag = true;
}
let certificationGrade = "regular";
if (scoringGuide.stateWithLowCertification(this._candidate.originState)) {
certificationGrade = "low";
result -= 5;
}
// lots more code like this
result -= Math.max(healthLevel - 5, 0);
return result;
}
ผมทำต่อกับ parameter อื่น ๆ
function score(candidate, medicalExam, scoringGuide) {
return new Scorer(candidate, medicalExam, scoringGuide).execute();
}
class Scorer…
constructor(candidate, medicalExam, scoringGuide){
this._candidate = candidate;
this._medicalExam = medicalExam;
this._scoringGuide = scoringGuide;
}
execute () {
let result = 0;
let healthLevel = 0;
let highMedicalRiskFlag = false;
if (this._medicalExam.isSmoker) {
healthLevel += 10;
highMedicalRiskFlag = true;
}
let certificationGrade = "regular";
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
certificationGrade = "low";
result -= 5;
}
// lots more code like this
result -= Math.max(healthLevel - 5, 0);
return result;
}
นั่นคือการทำ Replace Function with Command ให้เสร็จสมบูรณ์ แต่ประเด็นทั้งหมดของการทำ refactoring นี้คือการเปิดทางให้ผม ย่อยสลาย function ที่ซับซ้อนได้—ดังนั้นขอให้ผมสรุปขั้นตอนบางอย่างเพื่อให้บรรลุเป้าหมายนั้น ขั้นต่อไปของผมคือการเปลี่ยน local variable ทั้งหมดให้เป็น field โดยทำทีละตัวเหมือนเดิม
class Scorer…
constructor(candidate, medicalExam, scoringGuide){
this._candidate = candidate;
this._medicalExam = medicalExam;
this._scoringGuide = scoringGuide;
}
execute () {
this._result = 0;
let healthLevel = 0;
let highMedicalRiskFlag = false;
if (this._medicalExam.isSmoker) {
healthLevel += 10;
highMedicalRiskFlag = true;
}
let certificationGrade = "regular";
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
certificationGrade = "low";
this._result -= 5;
}
// lots more code like this
this._result -= Math.max(healthLevel - 5, 0);
return this._result;
}
ผมทำซ้ำแบบนี้กับ local variable ทั้งหมด (นี่เป็นหนึ่งใน refactoring ที่ผมรู้สึกว่ามันง่ายพอ จนไม่ได้ให้มันมีรายการในแคตตาล็อกนี้ ซึ่งผมรู้สึกผิดเล็กน้อย)
class Scorer…
constructor(candidate, medicalExam, scoringGuide){
this._candidate = candidate;
this._medicalExam = medicalExam;
this._scoringGuide = scoringGuide;
}
execute () {
this._result = 0;
this._healthLevel = 0;
this._highMedicalRiskFlag = false;
if (this._medicalExam.isSmoker) {
this._healthLevel += 10;
this._highMedicalRiskFlag = true;
}
this._certificationGrade = "regular";
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
this._certificationGrade = "low";
this._result -= 5;
}
// lots more code like this
this._result -= Math.max(this._healthLevel - 5, 0);
return this._result;
}
เมื่อผมย้าย state ทั้งหมดของ function ไปไว้ที่ command object แล้ว ผมก็สามารถใช้ refactoring อย่างเช่น Extract Function ( 106 ) ได้โดยไม่ต้องพัวพันกับ variable และ scope ทั้งหมดของมัน
class Scorer…
execute () {
this._result = 0;
this._healthLevel = 0;
this._highMedicalRiskFlag = false;
this.scoreSmoking();
this._certificationGrade = "regular";
if (this._scoringGuide.stateWithLowCertification(this._candidate.originState)) {
this._certificationGrade = "low";
this._result -= 5;
}
// lots more code like this
this._result -= Math.max(this._healthLevel - 5, 0);
return this._result;
}
scoreSmoking() {
if (this._medicalExam.isSmoker) {
this._healthLevel += 10;
this._highMedicalRiskFlag = true;
}
}
สิ่งนี้ทำให้ผมจัดการ command ได้คล้ายกับวิธีที่ผมจัดการกับ nested function อันที่จริง เมื่อทำ refactoring นี้ใน JavaScript การใช้ nested functions ก็เป็นทางเลือกที่สมเหตุสมผลแทนการใช้ command ผมยังคงใช้ command สำหรับกรณีนี้ ส่วนหนึ่งเพราะผมคุ้นเคยกับ command มากกว่า และส่วนหนึ่งเพราะด้วย command ผมสามารถเขียน test และการเรียกเพื่อ debugging กับ function ย่อย ๆ ได้