Split Phase (การแบ่งเฟส)

A figure shows a simple representation and a code for the split phase.

การแสดงภาพของการแบ่งเฟส (split phase) แสดง code สองสามบรรทัดของการทำงานสองแบบที่ถูกแบ่งออกเป็นสอง module แยกกัน code จริงอ่านได้ว่า, const orderData = orderString.split(/\s+/); const productPrice = priceList[orderData[0].split("-")[1]]; const orderPrice = parseInt(orderData[1]) * productPrice; code ที่ refactor แล้วอ่านได้ว่า, const orderRecord = parseOrder(order); const orderPrice = price(orderRecord, priceList); function parseOrder(aString) { const values = aString.split(/\s+/); return ({ productID: values[0].split("-")[1], quantity: parseInt(values[1]), }); } function price(order, priceList) { return order.quantity * priceList[order.productID]; }

Motivation (แรงจูงใจ)

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

หนึ่งในวิธีที่หรูหราที่สุดในการแบ่งแบบนี้คือการแยกพฤติกรรมออกเป็นสองเฟสที่ต่อเนื่องกัน ตัวอย่างที่ดีของเรื่องนี้คือตอนที่คุณมี การประมวลผลบางอย่างที่ input ไม่สะท้อนโมเดลที่คุณต้องใช้เพื่อดำเนินลอจิก ก่อนเริ่มต้น คุณสามารถจัดแต่ง input ให้อยู่ในรูปแบบที่ สะดวกสำหรับการประมวลผลหลักของคุณ หรือคุณอาจนำลอจิกที่ต้องทำมาแยกย่อยเป็นขั้นตอนที่ต่อเนื่องกัน โดยแต่ละขั้นตอนแตกต่างกันอย่างมีนัยสำคัญในสิ่งที่มันทำ

ตัวอย่างที่ชัดเจนที่สุดของเรื่องนี้คือ compiler งานพื้นฐานของมันคือการนำข้อความ (โค้ดในภาษาโปรแกรม) มาแปลงเป็นรูปแบบที่ รันได้ (เช่น object code สำหรับฮาร์ดแวร์เฉพาะ) เมื่อเวลาผ่านไป เราพบว่าสิ่งนี้สามารถแบ่งออกเป็นห่วงโซ่ของเฟสได้อย่างมีประโยชน์: การแยกข้อความเป็นโทเคน (tokenizing), การแยกวิเคราะห์โทเคนเป็น syntax tree, จากนั้นก็ขั้นตอนต่าง ๆ ในการแปลง syntax tree (เช่นเพื่อ optimization), และสุดท้ายคือการสร้าง object code แต่ละขั้นตอนมี scope จำกัด และผมสามารถคิดถึงขั้นตอนหนึ่งได้โดยไม่ต้องเข้าใจรายละเอียดของขั้นตอนอื่น

การแบ่งเฟสแบบนี้เป็นเรื่องปกติใน software ขนาดใหญ่ เฟสต่าง ๆ ใน compiler แต่ละเฟสอาจมี function และ class มากมาย แต่ผมก็สามารถทำ split-phase refactoring พื้นฐานกับ code ส่วนใดก็ได้—เมื่อใดก็ตามที่ผมเห็นโอกาสในการแยก code ออกเป็นเฟสต่าง ๆ อย่างเป็นประโยชน์ เบาะแสที่ดีที่สุดคือเมื่อขั้นตอนต่าง ๆ ของ code ส่วนนั้นใช้ชุดข้อมูลและ function ที่ต่างกัน การเปลี่ยนมันให้เป็น module แยกกันทำให้ผม สามารถทำให้ความแตกต่างนี้ชัดเจน เผยให้เห็นความแตกต่างใน code

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

  • แยก code ของเฟสที่สองออกเป็น function ของมันเอง

  • test

  • แนะนำ intermediate data structure เป็น argument เพิ่มเติมให้กับ function ที่ถูกแยกออกมา

  • test

  • ตรวจสอบ parameter แต่ละตัวของเฟสที่สองที่ถูกแยกออกมา ถ้ามันถูกใช้โดยเฟสแรก ให้ย้ายมันไปไว้ใน intermediate data structure test หลังจากการย้ายแต่ละครั้ง

    บางครั้ง parameter บางตัวไม่ควรถูกใช้โดยเฟสที่สอง ในกรณีนี้ ให้แยกผลลัพธ์ของการใช้งาน parameter แต่ละครั้ง ออกเป็น field ใน intermediate data structure แล้วใช้ Move Statements to Callers ( 217 ) กับบรรทัดที่ใส่ค่ามัน

  • ใช้ Extract Function ( 106 ) กับ code ของเฟสแรก โดยให้คืนค่า intermediate data structure

    การแยกเฟสแรกออกเป็น transformer object ก็เป็นเรื่องที่สมเหตุสมผลเช่นกัน

Example (ตัวอย่าง)

ผมจะเริ่มด้วย code สำหรับคิดราคาคำสั่งซื้อของสินค้าประเภทคลุมเครือและไม่สำคัญบางอย่าง:

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const shippingPerCase = (basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = quantity * shippingPerCase;
  const price =  basePrice - discount + shippingCost;
  return price;
}

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

ผมเริ่มต้นด้วยการใช้ Extract Function ( 106 ) กับการคำนวณค่าขนส่ง

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const price =  applyShipping(basePrice, shippingMethod, quantity, discount);
  return price;
}
function applyShipping(basePrice, shippingMethod, quantity, discount) {
  const shippingPerCase = (basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = quantity * shippingPerCase;
  const price =  basePrice - discount + shippingCost;
  return price;
}

ผมส่งข้อมูลทั้งหมดที่เฟสที่สองนี้ต้องการเข้าไปเป็น parameter รายบุคคล ในกรณีที่สมจริงกว่านี้ อาจมี parameter จำนวนมาก แต่ผมไม่กังวลกับมัน เพราะผมจะค่อย ๆ ลดจำนวนมันลงทีหลัง

ต่อไป ผมแนะนำ intermediate data structure ที่จะใช้สื่อสารระหว่างสองเฟส

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const priceData = {};
  const price =  applyShipping(priceData, basePrice, shippingMethod, quantity, discount);
  return price;
}
function applyShipping(priceData, basePrice, shippingMethod, quantity, discount) {
  const shippingPerCase = (basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = quantity * shippingPerCase;
  const price =  basePrice - discount + shippingCost;
  return price;
}

ตอนนี้ ผมดู parameter ต่าง ๆ ของ applyShipping ตัวแรกคือ basePrice ซึ่งถูกสร้างโดย code ของเฟสแรก ดังนั้นผมจึงย้ายมันเข้าไปใน intermediate data structure โดยเอามันออกจาก parameter list

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const priceData = {basePrice: basePrice};
  const price =  applyShipping(priceData, basePrice, shippingMethod, quantity, discount);
  return price;
}
function applyShipping(priceData, basePrice, shippingMethod, quantity, discount) {
  const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = quantity * shippingPerCase;
  const price =  priceData.basePrice - discount + shippingCost;
  return price;
}

parameter ตัวถัดไปใน list คือ shippingMethod ตัวนี้ผมปล่อยไว้ตามเดิม เพราะมันไม่ได้ถูกใช้โดย code ของเฟสแรก

หลังจากนี้ ผมมี quantity ตัวนี้ถูกใช้โดยเฟสแรกแต่ไม่ได้ถูกสร้างโดยมัน ดังนั้นจริง ๆ แล้วผมสามารถปล่อยมันไว้ใน parameter list ได้ แต่ความชอบตามปกติของผมคือการย้ายให้มากที่สุดเท่าที่จะทำได้ไปไว้ใน intermediate data structure

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const priceData = {basePrice: basePrice, quantity: quantity};
  const price =  applyShipping(priceData, shippingMethod, quantity, discount);
  return price;
}
function applyShipping(priceData, shippingMethod, quantity, discount) {
  const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = priceData.quantity * shippingPerCase;
  const price =  priceData.basePrice - discount + shippingCost;
  return price;
}

ผมทำแบบเดียวกันกับ discount

function priceOrder(product, quantity, shippingMethod) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  const priceData = {basePrice: basePrice, quantity: quantity, discount:discount};
  const price =  applyShipping(priceData, shippingMethod, discount);
  return price;
}
function applyShipping(priceData, shippingMethod, discount) {
  const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = priceData.quantity * shippingPerCase;
  const price =  priceData.basePrice - priceData.discount + shippingCost;
  return price;
}

เมื่อผมไล่ดู parameter ทั้งหมดของ function เสร็จแล้ว ผมก็มี intermediate data structure ที่สมบูรณ์ ดังนั้นผมจึงสามารถแยก code ของเฟสแรกออกเป็น function ของมันเอง โดยคืนค่าข้อมูลนี้

function priceOrder(product, quantity, shippingMethod) {
  const priceData = calculatePricingData(product, quantity);
  const price =  applyShipping(priceData, shippingMethod);
  return price;
}
function calculatePricingData(product, quantity) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  return {basePrice: basePrice, quantity: quantity, discount:discount};
}
function applyShipping(priceData, shippingMethod) {
  const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = priceData.quantity * shippingPerCase;
  const price =  priceData.basePrice - priceData.discount + shippingCost;
  return price;
}

และท้ายที่สุด ผมก็อดใจไม่ไหวที่จะเก็บกวาดเจ้า constant สุดท้ายพวกนั้นออกไป

function priceOrder(product, quantity, shippingMethod) {
  const priceData = calculatePricingData(product, quantity);
  return applyShipping(priceData, shippingMethod);
}
function calculatePricingData(product, quantity) {
  const basePrice = product.basePrice * quantity;
  const discount = Math.max(quantity - product.discountThreshold, 0)
          * product.basePrice * product.discountRate;
  return {basePrice: basePrice, quantity: quantity, discount:discount};
}
function applyShipping(priceData, shippingMethod) {
  const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold)
          ? shippingMethod.discountedFee : shippingMethod.feePerCase;
  const shippingCost = priceData.quantity * shippingPerCase;
  return priceData.basePrice - priceData.discount + shippingCost;
}