有一系列条件测试,都得到相同结果
private double disabilityAmount() {if (seniority < 2) return 0;if (monthsDisabled > 2) return 0;if (isPartTime) return 0;// ...
}
重构:将这些条件测试合并为一个条件表达式,并提炼为一个独立函数
private double disabilityAmount() {if (isNotEligibleForDisability()) return 0;// ...
}private boolean isNotEligibleForDisability() {return (seniority < 2) || (monthsDisabled > 2) || isPartTime;
}