Decompose Conditional (แยกเงื่อนไขที่ซับซ้อน)

A figure illustrates how the refactoring technique is used to decompose the conditional expression.

การแสดงในรูปแบบแผนภาพแสดงคำสั่ง conditional (if-else) ที่ซับซ้อนถูกแยกย่อยเป็นชิ้น code หลายชิ้น ซึ่งแต่ละชิ้นมีการเรียกใช้ function code จริงอ่านว่า if (!aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd)) charge = quantity * plan.summerRate; else charge = quantity * plam.regularRate + plan.regularServiceCharge; code หลังการ refactoring อ่านว่า if (summer()) charge = summerCharge(); else charge = regularCharge();

Motivation (แรงจูงใจ)

หนึ่งในแหล่งที่มาของความซับซ้อนที่พบบ่อยที่สุดในโปรแกรมคือ conditional logic ที่ซับซ้อน เวลาผมเขียน code ให้ทำงานต่าง ๆ กันไปตามเงื่อนไขหลายอย่าง ผมอาจลงเอยด้วย function ที่ยาวมากได้อย่างรวดเร็ว Long Function เป็นปัจจัยที่ทำให้อ่านยากอยู่แล้ว แต่เงื่อนไขยิ่งเพิ่มความยากขึ้นไปอีก ปัญหามักอยู่ที่ว่า code ทั้งในส่วนการตรวจสอบเงื่อนไขและส่วนการกระทำต่าง ๆ บอกผมว่ามีอะไรเกิดขึ้น แต่กลับบดบังได้ง่ายว่า ทำไม มันถึงเกิดขึ้น

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

จริง ๆ แล้วนี่เป็นเพียงกรณีหนึ่งของการใช้ Extract Function ( 106 ) กับ code ของผม แต่ผมอยากเน้นกรณีนี้ไว้ เพราะเป็นกรณีที่ผมพบว่าการลงมือทำแบบนี้ให้ผลคุ้มค่าอย่างน่าทึ่ง

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

  • ใช้ Extract Function ( 106 ) กับ conditional และแต่ละแขนงของ conditional

Example (ตัวอย่าง)

สมมติว่าผมกำลังคำนวณค่าใช้จ่ายสำหรับบางอย่างซึ่งมีอัตราแยกกันสำหรับฤดูหนาวและฤดูร้อน:

if (!aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd))
  charge = quantity * plan.summerRate;
else
  charge = quantity * plan.regularRate + plan.regularServiceCharge;

ผมแยกเงื่อนไขออกมาเป็น function ของมันเอง

if (summer())
  charge = quantity * plan.summerRate;
else
  charge = quantity * plan.regularRate + plan.regularServiceCharge;

function summer() {
  return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}

จากนั้นผมทำกับแขนง then :

if (summer())
  charge = summerCharge();
else
  charge = quantity * plan.regularRate + plan.regularServiceCharge;

function summer() {
  return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}
function summerCharge() {
  return quantity * plan.summerRate;
}

สุดท้าย แขนง else :

if (summer())
  charge = summerCharge();
else
  charge = regularCharge();

function summer() {
  return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}
function summerCharge() {
  return quantity * plan.summerRate;
}
function regularCharge() {
  return quantity * plan.regularRate + plan.regularServiceCharge;
}

พอทำเสร็จ ผมชอบจัดรูปแบบ conditional ใหม่โดยใช้ ternary operator

charge = summer() ? summerCharge() : regularCharge();

function summer() {
  return !aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd);
}
function summerCharge() {
  return quantity * plan.summerRate;
}
function regularCharge() {
  return quantity * plan.regularRate + plan.regularServiceCharge;
}