Replace Query with Parameter (แทน Query ด้วย Parameter)

เป็นการย้อนกลับของ: Replace Parameter with Query ( 324 )

A figure illustrates how the refactoring technique is used to replace a query with a parameter.

การแสดงในรูปแบบแผนภาพแสดงการแทนที่การอ้างอิงภายในของ method ด้วย parameter code จริงอ่านว่า targetTemperature(aPlan) function targetTemperature(aPlan) { currentTemperature = thermostat.currentTemperature; // rest of function... code หลังการ refactoring อ่านว่า targetTemperature(aPlan, thermostat.currentTemperature) function targetTemperature(aPlan, currentTemperature) { // rest of function...

Motivation (แรงจูงใจ)

เมื่อมองผ่าน body ของ function บางครั้งผมเห็นการอ้างอิงถึงบางอย่างใน scope ของ function ที่ผมไม่สบายใจด้วย นี่อาจเป็นการอ้างอิงถึง global variable หรือองค์ประกอบใน module เดียวกันที่ผมตั้งใจจะย้ายออกไป เพื่อแก้ปัญหานี้ ผมต้องแทนที่การอ้างอิงภายในด้วย parameter โดยย้ายความรับผิดชอบในการหาค่าการอ้างอิงนั้นไปที่ caller ของ function

กรณีเหล่านี้ส่วนใหญ่เกิดจากความต้องการของผมที่จะปรับเปลี่ยนความสัมพันธ์ของ dependency ใน code—เพื่อให้ function เป้าหมายไม่ต้องพึ่งพาองค์ประกอบที่ผมอยากทำให้เป็น parameter อีกต่อไป มีความตึงเครียดระหว่างการเปลี่ยนทุกอย่างให้เป็น parameter ซึ่งให้ผลเป็น parameter list ที่ยาวซ้ำซาก กับการแบ่งปัน scope จำนวนมากซึ่งนำไปสู่ coupling ระหว่าง function มากเกินไป เช่นเดียวกับการตัดสินใจที่ยุ่งยากหลายอย่าง มันไม่ใช่สิ่งที่ผมจะทำได้ถูกต้องเสมอไป ดังนั้นจึงสำคัญที่ผมจะต้องสามารถเปลี่ยนแปลงสิ่งต่าง ๆ ได้อย่างน่าเชื่อถือ เพื่อให้โปรแกรมได้รับประโยชน์จากความเข้าใจที่ดีขึ้นเรื่อย ๆ ของผม

การให้เหตุผลกับ function ที่จะให้ผลลัพธ์เดิมเสมอเมื่อถูกเรียกด้วยค่าพารามิเตอร์เดียวกันนั้นง่ายกว่า—สิ่งนี้เรียกว่า referential transparency ถ้า function เข้าถึงองค์ประกอบบางอย่างใน scope ของมันที่ไม่เป็น referentially transparent function ที่ครอบมันอยู่ก็ขาด referential transparency ไปด้วย ผมแก้ไขได้โดย ย้ายองค์ประกอบนั้นไปเป็นพารามิเตอร์ แม้ว่าการย้ายแบบนี้จะผลักความรับผิดชอบไปที่ caller แต่การสร้าง module ที่ชัดเจนพร้อม referential transparency มักให้ประโยชน์มหาศาล รูปแบบทั่วไปคือการมี module ที่ประกอบด้วย pure functions ซึ่งถูกห่อด้วยตรรกะที่จัดการ I/O และองค์ประกอบ variable อื่น ๆ ของโปรแกรม ผมสามารถใช้ Replace Query with Parameter เพื่อทำให้บางส่วนของโปรแกรมบริสุทธิ์ขึ้น ซึ่งทำให้ส่วนนั้น test และให้เหตุผลได้ง่ายขึ้น

แต่ Replace Query with Parameter ไม่ได้มีแต่ข้อดี การย้าย query ไปเป็นพารามิเตอร์ ผมบังคับให้ caller ของผมต้องหาวิธีส่งค่านี้ให้ได้ สิ่งนี้ทำให้ชีวิตของ caller ของ function ยุ่งยากขึ้น และความโน้มเอียงปกติของผมคือการออกแบบ interface ที่ทำให้ชีวิตของผู้ใช้มันง่ายขึ้น ท้ายที่สุด มันลงเอยที่การจัดสรรความรับผิดชอบรอบ ๆ โปรแกรม และนั่นคือการตัดสินใจที่ไม่ทั้งง่ายและไม่ immutable—ซึ่งเป็นเหตุผลว่าทำไมการ refactoring นี้ (และการ refactoring ย้อนกลับของมัน) จึงเป็นสิ่งที่ผมต้องคุ้นเคยเป็นอย่างดี

Mechanics (ขั้นตอนการทำ)

  • ใช้ Extract Variable ( 119 ) กับ code query เพื่อแยกมันออกจากส่วนที่เหลือของ body function

  • ใช้ Extract Function ( 106 ) กับ code body ที่ไม่ใช่การเรียก query

    ตั้งชื่อ function ใหม่ให้ค้นหาได้ง่าย เพื่อการเปลี่ยนชื่อในภายหลัง

  • ใช้ Inline Variable ( 123 ) เพื่อกำจัดตัวแปรที่เพิ่งสร้างขึ้น

  • ใช้ Inline Function ( 115 ) กับ function เดิม

  • เปลี่ยนชื่อ function ใหม่ให้เป็นชื่อเดียวกับ function เดิม

Example (ตัวอย่าง)

ลองพิจารณาระบบควบคุมอุณหภูมิที่เรียบง่ายแต่น่าหงุดหงิดระบบหนึ่ง มันให้ผู้ใช้เลือกอุณหภูมิบนเทอร์โมสตัท—แต่จะตั้ง target temperature ภายในช่วงที่กำหนดโดย heating plan เท่านั้น

class HeatingPlan…

get targetTemperature() {
  if      (thermostat.selectedTemperature >  this._max) return this._max;
  else if (thermostat.selectedTemperature <  this._min) return this._min;
  else return thermostat.selectedTemperature;
}

caller…

if      (thePlan.targetTemperature > thermostat.currentTemperature) setToHeat();
else if (thePlan.targetTemperature < thermostat.currentTemperature) setToCool();
else setOff();

ในฐานะผู้ใช้ระบบแบบนี้ ผมอาจหงุดหงิดที่ความต้องการของผมถูกขัดขวางโดยกฎของ heating plan แต่ในฐานะโปรแกรมเมอร์ ผมอาจกังวลมากกว่าเกี่ยวกับการที่ function targetTemperature มี dependency กับ global thermostat object ผมสามารถทำลาย dependency นี้ได้โดยย้ายมันไปเป็น parameter

ขั้นแรกของผมคือการใช้ Extract Variable ( 119 ) กับ parameter ที่ผมต้องการให้มีใน function

class HeatingPlan…

get targetTemperature() {
  const selectedTemperature = thermostat.selectedTemperature;
  if      (selectedTemperature >  this._max) return this._max;
  else if (selectedTemperature <  this._min) return this._min;
  else return selectedTemperature;
}

นั่นทำให้ง่ายที่จะใช้ Extract Function ( 106 ) กับทั้ง body ของ function ยกเว้นส่วนที่หาค่า parameter

class HeatingPlan…

get targetTemperature() {
  const selectedTemperature = thermostat.selectedTemperature;
  return this.xxNEWtargetTemperature(selectedTemperature);
}

xxNEWtargetTemperature(selectedTemperature) {
  if      (selectedTemperature >  this._max) return this._max;
  else if (selectedTemperature <  this._min) return this._min;
  else return selectedTemperature;
}

จากนั้นผมก็ inline variable ที่เพิ่งแยกออกมา ซึ่งจะเหลือ function เป็นเพียงการเรียกง่าย ๆ

class HeatingPlan…

get targetTemperature() {
  return this.xxNEWtargetTemperature(thermostat.selectedTemperature);
}

ตอนนี้ผมสามารถใช้ Inline Function ( 115 ) กับ method นี้ได้

caller…

if      (thePlan.xxNEWtargetTemperature(thermostat.selectedTemperature) >
         thermostat.currentTemperature)
  setToHeat();
else if (thePlan.xxNEWtargetTemperature(thermostat.selectedTemperature) <
         thermostat.currentTemperature)
  setToCool();
else
  setOff();

ผมใช้ประโยชน์จากชื่อที่ค้นหาได้ง่ายของ function ใหม่ในการเปลี่ยนชื่อโดยลบ prefix ออก

caller…

if      (thePlan.targetTemperature(thermostat.selectedTemperature) >
         thermostat.currentTemperature)
  setToHeat();
else if (thePlan.targetTemperature(thermostat.selectedTemperature) <
         thermostat.currentTemperature)
  setToCool();
else
  setOff();

class HeatingPlan…

targetTemperature(selectedTemperature) {
  if      (selectedTemperature >  this._max) return this._max;
  else if (selectedTemperature <  this._min) return this._min;
  else return selectedTemperature;
}

ดังที่มักเกิดขึ้นกับการ refactoring นี้ code ฝั่งผู้เรียกดูเทอะทะกว่าเดิม การย้าย dependency ออกจาก module ผลักความรับผิดชอบในการจัดการกับ dependency นั้นกลับไปที่ caller นั่นคือสิ่งที่เรายอมแลกเพื่อลด coupling

แต่การลบ coupling กับ thermostat object ไม่ใช่ประโยชน์เดียวที่ผมได้จากการ refactoring ครั้งนี้ class HeatingPlan เป็น immutable—field ของมันถูกตั้งใน constructor โดยไม่มี method ใดแก้มันได้ (ผมจะไม่ให้คุณเหนื่อยกับการดูทั้ง class; เชื่อผมเถอะ) ด้วย heating plan ที่เป็น immutable การย้ายการอ้างอิง thermostat ออกจาก body ของ function ยังทำให้ targetTemperature เป็น referentially transparent ทุกครั้งที่ผมเรียก targetTemperature บน object เดียวกัน ด้วย argument เดียวกัน ผมจะได้ผลลัพธ์เดียวกัน ถ้า method ทั้งหมดของ heating plan มี referential transparency คลาสนี้จะ test และให้เหตุผลได้ง่ายขึ้นมาก

ปัญหาหนึ่งของโมเดล class ใน JavaScript คือมันเป็นไปไม่ได้ที่จะบังคับให้ class เป็น immutable—มีทางเข้าถึงข้อมูลของ object ได้เสมอ แต่การเขียน class ที่ส่งสัญญาณและส่งเสริมความไม่เปลี่ยนแปลก (immutability) มักจะเพียงพอ การสร้าง class ที่มีคุณลักษณะนี้มักเป็นกลยุทธ์ที่สมเหตุสมผล และ Replace Query with Parameter ก็เป็นเครื่องมือที่มีประโยชน์สำหรับการทำเช่นนี้