徐州软件开发网、徐州程序员、徐州程序员招聘求职、徐州软件企业推荐、 软件项目交易。点击注册加入我们,进入论坛发布消息

【行动胜于雄辩】徐州程序员交流QQ群:324379(一群,满)1182709(二群).进群请先看公告! 招聘求职请先在本站'招聘\求职区'发布,只在群里发布的,将被请出程序员交流群! 谢谢合作

本主题共有 0 篇回复,最新回复发表于 10-10-2009 22:21,作者 xzsdn_08
页 1 / 1 (1 项)
帖子排序: 上一主题 下一主题
  • 10-10-2009 22:21

    • xzsdn_08
    • 灌水10强
      男
    • 注册时间 01-04-2009
    • 江苏徐州
    • 发帖总数 641

    31天重构指南 30尽快返回

    Refactoring Day 30 : Return ASAP

    This topic actually came up during the Remove Arrowhead Antipattern refactoring. The refactoring introduces this as a side effect to remove the arrowhead. To eliminate the arrowhead you return as soon as possible.

       1: public class Order
       2: {
       3:     public Customer Customer { get; private set; }
       4:  
       5:     public decimal CalculateOrder(Customer customer, IEnumerable<Product> products, decimal discounts)
       6:     {
       7:         Customer = customer;
       8:         decimal orderTotal = 0m;
       9:  
      10:         if (products.Count() > 0)
      11:         {
      12:             orderTotal = products.Sum(p => p.Price);
      13:             if (discounts > 0)
      14:             {
      15:                 orderTotal -= discounts;
      16:             }
      17:         }
      18:  
      19:         return orderTotal;
      20:     }
      21: }

    The idea is that as soon as you know what needs to be done and you have all the required information, you should exit the method as soon as possible and not continue along.

       1: public class Order
       2: {
       3:     public Customer Customer { get; private set; }
       4:  
       5:     public decimal CalculateOrder(Customer customer, IEnumerable<Product> products, decimal discounts)
       6:     {
       7:         if (products.Count() == 0)
       8:             return 0;
       9:  
      10:         Customer = customer;
      11:         decimal orderTotal = products.Sum(p => p.Price);
      12:  
      13:         if (discounts == 0)
      14:             return orderTotal;
      15:  
      16:         orderTotal -= discounts;
      17:  
      18:         return orderTotal;
      19:     }
      20: }

    This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

页 1 / 1 (1 项)
徐州软件开发网、徐州软件开发社区、徐州程序员
© Copyright 2008-2011 XZSDN.NET   Powered By communityserver   站长博客 我的Blog
苏ICP备09002379号 | 意见反馈 | 联系我