Parameterize Function (ทำให้ Function รับ Parameter)
เดิมเรียกว่า: Parameterize Method
การแสดงในรูปแบบแผนภาพแสดง function พร้อมกับ parameter ของมัน code จริงอ่านว่า function tenPercentRaise(aPerson) { aPerson.salary = aPerson.salary.multiply(1.1); } function fivePercentRaise(aPerson) { aPerson.salary = aPerson.salary.multiply(1.05); } code หลังการ refactoring อ่านว่า function raise(aPerson, factor) { aPerson.salary = aPerson.salary.multiply(1 + factor); }
Motivation (แรงจูงใจ)
ถ้าผมเห็น function สองตัวที่ทำงานด้วยตรรกะคล้ายกันมากแต่ใช้ literal value ต่างกัน ผมสามารถลบ duplication นั้นได้โดยใช้ function เดียวที่มี parameter สำหรับค่าที่ต่างกัน สิ่งนี้เพิ่มประโยชน์ใช้สอยของ function เพราะผมสามารถนำไปใช้ที่อื่น ด้วยค่าที่ต่างกันได้
Mechanics (ขั้นตอนการทำ)
-
เลือก method ที่คล้ายกันมาหนึ่งตัว
-
ใช้ Change Function Declaration ( 124 ) เพื่อเพิ่ม literal ใด ๆ ที่ต้องเปลี่ยนเป็น parameter
-
สำหรับ caller แต่ละจุดของ function ให้เพิ่มค่า literal นั้น
-
ทำ test
-
เปลี่ยน body ของ function ให้ใช้ parameter ใหม่ ทำ test หลังการเปลี่ยนแปลงแต่ละครั้ง
-
สำหรับ function ที่คล้ายกันแต่ละตัว ให้แทนที่การเรียกด้วยการเรียก function ที่รับพารามิเตอร์แล้ว ทำ test หลังแต่ละอัน
ถ้า function ที่รับพารามิเตอร์แล้วใช้กับ function ที่คล้ายกันตัวอื่นไม่ได้ ให้ปรับมันสำหรับ function ใหม่ก่อน แล้วค่อยไปตัวถัดไป
Example (ตัวอย่าง)
ตัวอย่างที่เห็นได้ชัดก็ประมาณนี้:
function tenPercentRaise(aPerson) {
aPerson.salary = aPerson.salary.multiply(1.1);
}
function fivePercentRaise(aPerson) {
aPerson.salary = aPerson.salary.multiply(1.05);
}
หวังว่ามันคงชัดเจนว่าผมสามารถแทนที่สองตัวนี้ด้วย
function raise(aPerson, factor) {
aPerson.salary = aPerson.salary.multiply(1 + factor);
}
แต่มันอาจซับซ้อนกว่านั้นได้เล็กน้อย ลองพิจารณา code นี้:
function baseCharge(usage) {
if (usage < 0) return usd(0);
const amount =
bottomBand(usage) * 0.03
+ middleBand(usage) * 0.05
+ topBand(usage) * 0.07;
return usd(amount);
}
function bottomBand(usage) {
return Math.min(usage, 100);
}
function middleBand(usage) {
return usage > 100 ? Math.min(usage, 200) - 100 : 0;
}
function topBand(usage) {
return usage > 200 ? usage - 200 : 0;
}
ตรงนี้ตรรกะคล้ายกันอย่างเห็นได้ชัด—แต่มันคล้ายกันพอที่จะรองรับการสร้าง method ที่รับพารามิเตอร์สำหรับแต่ละ band หรือไม่? มันคล้ายกันพอครับ แต่อาจจะมองไม่ออกง่ายเท่ากรณีง่าย ๆ ข้างต้นสักเล็กน้อย
เมื่อมองหาการทำให้ function ที่เกี่ยวข้องกันหลายตัวรับ parameter วิธีของผมคือเลือก function มาหนึ่งตัวแล้วใช้ Add Parameter โดยมองเผื่อกรณีอื่น ๆ ไปด้วย สำหรับสิ่งที่มีลักษณะเป็นช่วงแบบนี้ ปกติจุดเริ่มต้นที่ดีคือช่วงกลาง ดังนั้นผมจะจัดการกับ middleBand เพื่อเปลี่ยนให้มันใช้ parameter แล้วค่อยปรับ caller ตัวอื่นให้เข้ากัน
middleBand ใช้ literal value สองค่า: 100 และ 200 ซึ่งแทนขอบล่างและขอบบนของ band กลางนี้ ผมเริ่มต้นด้วยการใช้ Change Function Declaration ( 124 ) เพื่อเพิ่มมันเข้าไปในการเรียก ขณะเดียวกัน ผมจะเปลี่ยนชื่อ function ให้เป็นชื่อที่สมเหตุสมผลกับการรับพารามิเตอร์ด้วย
function withinBand(usage, bottom, top) {
return usage > 100 ? Math.min(usage, 200) - 100 : 0;
}
function baseCharge(usage) {
if (usage < 0) return usd(0);
const amount =
bottomBand(usage) * 0.03
+ withinBand(usage, 100, 200) * 0.05
+ topBand(usage) * 0.07;
return usd(amount);
}
ผมแทนที่ literal แต่ละตัวด้วยการอ้างอิงถึง parameter:
function withinBand(usage, bottom, top) {
return usage > bottom ? Math.min(usage, 200) - bottom : 0;
}
จากนั้น:
function withinBand(usage, bottom, top) {
return usage > bottom ? Math.min(usage, top) - bottom : 0;
}
ผมแทนที่การเรียก band ล่างด้วยการเรียก function ที่รับพารามิเตอร์ใหม่นี้
function baseCharge(usage) {
if (usage < 0) return usd(0);
const amount =
withinBand(usage, 0, 100) * 0.03
+ withinBand(usage, 100, 200) * 0.05
+ topBand(usage) * 0.07;
return usd(amount);
}
function bottomBand(usage) {
return Math.min(usage, 100);
}
เพื่อแทนที่การเรียก band บน ผมจำเป็นต้องใช้ค่า infinity
function baseCharge(usage) {
if (usage < 0) return usd(0);
const amount =
withinBand(usage, 0, 100) * 0.03
+ withinBand(usage, 100, 200) * 0.05
+ withinBand(usage, 200, Infinity) * 0.07;
return usd(amount);
}
function topBand(usage) {
return usage > 200 ? usage - 200 : 0;
}
ด้วยตรรกะที่ทำงานแบบที่เป็นอยู่ในตอนนี้ ผมสามารถลบ guard clause ตัวแรกทิ้งได้ แต่ถึงแม้มันจะไม่จำเป็นในเชิงตรรกะแล้ว ผมก็ยังชอบเก็บมันไว้เพราะมันช่วยบอกวิธีการจัดการกับกรณีนั้น