Combine Functions into Class (การรวมฟังก์ชันเข้าด้วยกันเป็นคลาส)

A figure shows a simple representation and a code for combining the functions into a class.

การแสดงภาพของการรวมฟังก์ชันเข้าเป็นคลาสแสดงข้อมูลและฟังก์ชันถูกผูกเข้าด้วยกัน ที่ด้านบน และ code จริงด้านล่างอ่านได้ว่า, function base(aReading) {...} function taxableCharge(aReading) {...} function calculateBaseCharge(aReading) {...} code ที่ refactor แล้วอ่านได้ว่า, class Reading { base() {...} taxableCharge() {...} calculateBaseCharge() {...} }

Motivation (แรงจูงใจ)

คลาสเป็นโครงสร้างพื้นฐานในภาษาโปรแกรมสมัยใหม่ส่วนใหญ่ มันผูกข้อมูลและ function เข้าด้วยกันในสภาพแวดล้อมร่วมกัน โดยเปิดเผยข้อมูลและ function บางส่วนให้องค์ประกอบอื่นของโปรแกรมใช้เพื่อการทำงานร่วมกัน มันเป็นโครงสร้างหลักในภาษา object-oriented แต่ก็มีประโยชน์กับแนวทางอื่นด้วยเช่นกัน

เมื่อผมเห็นกลุ่มของ function ที่ทำงานใกล้ชิดกันบนข้อมูลชุดเดียวกัน (โดยปกติถูกส่งเข้ามาเป็น arguments ในการเรียก function) ผมเห็นโอกาสในการสร้าง class การใช้ class ทำให้สภาพแวดล้อมร่วมที่ function เหล่านี้แบ่งปันชัดเจนขึ้น ช่วยให้ผมทำให้การเรียก function ภายใน object ง่ายขึ้นโดยการลบ arguments จำนวนมากออก และเป็นตัวอ้างอิงสำหรับส่ง object แบบนั้นไปยังส่วนอื่นของระบบ

นอกจากการจัดระเบียบ function ที่ก่อตัวอยู่แล้ว refactoring นี้ยังให้โอกาสที่ดีในการระบุชิ้นส่วนการคำนวณอื่น ๆ และ refactor พวกมันเป็น methods บน class ใหม่

อีกวิธีหนึ่งในการจัดกลุ่มฟังก์ชันเข้าด้วยกันคือ Combine Functions into Transform ( 149 ) จะใช้วิธีไหนขึ้นอยู่กับบริบทที่กว้างขึ้นของโปรแกรม ข้อได้เปรียบที่สำคัญอย่างหนึ่งของการใช้ class คือ มันทำให้ clients แก้ไขข้อมูลหลักของ object ได้ และค่าที่ได้มายังคงสอดคล้องกัน

นอกจากการรวมเป็น class แล้ว ฟังก์ชันแบบนี้ยังรวมเป็น nested function ได้ด้วย โดยปกติผมชอบ class มากกว่า nested function เพราะการ test ฟังก์ชันที่ซ้อนอยู่ในอีกฟังก์ชันหนึ่งทำได้ยาก class ยังจำเป็นเมื่อมีฟังก์ชัน มากกว่าหนึ่งตัวในกลุ่มที่ผมต้องการเปิดเผยให้ผู้ร่วมงานใช้

ภาษาที่ไม่มี class เป็นองค์ประกอบระดับหนึ่ง แต่มี first-class functions มักใช้ Function As Object [ mf-fao ] เพื่อให้ความสามารถนี้

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

  • ใช้ Encapsulate Record ( 162 ) กับเรคคอร์ดข้อมูลร่วมที่ function ใช้ร่วมกัน

    ถ้าข้อมูลที่ร่วมกันระหว่าง function ยังไม่ได้ถูกจัดกลุ่มเป็นโครงสร้าง record ให้ใช้ Introduce Parameter Object ( 140 ) เพื่อสร้าง record จัดกลุ่มมันเข้าด้วยกัน

  • นำแต่ละฟังก์ชันที่ใช้ record ร่วมแล้วใช้ Move Function ( 198 ) เพื่อย้ายมันเข้าไปใน class ใหม่

    arguments ใด ๆ ของการเรียก function ที่เป็นสมาชิกสามารถลบออกจาก argument list ได้

  • แต่ละชิ้นของลอจิกที่จัดการข้อมูลสามารถแยกออกมาได้ด้วย Extract Function ( 106 ) จากนั้นย้ายเข้าไปใน class ใหม่

Example (ตัวอย่าง)

ผมเติบโตในอังกฤษ ประเทศที่ขึ้นชื่อเรื่องความรักในชา (โดยส่วนตัวแล้ว ผมไม่ชอบชาส่วนใหญ่ที่พวกเขาเสิร์ฟในอังกฤษ แต่ต่อมาผมก็ติดใจชาจีนและญี่ปุ่น) ดังนั้นจินตนาการนักเขียนของผมจึงวาดภาพหน่วยงานรัฐ ที่ทำหน้าที่ส่งชาให้ประชาชน ทุกเดือนพวกเขาอ่านมาตรวัดชา เพื่อให้ได้ record แบบนี้:

reading = {customer: "ivan", quantity: 10, month: 5, year: 2017};

ผมไล่ดู code ที่ประมวลผล record เหล่านี้ และเห็นหลายจุดที่มีการคำนวณคล้ายกันบนข้อมูล ดังนั้นผมจึงหาจุดที่คำนวณ base charge:

client 1…

const aReading = acquireReading();
const baseCharge = baseRate(aReading.month, aReading.year) * aReading.quantity;

การเป็นอังกฤษ ทุกสิ่งที่จำเป็นต้องถูกเก็บภาษี ชาก็เช่นกัน แต่กฎอนุญาตให้ชาในระดับจำเป็นพื้นฐานอย่างน้อย ปลอดภาษี

client 2…

const aReading = acquireReading();
const base = (baseRate(aReading.month, aReading.year) * aReading.quantity);
const taxableCharge = Math.max(0, base - taxThreshold(aReading.year));

ผมมั่นใจว่าคุณสังเกตเห็นเหมือนผมว่าสูตรของ base charge ซ้ำกันระหว่างโค้ดสองส่วนนี้ ถ้าคุณเป็นเหมือนผม คุณก็เอื้อมไปหยิบ Extract Function ( 106 ) แล้ว น่าสนใจที่ดูเหมือนงานของเราถูกทำให้เสร็จที่อื่นแล้ว

client 3…

const aReading = acquireReading();
const basicChargeAmount = calculateBaseCharge(aReading);

function calculateBaseCharge(aReading) {
  return baseRate(aReading.month, aReading.year) * aReading.quantity;
}

เมื่อเห็นเช่นนี้ ผมมีแรงกระตุ้นตามธรรมชาติที่จะเปลี่ยน code client สองส่วนก่อนหน้าให้ใช้ฟังก์ชันนี้ แต่ปัญหาของ top-level functions แบบนี้คือมันมักถูกมองข้ามง่าย ผมอยากเปลี่ยน code เพื่อให้ฟังก์ชัน มีความเชื่อมโยงใกล้ชิดกับข้อมูลที่มันประมวลผลมากขึ้น วิธีที่ดีในการทำคือการเปลี่ยนข้อมูลให้เป็น class

เพื่อเปลี่ยนเรคคอร์ดเป็น class ผมใช้ Encapsulate Record ( 162 ) .

class Reading {
  constructor(data) {
    this._customer = data.customer;
    this._quantity = data.quantity;
    this._month = data.month;
    this._year = data.year;
  }
  get customer() {return this._customer;}
  get quantity() {return this._quantity;}
  get month()    {return this._month;}
  get year()     {return this._year;}
}

เพื่อย้ายพฤติกรรม ผมจะเริ่มด้วย function ที่ผมมีอยู่แล้ว: calculateBaseCharge . เพื่อใช้ class ใหม่ ผมต้องใช้มันกับข้อมูลทันทีที่ได้ข้อมูลมา

client 3…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const basicChargeAmount = calculateBaseCharge(aReading);

จากนั้นผมใช้ Move Function ( 198 ) เพื่อย้าย calculateBaseCharge เข้าไปใน class ใหม่

class Reading…

get calculateBaseCharge() {
  return baseRate(this.month, this.year) * this.quantity;
}

client 3…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const basicChargeAmount = aReading.calculateBaseCharge;

ขณะที่ทำอยู่ ผมก็ใช้ Rename Function ( 124 ) เพื่อทำให้มันถูกใจผมมากขึ้น

get baseCharge() {
  return baseRate(this.month, this.year) * this.quantity;
}

client 3…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const basicChargeAmount = aReading.baseCharge;

ด้วยการตั้งชื่อนี้ client ของ reading class จะบอกไม่ได้ว่า base charge เป็น field หรือค่าที่ได้มา นี่คือสิ่งที่ดี—Uniform Access Principle [ mf-ua ] .

ตอนนี้ผมแก้ client ตัวแรกให้เรียก method แทนการคำนวณซ้ำ

client 1…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const baseCharge = aReading.baseCharge;

มีโอกาสสูงที่ผมจะใช้ Inline Variable ( 123 ) กับตัวแปร baseCharge ก่อนจบวัน แต่ที่เกี่ยวข้องกับ refactoring นี้มากกว่าคือ client ที่คำนวณจำนวนเงินที่ต้องเสียภาษี ขั้นแรกของผมตรงนี้คือการใช้ property base charge ใหม่

client 2…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = Math.max(0, aReading.baseCharge - taxThreshold(aReading.year));

ผมใช้ Extract Function ( 106 ) กับการคำนวณ taxable charge

function taxableChargeFn(aReading) {
  return Math.max(0, aReading.baseCharge - taxThreshold(aReading.year));
}

client 3…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = taxableChargeFn(aReading);

จากนั้นผมใช้ Move Function ( 198 ) .

class Reading…

get taxableCharge() {
  return  Math.max(0, this.baseCharge - taxThreshold(this.year));
}

client 3…

const rawReading = acquireReading();
const aReading = new Reading(rawReading);
const taxableCharge = aReading.taxableCharge;

เนื่องจาก derived data ทั้งหมดถูกคำนวณตามความต้องการ (on demand) ผมจึงไม่มีปัญหาหากต้องอัปเดตข้อมูลที่เก็บไว้ โดยทั่วไปผมชอบ immutable data แต่หลายสถานการณ์บังคับให้เราทำงานกับ mutable data (เช่น JavaScript ระบบนิเวศภาษาที่ไม่ได้ออกแบบมาโดยคำนึงถึง immutability) เมื่อมีความเป็นไปได้พอสมควรที่ข้อมูลจะถูกอัปเดต ที่ไหนสักแห่งในโปรแกรม class ก็มีประโยชน์มาก