Extract Variable (การแยกตัวแปร)

เดิมเรียกว่า: Introduce Explaining Variable

เป็นการย้อนกลับของ: Inline Variable ( 123 )

A figure shows a representation and a code of an extract variable.

การแสดงภาพของ Extract Variable แสดงส่วนหนึ่งของ expression ที่ถูกนำไปใส่ใน temporary variable code จริงอ่านได้ว่า, return order.quantity * order.itemPrice – Math.max(01 order.quantity - 500) * order.itemPrice * 0.05 + Math.min(order.quantity * order.itemPrice * 0.1, 100); code ที่ refactor แล้ว อ่านได้ว่า, const basePrice = order.quantity * order.itemPrice; const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05; const shipping = Math.min(basePrice * 0.1, 100); return basePrice - quantityDiscount + shipping;

Motivation (แรงจูงใจ)

Expressions อาจซับซ้อนและอ่านยากมาก ในสถานการณ์เช่นนั้น local variables อาจช่วยแยก expression ออกเป็นสิ่งที่จัดการได้ง่ายขึ้น โดยเฉพาะอย่างยิ่ง มันให้ความสามารถในการตั้งชื่อให้กับส่วนหนึ่งของลอจิกที่ซับซ้อน ซึ่งช่วยให้ผมเข้าใจจุดประสงค์ของสิ่งที่เกิดขึ้นได้ดีขึ้น

variable แบบนี้ยังมีประโยชน์ต่อ debugging เพราะมันเป็นจุดเกาะที่ง่ายสำหรับ debugger หรือ print statement ในการดักจับค่า

ถ้าผมกำลังพิจารณาใช้ Extract Variable นั่นหมายความว่าผมต้องการเพิ่มชื่อให้กับ expression ในโค้ดของผม เมื่อตัดสินใจได้แล้ว ผมก็คิดถึงบริบทของชื่อนั้นด้วย ถ้าชื่อนั้นมีความหมายเฉพาะภายใน function ที่ผมกำลังทำงานอยู่ Extract Variable ก็เป็นตัวเลือกที่ดี—แต่ถ้ามันสมเหตุสมผลในบริบทที่กว้างกว่า ผมจะพิจารณาทำให้ชื่อนั้น พร้อมใช้ในบริบทที่กว้างกว่านั้น ซึ่งโดยปกติแล้วอยู่ในรูปของ function ถ้าชื่อพร้อมใช้ในวงกว้างกว่านั้น code อื่น ก็สามารถใช้ expression นั้นได้โดยไม่ต้องเขียน expression ซ้ำ นำไปสู่ duplication ที่น้อยลงและการสื่อเจตนาที่ดีขึ้นของผม

ข้อเสียของการเลื่อนชื่อขึ้นไปยังบริบทที่กว้างกว่าคืองานที่เพิ่มขึ้น ถ้ามันต้องใช้ความพยายามมากกว่านั้นอย่างมีนัยสำคัญ ผมมีแนวโน้มที่จะเก็บไว้ทำทีหลังเมื่อผมสามารถใช้ Replace Temp with Query ( 178 ) ได้ แต่ถ้ามันง่าย ผมชอบทำทันทีเพื่อให้ชื่อพร้อมใช้ใน code ทันที ตัวอย่างที่ดีของเรื่องนี้คือ ถ้าผมกำลังทำงานใน class การใช้ Extract Function ( 106 ) ก็ทำได้ง่ายมาก

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

  • ตรวจสอบว่า expression ที่อยากแยกไม่มี side effects

  • ประกาศ immutable variable และกำหนดค่าเป็นสำเนาของ expression ที่อยากตั้งชื่อ

  • แทนที่ expression เดิมด้วย variable ใหม่

  • test.

ถ้า expression ปรากฏมากกว่าหนึ่งครั้ง ให้แทนที่แต่ละจุดด้วย variable และ test หลังการแทนที่แต่ละครั้ง

Example (ตัวอย่าง)

ผมเริ่มจากการคำนวณง่าย ๆ

function price(order) {
  //price is base price - quantity discount + shipping
  return order.quantity * order.itemPrice -
    Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
    Math.min(order.quantity * order.itemPrice * 0.1, 100);
}

ต่อให้จะง่ายขนาดนี้ ผมก็ยังทำให้มันตามง่ายขึ้นได้อีก อย่างแรก ผมสังเกตว่า base price คือผลคูณของ quantity กับ item price

function price(order) {
  //price is base price - quantity discount + shipping
  return order.quantity * order.itemPrice -
    Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
    Math.min(order.quantity * order.itemPrice * 0.1, 100);
}

เมื่อความเข้าใจนั้นอยู่ในหัวของผมแล้ว ผมก็ใส่ลงไปใน code ด้วยการสร้างและตั้งชื่อ variable สำหรับมัน

function price(order) {
  //price is base price - quantity discount + shipping
  const basePrice = order.quantity * order.itemPrice;
  return order.quantity * order.itemPrice -
    Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
    Math.min(order.quantity * order.itemPrice * 0.1, 100);
}

แน่นอนว่าแค่การประกาศและ initialize variable ไม่ได้ทำอะไรให้สำเร็จ ผมต้องใช้มันด้วย ดังนั้นผมจึงแทนที่ expression ที่เป็นต้นทางของมัน

function price(order) {
  //price is base price - quantity discount + shipping
  const basePrice = order.quantity * order.itemPrice;
  return basePrice -
    Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
    Math.min(order.quantity * order.itemPrice * 0.1, 100);
}

expression เดียวกันนั้นถูกใช้ในภายหลังด้วย ดังนั้นผมจึงแทนที่มันด้วย variable ตรงนั้นได้เช่นกัน

function price(order) {
  //price is base price - quantity discount + shipping
  const basePrice = order.quantity * order.itemPrice;
  return basePrice -
    Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
    Math.min(basePrice * 0.1, 100);
}

บรรทัดถัดไปคือ quantity discount ดังนั้นผมจึงแยกมันออกมาได้เช่นกัน

function price(order) {
  //price is base price - quantity discount + shipping
  const basePrice = order.quantity * order.itemPrice;
  const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
  return basePrice -
    quantityDiscount +
    Math.min(basePrice * 0.1, 100);
}

สุดท้าย ผมปิดท้ายด้วย shipping และตอนที่ทำเช่นนั้น ผมก็ลบ comment ทิ้งได้ด้วย เพราะมันไม่ได้บอกอะไรที่ code ไม่ได้บอกอยู่แล้ว

function price(order) {
  const basePrice = order.quantity * order.itemPrice;
  const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
  const shipping = Math.min(basePrice * 0.1, 100);
  return basePrice - quantityDiscount + shipping;
}

Example: With a Class (ตัวอย่าง: การใช้กับคลาส)

นี่คือ code เดียวกัน แต่คราวนี้อยู่ในบริบทของ class:

class Order {
  constructor(aRecord) {
    this._data = aRecord;
  }
  get quantity()  {return this._data.quantity;}
  get itemPrice() {return this._data.itemPrice;}

  get price() {
    return this.quantity * this.itemPrice -
      Math.max(0, this.quantity - 500) * this.itemPrice * 0.05 +
      Math.min(this.quantity * this.itemPrice * 0.1, 100);
  }
}

ในกรณีนี้ ผมต้องการแยกชื่อเดียวกันออกมา แต่ผมตระหนักว่าชื่อเหล่านั้นใช้กับ Order ทั้งหมด ไม่ใช่แค่การคำนวณราคา เนื่องจากมันใช้กับออเดอร์ทั้งหมด ผมจึงโน้มเอียงที่จะแยกชื่อเหล่านั้น ออกมาเป็น methods มากกว่า variable

class Order {
  constructor(aRecord) {
    this._data = aRecord;
  }
  get quantity()  {return this._data.quantity;}
  get itemPrice() {return this._data.itemPrice;}

  get price() {
    return this.basePrice - this.quantityDiscount + this.shipping;
  }
  get basePrice()        {return this.quantity * this.itemPrice;}
  get quantityDiscount() {return Math.max(0, this.quantity - 500) * this.itemPrice * 0.05;}
  get shipping()         {return Math.min(this.basePrice * 0.1, 100);}
}

นี่คือหนึ่งในประโยชน์ที่ยิ่งใหญ่ของ object—มันให้บริบทที่พอเหมาะสำหรับลอจิกที่จะใช้แบ่งปันลอจิกและข้อมูลส่วนอื่น ๆ สำหรับอะไรที่ง่ายขนาดนี้ มันไม่สำคัญเท่าไรนัก แต่กับ class ที่ใหญ่ขึ้น การแยกชิ้นพฤติกรรมร่วมกันออกมาเป็น abstraction ของตัวเองพร้อมชื่อของมันเองเพื่อใช้อ้างอิงเมื่อใดก็ตามที่ผมทำงานกับ object นั้นมีประโยชน์มาก