Rename Field (เปลี่ยนชื่อ Field)

A figure illustrates how the refactoring technique is used to rename a field.

การแสดงในรูปแบบแผนภาพแสดง field ชื่อ ‘nm’ ที่ถูกเปลี่ยนชื่อเป็น ‘name’ code จริงอ่านว่า class organization { get name() {...} } code หลังการ refactoring อ่านว่า class organization { get title() {...} }

Motivation (แรงจูงใจ)

ชื่อเป็นสิ่งสำคัญ และชื่อ field ใน record structures ยิ่งสำคัญเป็นพิเศษเมื่อโครงสร้างเหล่านั้นถูกใช้อย่างแพร่หลายทั่วทั้งโปรแกรม data structure มีบทบาทสำคัญอย่างยิ่งต่อการทำความเข้าใจโค้ด หลายปีก่อน Fred Brooks เคยกล่าวไว้ว่า “ถ้าคุณแสดง flowchart ให้ผมดูแต่ซ่อนตารางของคุณไว้ ผมก็จะยังคงงุนงงต่อไป แต่ถ้าคุณแสดงตารางของคุณให้ผมดู ผมก็มักจะไม่ต้องใช้ flowchart ของคุณอีก เพราะมันจะชัดเจนในตัวเอง” แม้ทุกวันนี้ผมจะไม่ค่อยเห็นผู้คนวาด flowchart แล้ว แต่คำกล่าวนี้ยังคงเป็นจริง data structure คือกุญแจสำคัญในการทำความเข้าใจสิ่งที่เกิดขึ้น

เนื่องจาก data structure เหล่านี้สำคัญมาก การรักษาให้มันชัดเจนจึงจำเป็นยิ่งนัก เช่นเดียวกับสิ่งอื่น ๆ ความเข้าใจข้อมูลของผมจะดีขึ้นเรื่อย ๆ ยิ่งผมทำงานกับ software มากขึ้น ดังนั้นจึงจำเป็นอย่างยิ่งที่ความเข้าใจที่ดีขึ้นนี้จะถูกฝังลงไปในโปรแกรม

คุณอาจต้องการเปลี่ยนชื่อ field ใน record structure แต่แนวคิดนี้ใช้ได้กับ class ด้วยเช่นกัน method getter และ setter ถือเป็น field ที่มีผลต่อผู้ใช้ class การเปลี่ยนชื่อ method เหล่านี้จึงสำคัญพอ ๆ กับการเปลี่ยนชื่อ field ใน record structures ทั่วไป

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

  • ถ้า record มี scope จำกัด ให้เปลี่ยนชื่อการเข้าถึง field ทั้งหมดแล้ว test ไม่จำเป็นต้องทำขั้นตอนที่เหลือ

  • ถ้า record ยังไม่ได้ถูก encapsulate ให้ใช้ Encapsulate Record ( 162 ) .

  • เปลี่ยนชื่อ private field ภายใน object แล้วปรับ method ภายในให้สอดคล้อง

  • test

  • ถ้า constructor ใช้ชื่อนั้น ให้ใช้ Change Function Declaration ( 124 ) เพื่อเปลี่ยนชื่อ

  • ใช้ Rename Function ( 124 ) กับ accessors

Example: Renaming a Field (ตัวอย่าง: การเปลี่ยนชื่อ Field)

ผมจะเริ่มจาก constant ตัวหนึ่ง

const organization = {name: "Acme Gooseberries", country: "GB"};

ผมต้องการเปลี่ยน “name” เป็น “title” object นี้ถูกใช้อย่างแพร่หลายใน code base และในโค้ดก็มีการอัปเดตค่า title อยู่หลายจุด ดังนั้นขั้นแรกของผมคือใช้ Encapsulate Record ( 162 ) .

class Organization {
  constructor(data) {
    this._name = data.name;
    this._country = data.country;
  }
  get name()    {return this._name;}
  set name(aString) {this._name = aString;}
  get country()    {return this._country;}
  set country(aCountryCode) {this._country = aCountryCode;}
}

const organization = new Organization({name: "Acme Gooseberries", country: "GB"});

เมื่อผม encapsulate โครงสร้าง record เข้าไปใน class แล้ว ตอนนี้มีสี่จุดที่ต้องจัดการในการเปลี่ยนชื่อ: function getter function setter constructor และ data structure ภายใน แม้ฟังดูเหมือนผมจะทำงานมากขึ้น แต่จริง ๆ แล้วมันทำให้งานของผมง่ายขึ้น เพราะผมสามารถเปลี่ยนทีละจุดอย่างอิสระแทนที่จะเปลี่ยนทั้งหมดพร้อมกัน โดยก้าวทีละเล็กทีละน้อย ก้าวที่เล็กลงหมายถึงสิ่งที่จะผิดพลาดในแต่ละก้าวน้อยลง—ดังนั้นจึงทำงานน้อยลง มันคงไม่ทำงานน้อยลงแน่ ๆ ถ้าผมไม่เคยทำพลาด—แต่การไม่ทำพลาดเลยนั้นเป็นความฝันที่ผมเลิกคิดถึงไปนานแล้ว

เนื่องจากผมได้ก๊อปปี้ data structure ที่รับเข้ามาไว้ใน data structure ภายใน ผมจึงต้องแยกทั้งสองออกจากกัน เพื่อให้ทำงานกับมันได้อย่างอิสระ ผมทำได้โดยการประกาศ field แยกต่างหาก แล้วปรับ constructor และ accessors ให้ใช้ field ใหม่นี้

class Organization…

class Organization {
  constructor(data) {
    this._title = data.name;
    this._country = data.country;
  }
  get name()    {return this._title;}
  set name(aString) {this._title = aString;}
  get country()    {return this._country;}
  set country(aCountryCode) {this._country = aCountryCode;}
}

ต่อไป ผมเพิ่มการรองรับการใช้ “title” ใน constructor

class Organization…

class Organization {
  constructor(data) {
    this._title = (data.title !== undefined) ? data.title : data.name;
    this._country = data.country;
  }
  get name()    {return this._title;}
  set name(aString) {this._title = aString;}
  get country()    {return this._country;}
  set country(aCountryCode) {this._country = aCountryCode;}
}

ตอนนี้ caller ของ constructor ของผมสามารถใช้ name หรือ title ก็ได้ (โดย title จะมีสิทธิ์เหนือกว่า) จากนั้นผมก็ไล่เปลี่ยน caller ของ constructor ทั้งหมดทีละจุดให้ใช้ชื่อใหม่

const organization = new Organization({title: "Acme Gooseberries", country: "GB"});

เมื่อเปลี่ยนครบทุกจุดแล้ว ผมก็สามารถลบการรองรับชื่อ name ออกได้

class Organization…

class Organization {
  constructor(data) {
    this._title = data.title;
    this._country = data.country;
  }
  get name()    {return this._title;}
  set name(aString) {this._title = aString;}
  get country()    {return this._country;}
  set country(aCountryCode) {this._country = aCountryCode;}
}

เมื่อ constructor และข้อมูลใช้ชื่อใหม่แล้ว ผมก็สามารถเปลี่ยน accessors ได้ ซึ่งง่ายเพียงแค่ใช้ Rename Function ( 124 ) กับแต่ละตัว

class Organization…

class Organization {
  constructor(data) {
    this._title = data.title;
    this._country = data.country;
  }
  get title()    {return this._title;}
  set title(aString) {this._title = aString;}
  get country()    {return this._country;}
  set country(aCountryCode) {this._country = aCountryCode;}
}

ผมสาธิตกระบวนการนี้ในรูปแบบที่หนักหน่วงที่สุดเท่าที่จำเป็นสำหรับ data structure ที่ถูกใช้อย่างแพร่หลาย ถ้ามันถูกใช้เฉพาะในวงจำกัด เช่น ใน function เดียว ผมก็อาจแค่เปลี่ยนชื่อ property ต่าง ๆ ทีเดียวโดยไม่ต้องทำ encapsulation การจะใช้ขั้นตอนเต็มรูปแบบเมื่อไหร่เป็นเรื่องของดุลยพินิจ—แต่เช่นเดียวกับการ refactoring ทั่วไป ถ้า test ของผมพัง นั่นคือสัญญาณว่าผมต้องใช้กระบวนการที่ค่อยเป็นค่อยไปมากกว่า

บางภาษาเปิดให้ผมทำให้ data structure เป็น immutable ได้ ในกรณีนี้ แทนที่จะ encapsulate ผมสามารถคัดลอกค่าไปยังชื่อใหม่ ค่อย ๆ เปลี่ยนผู้ใช้งาน แล้วจึงลบชื่อเก่าทิ้ง การทำข้อมูลซ้ำกันเป็นสูตรสำเร็จของหายนะเมื่อใช้กับ mutable data structures และการกำจัดหายนะแบบนี้แหละที่เป็นเหตุผลว่า immutable data ถึงได้รับความนิยม