Replace Command with Function (แทน Command ด้วย Function)

เป็นการย้อนกลับของ: Replace Function with Command ( 337 )

A figure illustrates how the refactoring technique is used to replace a command with a function.

การแสดงในรูปแบบแผนภาพแสดง command ที่ถูกแทนที่ด้วย method พร้อม object ของมัน code จริงอ่านว่า class ChargeCalculator { constructor (customer, usage){ this._customer = customer; this._usage = usage; } execute() { return this._customer.rate * this._usage; } } code หลังการ refactoring อ่านว่า function charge(customer, usage) { return customer.rate * usage; }

Motivation (แรงจูงใจ)

Command object มอบกลไกอันทรงพลังสำหรับจัดการการคำนวณที่ซับซ้อน มันสามารถถูกย่อยสลายเป็น method ย่อย ๆ ที่แชร์ state ร่วมกันผ่าน field ได้ง่าย มันสามารถถูกเรียกผ่าน method ที่แตกต่างกันเพื่อให้ได้ผลลัพธ์ที่แตกต่างกัน และข้อมูลของมันสามารถถูกประกอบขึ้นเป็นขั้นตอนได้ แต่พลังนั้นมาพร้อมกับราคาที่ต้องจ่าย บ่อยครั้งที่ผมแค่อยากเรียก function แล้วให้มันทำงานของมัน ถ้าเป็นกรณีนั้น และ function ไม่ได้ซับซ้อนเกินไป command object ก็สร้างปัญหา มากกว่าคุณค่าที่ได้รับ และควรถูกเปลี่ยนเป็น function ทั่วไป

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

  • ใช้ Extract Function ( 106 ) กับส่วนที่สร้าง command และส่วนที่เรียก execution method ของ command

    การทำแบบนี้จะสร้าง function ใหม่ที่จะมาแทนที่ command ในภายหลัง

  • สำหรับแต่ละ method ที่ถูกเรียกโดย execution method ของ command ให้ใช้ Inline Function ( 115 )

    ถ้า function ที่ช่วยทำงานส่งค่ากลับ ให้ใช้ Extract Variable ( 119 ) กับการเรียกมันก่อน แล้วจึงใช้ Inline Function ( 115 )

  • ใช้ Change Function Declaration ( 124 ) เพื่อย้าย parameter ทั้งหมดของ constructor ไปไว้ที่ execution method ของ command แทน

  • สำหรับแต่ละ field ให้แก้การอ้างอิงใน execution method ของ command ให้ใช้ parameter แทน test หลังการเปลี่ยนแปลงแต่ละครั้ง

  • ทำการ inline การเรียก constructor และการเรียก execution method ของ command เข้าไปใน caller (ซึ่งก็คือ function ที่สร้างขึ้นมาแทนที่)

  • test

  • ใช้ Remove Dead Code ( 237 ) กับ class command

Example (ตัวอย่าง)

ผมจะเริ่มด้วย command object เล็ก ๆ นี้:

class ChargeCalculator {
  constructor (customer, usage, provider){
    this._customer = customer;
    this._usage = usage;
    this._provider = provider;
  }
  get baseCharge() {
    return this._customer.baseRate * this._usage;
  }
  get charge() {
    return this.baseCharge + this._provider.connectionCharge;
  }
}

มันถูกใช้โดย code แบบนี้:

caller…

monthCharge = new ChargeCalculator(customer, usage, provider).charge;

class command นั้นเล็กและเรียบง่ายพอที่จะเปลี่ยนเป็น function ได้ดีกว่า

ผมเริ่มต้นด้วยการใช้ Extract Function ( 106 ) เพื่อห่อหุ้มการสร้าง class และการเรียกใช้งาน

caller…

monthCharge = charge(customer, usage, provider);

top level…

function charge(customer, usage, provider) {
  return new ChargeCalculator(customer, usage, provider).charge;
}

ผมต้องตัดสินใจว่าจะจัดการกับ function ที่ช่วยทำงานอย่างไร ในกรณีนี้คือ baseCharge วิธีปกติของผมสำหรับ function ที่ส่งค่ากลับคือ การใช้ Extract Variable ( 119 ) กับค่านั้นก่อน

class ChargeCalculator…

get baseCharge() {
  return this._customer.baseRate * this._usage;
}
get charge() {
  const baseCharge = this.baseCharge;
  return baseCharge + this._provider.connectionCharge;
}

จากนั้น ผมใช้ Inline Function ( 115 ) กับ function ที่ช่วยทำงานนั้น

class ChargeCalculator…

get charge() {
  const baseCharge = this._customer.baseRate * this._usage;
  return baseCharge + this._provider.connectionCharge;
}

ตอนนี้ผมมีการประมวลผลทั้งหมดอยู่ใน function เดียวแล้ว ขั้นต่อไปของผมคือการย้ายข้อมูลที่ส่งให้ constructor ไปที่ method หลัก ผมเริ่มต้นด้วยการใช้ Change Function Declaration ( 124 ) เพื่อเพิ่ม parameter ทั้งหมดของ constructor ให้กับ method charge

class ChargeCalculator…

constructor (customer, usage, provider){
  this._customer = customer;
  this._usage = usage;
  this._provider = provider;
}

charge(customer, usage, provider) {
  const baseCharge = this._customer.baseRate * this._usage;
  return baseCharge + this._provider.connectionCharge;
}

top level…

function charge(customer, usage, provider) {
  return new ChargeCalculator(customer, usage, provider)
                      .charge(customer, usage, provider);
}

ตอนนี้ผมสามารถแก้ body ของ charge เพื่อใช้ parameter ที่ส่งเข้ามาแทนได้ โดยทำทีละตัว

class ChargeCalculator…

constructor (customer, usage, provider){
  this._customer = customer;
  this._usage = usage;
  this._provider = provider;
}

charge(customer, usage, provider) {
  const baseCharge = customer.baseRate * this._usage;
  return baseCharge + this._provider.connectionCharge;
}

ผมไม่จำเป็นต้องลบ assignment ให้ this._customer ใน constructor เพราะมันจะถูกเพิกเฉยไปเอง แต่ผมชอบทำมัน เพราะถ้าผมพลาดการแก้จุดที่ใช้ field ให้เป็น parameter การลบนี้จะทำให้ test ล้มเหลว (และถ้า test ไม่ล้มเหลว ผมควรพิจารณาเพิ่ม test ใหม่)

ผมทำซ้ำกับ parameter อื่น ๆ จนได้ผลลัพธ์เป็น

class ChargeCalculator…

charge(customer, usage, provider) {
  const baseCharge = customer.baseRate * usage;
  return baseCharge + provider.connectionCharge;
}

เมื่อทำทั้งหมดนี้เสร็จ ผมก็สามารถ inline เข้าไปใน function charge ระดับบนสุด นี่คือ Inline Function ( 115 ) แบบพิเศษ เพราะมันเป็นการ inline ทั้งการเรียก constructor และการเรียก method เข้าด้วยกัน

top level…

function charge(customer, usage, provider) {
  const baseCharge = customer.baseRate * usage;
  return baseCharge + provider.connectionCharge;
}

class command กลายเป็น dead code แล้ว ดังนั้นผมจะใช้ Remove Dead Code ( 237 ) เพื่อทำการฝังศพอย่างมีเกียรติให้มัน