学习重构(5)-简化条件表达式
1.Decompose Conditional (分解条件表达式) 应用场景:你有一个复杂的条件(if-then-else)语句。从if、then、else三个段落中分别提炼出独立函数。 示例: if (date.before(SUMMER_START) || date.after(SUMMER_END)) { charge = quantity * mWinterRate + mWinterServiceCharge; } else { charge = quantity * mSummerRate; } 重构为: if (notSummer(date)) { charge = winterCharge(quantity); } else { charge = summerCharge(quantity); } private boolean notSummer(Date date) { return date.before(SUMMER_START) || date.after(SUMMER_END); } private double winterCharge(int quantity) { return quantity * mWinterRate + mWinterServiceCharge; } private double