有一个复杂的 if-else 语句
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {charge = quantity * winterRate + winterServiceCharge;
} else {charge = quantity * summerRate;
}
重构: 从if-else 中分别提炼出独立函数
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 summerCharge(int quantity) {return quantity * summerRate;
}private double winterCharge(int quantity) {return quantity * winterRate + winterServiceCharge;
}