该话题实际上是诞生于移除箭头反模式重构之中。在移除箭头时,它被认为是重构产生的副作用。为了消除箭头,你需要尽快地return。
public class Order {
public Customer Customer;//getter setter

public Double CalculateOrder(Customer customer, List<Product> products, Double discounts) {
Customer = customer;
Double orderTotal = 0d;
if (products.size() > 0) {
orderTotal = sum(products);
if (discounts > 0) {
orderTotal -= discounts;
}
}
return orderTotal;
}
}
该重构的理念就是,当你知道应该处理什么并且拥有全部需要的信息之后,立即退出所在方法,不再继续执行。
public class Order {
public Customer Customer;//getter setter

public Double CalculateOrder(Customer customer, List<Product> products, Double discounts) {
if (products.size() == 0){
return 0d;
}
Customer = customer;
Double orderTotal = sun(products);
if (discounts == 0)
return orderTotal;
orderTotal -= discounts;
return orderTotal;
}
}






posted on 2016-08-19 10:09  jgig11  阅读(187)  评论(0编辑  收藏  举报