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

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

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

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

    31天重构指南 28为布尔方法命名

    Refactoring Day 28 : Rename boolean method


    Today’s refactoring doesn’t necessarily come from Fowlers refactoring catalog. If anyone knows where this “refactoring” actually comes from, please let me know.

    Granted, this could be viewed as not being a refactoring as the methods are actually changing, but this is a gray area and open to debate. Methods with a large number of boolean parameters can quickly get out of hand and can produce unexpected behavior. Depending on the number of parameters will determine how many methods need to be broken out. Let’s take a look at where this refactoring starts:

       1: public class BankAccount
       2: {
       3:     public void CreateAccount(Customer customer, bool withChecking, bool withSavings, bool withStocks)
       4:     {
       5:         // do work
       6:     }
       7: }

    We can make this work a little better simple by exposing the boolean parameters via well named methods and in turn make the original method private to prevent anyone from calling it going forward. Obviously you could have a large number of permutations here and perhaps it makes more sense to refactor to a Parameter object instead.

       1: public class BankAccount
       2: {
       3:     public void CreateAccountWithChecking(Customer customer)
       4:     {
       5:         CreateAccount(customer, true, false);
       6:     }
       7:  
       8:     public void CreateAccountWithCheckingAndSavings(Customer customer)
       9:     {
      10:         CreateAccount(customer, true, true);
      11:     }
      12:  
      13:     private void CreateAccount(Customer customer, bool withChecking, bool withSavings)
      14:     {
      15:         // do work
      16:     }
      17: }

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

    Kick It on DotNetKicks.com

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