Replace Temp with Query (แทนที่ตัวแปรชั่วคราวด้วย query)

A figure shows a simple representation and a code to replace a temporary variable with a query.

แผนภาพแสดงการแทนที่ temporary variable ด้วย query โดยมี temporary variable ที่ถูก เรียกใช้ทีหลังภายใน function อยู่ด้านบน และ code จริงคือ const basePrice = this._quantity * this._itemPrice; if (basePrice > 1000) return basePrice * 0.95; else return basePrice * 0.98; code หลัง refactoring คือ get basePrice() {this._quantity * this._itemPrice;} ... if (this.basePrice > 1000) return this.basePrice * 0.95; else return this.basePrice * 0.98;

Motivation (แรงจูงใจ)

ประโยชน์หนึ่งของ temporary variable คือการเก็บค่าจาก code บางส่วนไว้เพื่ออ้างถึงมันทีหลังภายใน function การใช้ temp ทำให้ผมอ้างถึงค่านั้น พร้อมกับอธิบายความหมายของมัน และไม่ต้องเขียน code ที่ คำนวณค่าซ้ำ แต่ถึงการใช้ variable จะสะดวก การก้าวไปอีกขั้นด้วยการใช้ function แทนก็มักจะคุ้มค่า

ถ้าผมกำลังแยก function ขนาดใหญ่ การเปลี่ยน variable ให้เป็น function ของมันเองจะทำให้ แยกส่วนต่าง ๆ ของ function ได้ง่ายขึ้น เพราะผมไม่ต้อง ส่ง variable เข้าไปใน function ที่แยกออกมา การใส่ logic ลงใน function มักสร้างขอบเขต ที่แข็งแรงขึ้นระหว่าง logic ที่แยกออกมากับ function เดิม ซึ่งช่วยให้ผมหาและหลีกเลี่ยง dependencies และ side effects ที่ไม่เป็นธรรมชาติ

การใช้ function แทน variable ยังช่วยให้ผมไม่ต้องก็อปปี้ logic การคำนวณซ้ำใน function อื่นที่คล้ายกัน เมื่อใดที่ผมเห็น variable ถูกคำนวณแบบเดียวกันในหลายที่ ผมจะพยายามเปลี่ยนมันให้เป็น function เดียว

refactoring นี้จะได้ผลดีที่สุดเมื่อผมอยู่ใน class เพราะ class เป็นบริบทร่วม (shared context) ให้กับ method ที่ผม กำลังแยกออกมา นอก class ผมมักจะลงเอยด้วย parameter มากเกินไปในฟังก์ชันระดับบนสุด ซึ่งกลบข้อดี ของการใช้ฟังก์ชันไปเกือบหมด ฟังก์ชันซ้อน (nested function) ช่วยเลี่ยงปัญหานี้ได้ แต่ก็จำกัดความสามารถในการแชร์ logic ระหว่างฟังก์ชันที่เกี่ยวข้องกัน

temporary variable บางตัวเท่านั้นที่เหมาะกับ Replace Temp with Query variable ต้องถูกคำนวณครั้งเดียว แล้วหลังจากนั้นมีแต่การอ่านเท่านั้น ในกรณีที่ง่ายที่สุด หมายถึงมีการ assignment ให้ variable เพียงครั้งเดียว แต่ก็เป็นไปได้ที่จะมี assignment หลายครั้งใน code ชุดที่ซับซ้อนกว่า—ซึ่งทั้งหมดต้องถูกแยกเข้าไปใน query ยิ่งไปกว่านั้น logic ที่ใช้คำนวณ variable ต้องให้ผลลัพธ์เหมือนเดิมทุกครั้งที่ variable ถูกใช้ ทีหลัง—ซึ่งตัด variable ที่ใช้เป็นภาพนิ่ง (snapshot) ที่มีชื่อแบบ oldAddress .

Mechanics (กลไก)

  • ตรวจสอบว่า variable ถูกกำหนดค่าจนครบก่อนที่จะถูกใช้ และ code ที่คำนวณมันไม่ได้ให้ ค่าที่แตกต่างกันในแต่ละครั้งที่ถูกใช้

  • ถ้า variable ไม่ใช่ read-only และสามารถทำให้เป็น read-only ได้ ก็ทำเลย

  • test.

  • แยก assignment ของ variable ออกเป็น function

    ถ้า variable กับ function ใช้ชื่อร่วมกันไม่ได้ ให้ใช้ชื่อชั่วคราวสำหรับ function

    ทำให้แน่ใจว่า function ที่แยกออกมาไม่มี side effects ถ้าไม่ได้ ให้ใช้ Separate Query from Modifier ( 306 ) .

  • test.

  • ใช้ Inline Variable ( 123 ) เพื่อลบ temp ทิ้ง

Example (ตัวอย่าง)

นี่คือ class ง่าย ๆ:

class Order…

คลิกที่นี่เพื่อดู code

  constructor(quantity, item) {
    this._quantity = quantity;
    this._item = item;
  }
  
  get price() {
    var basePrice = this._quantity * this._item.price;
    var discountFactor = 0.98;
    if (basePrice > 1000) discountFactor -= 0.03;
    return basePrice * discountFactor;
  }
}

ผมต้องการแทนที่ temp basePrice และ discountFactor ด้วย method

เริ่มจาก basePrice ผมทำให้มันเป็น const แล้วรัน test วิธีนี้ดีสำหรับตรวจสอบว่าผมไม่ได้พลาดการกำหนดค่าซ้ำ—ซึ่งไม่น่าเกิดใน function สั้นขนาดนี้ แต่มักจะเกิดเมื่อผมจัดการกับอะไรที่ใหญ่กว่านั้น

class Order…

คลิกที่นี่เพื่อดู code

  constructor(quantity, item) {
    this._quantity = quantity;
    this._item = item;
  }

  get price() {
    const basePrice = this._quantity * this._item.price;
    var discountFactor = 0.98;
    if (basePrice > 1000) discountFactor -= 0.03;
    return basePrice * discountFactor;
  }
}

จากนั้นผมก็แยกฝั่งขวาของ assignment ไปไว้ใน method getter

class Order…

คลิกที่นี่เพื่อดู code

get price() {
  const basePrice = this.basePrice;
  var discountFactor = 0.98;
  if (basePrice > 1000) discountFactor -= 0.03;
  return basePrice * discountFactor;
}

 get basePrice() {
   return this._quantity * this._item.price;
 }

ผม test แล้วใช้ Inline Variable ( 123 ) .

class Order…

คลิกที่นี่เพื่อดู code

get price() {
  const basePrice = this.basePrice;
  var discountFactor = 0.98;
  if (this.basePrice > 1000) discountFactor -= 0.03;
  return this.basePrice * discountFactor;
}

จากนั้นผมก็ทำขั้นตอนซ้ำกับ discountFactor โดยเริ่มจากใช้ Extract Function ( 106 ) .

class Order…

คลิกที่นี่เพื่อดู code

get price() {
  const discountFactor = this.discountFactor;
  return this.basePrice * discountFactor;
}

 get discountFactor() {
   var discountFactor = 0.98;
   if (this.basePrice > 1000) discountFactor -= 0.03;
   return discountFactor;
 }

ในกรณีนี้ function ที่แยกออกมาต้องครอบคลุม assignment ทั้งสองครั้งของ discountFactor ผมยังสามารถทำให้ variable เดิมเป็น const .

จากนั้นก็ inline:

คลิกที่นี่เพื่อดู code

get price() {
  return this.basePrice * this.discountFactor;
}