zoukankan      html  css  js  c++  java
  • 重构30-Return ASAP(尽快返回)

    该话题实际上是诞生于移除箭头反模式重构之中。在移除箭头时,它被认为是重构产生的副作用。为了消除箭头,你需要尽快地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;
    }
    }






  • 相关阅读:
    JavaScript 对象
    Java条件语句
    函数的使用注意事项:
    函数的特点
    函数的格式
    for循环
    break和continue的区别和作用
    注意事项
    CSS浮动清除的方法
    转:Oracle 中union的用法
  • 原文地址:https://www.cnblogs.com/jgig11/p/5786508.html
Copyright © 2011-2022 走看看