Glossary
Refactoring Terminology
หัวข้อที่มีชื่อว่า “Refactoring Terminology”| English | Thai Style | ความหมาย |
|---|---|---|
| Refactoring | Refactoring | ปรับปรุงโครงสร้าง code โดยที่ behavior ยังเหมือนเดิม |
| Code Smell | Code Smell | สัญญาณที่บอกว่า code มีปัญหาโครงสร้าง ยังไม่ใช่ bug |
| Technical Debt | Technical Debt | ต้นทุนสะสมจาก code ที่รีบเขียนโดยไม่ดูแลคุณภาพ |
| Maintainability | maintainability | ความง่ายในการแก้ไขและต่อเติม code ในอนาคต |
| Readability | readability | ความง่ายในการอ่านและเข้าใจ code |
| Complexity | complexity | ความซับซ้อนของ code ที่เพิ่ม cognitive load ผู้อ่าน |
| Coupling | coupling | ระดับการพึ่งพากันระหว่าง module หรือ class |
| Cohesion | cohesion | ระดับที่ code ในหน่วยเดียวกันเกี่ยวข้องกัน |
| Responsibility | responsibility | หน้าที่หรือเหตุผลที่ class/function มีอยู่ |
| Behavior | behavior | สิ่งที่ code ทำ — ผลลัพธ์ที่ caller มองเห็นจากภายนอก |
| Legacy Code | legacy code | code เดิมที่ขาด test และเปลี่ยนได้ยาก |
| Cognitive Load | cognitive load | จำนวนสิ่งที่ต้องคิดพร้อมกันขณะอ่าน code |
Refactoring Techniques
หัวข้อที่มีชื่อว่า “Refactoring Techniques”| English | ทำอะไร |
|---|---|
| Extract Function | แยก code block ออกมาเป็น function ใหม่ที่มีชื่อ |
| Extract Class | แยก responsibility ออกจาก class ใหญ่เป็น class ใหม่ |
| Inline Function | รวม function ที่ชื่อไม่ได้บอกอะไรเพิ่มกลับเข้า caller |
| Inline Variable | รวม variable ที่ไม่ได้ช่วยอธิบาย กลับไปใส่ใน expression |
| Move Method | ย้าย method ไปอยู่ใน class ที่เกี่ยวข้องกันมากกว่า |
| Move Field | ย้าย field ไปอยู่ใน class ที่ใช้งานจริง |
| Rename Variable | เปลี่ยนชื่อให้สื่อ intent ชัดขึ้น |
| Rename Function | เปลี่ยนชื่อ function ให้ตรงกับสิ่งที่ทำจริง ๆ |
| Encapsulate Field | ห่อหุ้ม field ด้วย getter/setter เพื่อควบคุม access |
| Extract Variable | ตั้งชื่อให้ expression ซับซ้อนด้วย variable ที่อธิบาย |
| Replace Temp with Query | แทน temporary variable ด้วย function ที่ query ค่านั้น |
| Introduce Parameter Object | รวม parameter ที่มักไปด้วยกันเป็น object เดียว |
| Replace Conditional with Polymorphism | แทน switch/if-else ด้วย subclass หรือ strategy |
| Split Phase | แบ่ง function ที่ทำสองขั้นตอนออกเป็นสอง function |
| Replace Magic Literal | แทนตัวเลข/string ลอยตัวด้วย named constant |
Code Smells
หัวข้อที่มีชื่อว่า “Code Smells”| Code Smell | อาการ | วิธีแก้ |
|---|---|---|
| Long Method | function ยาว ทำหลายหน้าที่ | Extract Function |
| Large Class | class ใหญ่ มี responsibility มากเกินไป | Extract Class |
| Feature Envy | method สนใจ data ของ class อื่นมากกว่า class ตัวเอง | Move Method |
| Duplicate Code | logic เดียวกันปรากฏในหลายที่ | Extract Function |
| Data Clumps | parameter ชุดเดียวกันปรากฏในหลาย function | Introduce Parameter Object |
| Primitive Obsession | ใช้ primitive type แทน object ที่มีความหมาย | Replace Primitive with Object |
| Divergent Change | เปลี่ยน class เดียวด้วยเหตุผลหลายอย่าง | Extract Class |
| Shotgun Surgery | เปลี่ยนหนึ่งเรื่องแต่ต้องแก้หลาย class | Move Method / Move Field |
| Switch Statement | switch ที่ซ้ำอยู่หลายที่ | Replace Conditional with Polymorphism |
| Magic Number | ตัวเลขลอยตัวใน code ที่ไม่รู้ความหมาย | Replace Magic Literal |
| Dead Code | code ที่ไม่มีใครเรียกแล้ว | ลบทิ้ง |
| Temporary Field | field ที่มีค่าเฉพาะบางสถานการณ์ | Extract Class |
| God Object | class เดียวรู้ทุกอย่างและทำทุกอย่าง | Extract Class หลายรอบ |
เมื่อ ไม่ ควร Refactor
หัวข้อที่มีชื่อว่า “เมื่อ ไม่ ควร Refactor”- ไม่มี test รองรับ — refactor โดยไม่มี test คือการเดา
- ใกล้ปล่อย production — เสี่ยง regression เกินไป
- ยังไม่เข้าใจ business logic — แก้ผิดจุดแน่นอน
- เป็น code ที่ยังไงก็กำลังจะลบทิ้งอยู่แล้ว
- ตรงจุดนั้น performance สำคัญกว่า readability