Replace Parameter with Query (แทน Parameter ด้วย Query)
เดิมเรียกว่า: Replace Parameter with Method
เป็นการย้อนกลับของ: Replace Query with Parameter ( 327 )
การแสดงในรูปแบบแผนภาพแสดง function ที่มี parameter ถูกแทนที่ด้วยการ query ของ method อื่น code จริงอ่านว่า availableVacation(anEmployee, anEmployee.grade); function availableVacation(anEmployee, grade) { // calculate vacation... code หลังการ refactoring อ่านว่า availableVacation(anEmployee) function availableVacation(anEmployee) { const grade = anEmployee.grade; // calculate vacation...
Motivation (แรงจูงใจ)
parameter list ของ function ควรสรุปจุดที่ function นั้นแปรผันได้ โดยบ่งบอกวิธีหลัก ๆ ที่ function นั้นอาจมีพฤติกรรมต่างกัน เช่นเดียวกับคำสั่งใด ๆ ใน code การหลีกเลี่ยง duplication เป็นเรื่องดี และการมี parameter list สั้น ๆ ก็เข้าใจง่ายกว่า
ถ้าการเรียกส่งค่าที่ function สามารถหาได้เองโดยง่ายไม่แพ้กัน นั่นคือ duplication รูปแบบหนึ่ง—ซึ่งทำให้ caller ยุ่งยากโดยไม่จำเป็น เพราะมันต้องหาค่าของ parameter ทั้งที่ภาระนั้นจะถูกปลดออกไปได้
ขีดจำกัดของสิ่งนี้อยู่ในวลีที่ว่า "หาได้เองโดยง่ายไม่แพ้กัน" การลบ parameter ออก ผมกำลังย้ายความรับผิดชอบในการหาค่า parameter เมื่อมี parameter อยู่ การหาค่าของมันเป็นความรับผิดชอบของ caller แต่ถ้าไม่มี ความรับผิดชอบนั้นก็ย้ายไปอยู่ที่ body ของ function นิสัยปกติของผมคือการทำให้ชีวิตของ caller ง่ายขึ้น ซึ่งหมายถึงการย้ายความรับผิดชอบไปที่ body ของ function—แต่ก็ต่อเมื่อ ความรับผิดชอบนั้นเหมาะสมที่จะอยู่ตรงนั้น
เหตุผลทั่วไปที่สุดในการเลี่ยง Replace Parameter with Query คือการลบพารามิเตอร์จะเพิ่ม dependency ที่ไม่พึงประสงค์ ให้กับ body ของ function—บังคับให้มันเข้าถึงองค์ประกอบของโปรแกรมที่ผมอยากให้มันไม่ต้องรู้จัก นี่อาจเป็น dependency ใหม่ หรือ dependency เดิมที่ผมอยากลบทิ้ง ปกติเรื่องนี้เกิดขึ้นเมื่อผมต้องเพิ่มการเรียก function ที่เป็นปัญหาเข้าไปใน body หรือเข้าถึงบางอย่างภายใน receiver object ที่ผมอยากย้ายออกไปทีหลัง
กรณีที่ปลอดภัยที่สุดสำหรับ Replace Parameter with Query คือเมื่อค่าของพารามิเตอร์ที่อยากลบ หาได้เพียงแค่ query อีกพารามิเตอร์หนึ่งใน list เดียวกัน แทบไม่มีเหตุผลใดที่จะส่งพารามิเตอร์สองตัว ถ้าตัวหนึ่งสามารถหาได้จากอีกตัวหนึ่ง
สิ่งหนึ่งที่ต้องระวังคือถ้า function ที่ผมกำลังดูอยู่มี referential transparency—นั่นคือ ถ้าผมมั่นใจได้ว่ามันจะทำงานเหมือนเดิมทุกครั้งที่ถูกเรียกด้วยค่า parameter เดียวกัน function แบบนี้อ่านและ test ง่ายกว่ามาก และผมไม่อยากเปลี่ยนแปลงมันจนเสีย property นั้นไป ดังนั้นผมจะไม่แทนที่ parameter ด้วยการเข้าถึง global variable ที่ mutable
Mechanics (ขั้นตอนการทำ)
-
ถ้าจำเป็น ให้ใช้ Extract Function ( 106 ) กับการคำนวณค่า parameter
-
แทนที่การอ้างอิงถึง parameter ภายใน body ของ function ด้วยการอ้างอิงถึง expression ที่ให้ค่า parameter นั้น ทำ test หลังการเปลี่ยนแปลงแต่ละครั้ง
-
ใช้ Change Function Declaration ( 124 ) เพื่อลบ parameter
Example (ตัวอย่าง)
ผมใช้ Replace Parameter with Query บ่อยที่สุดเมื่อทำ refactoring อื่น ๆ จนทำให้พารามิเตอร์ตัวหนึ่งไม่จำเป็นอีกต่อไป ลองพิจารณา code นี้
class Order…
get finalPrice() {
const basePrice = this.quantity * this.itemPrice;
let discountLevel;
if (this.quantity > 100) discountLevel = 2;
else discountLevel = 1;
return this.discountedPrice(basePrice, discountLevel);
}
discountedPrice(basePrice, discountLevel) {
switch (discountLevel) {
case 1: return basePrice * 0.95;
case 2: return basePrice * 0.9;
}
}
เวลาผมทำให้ function ง่ายลง ผมมักจะใช้ Replace Temp with Query ( 178 ) ซึ่งจะนำผมไปสู่
class Order…
get finalPrice() {
const basePrice = this.quantity * this.itemPrice;
return this.discountedPrice(basePrice, this.discountLevel);
}
get discountLevel() {
return (this.quantity > 100) ? 2 : 1;
}
เมื่อทำแบบนี้แล้ว ก็ไม่จำเป็นต้องส่งผลลัพธ์ของ discountLevel ให้ discountedPrice อีกต่อไป—มันสามารถเรียกหาค่านั้นเองได้โดยง่ายไม่แพ้กัน
ผมแทนที่การอ้างอิงถึง parameter ทุกจุดด้วยการเรียก method แทน
class Order…
discountedPrice(basePrice, discountLevel) {
switch (this.discountLevel) {
case 1: return basePrice * 0.95;
case 2: return basePrice * 0.9;
}
}
จากนั้นผมสามารถใช้ Change Function Declaration ( 124 ) เพื่อลบ parameter
class Order…
get finalPrice() {
const basePrice = this.quantity * this.itemPrice;
return this.discountedPrice(basePrice, this.discountLevel);
}
discountedPrice(basePrice, discountLevel) {
switch (this.discountLevel) {
case 1: return basePrice * 0.95;
case 2: return basePrice * 0.9;
}
}