如果一个方法中包含多个布尔类型的参数,一是方法不容易理解,二是调用时容易出错。

重构前代码

 public class BankAccount
    {
        public void CreateAccount(Customer customer, bool withChecking, bool withSavings, bool withStocks)
        {
            // do work
        }
    }

 

重构后代码

public class BankAccount
    {
        public void CreateAccountWithChecking(Customer customer)
        {
            CreateAccount(customer, true, false);
        }

        public void CreateAccountWithCheckingAndSavings(Customer customer)
        {
            CreateAccount(customer, true, true);
        }

        private void CreateAccount(Customer customer, bool withChecking, bool withSavings)
        {
            // do work
        }
    }

 重构后,将原来方法改为private防止外部调用,而暴露出命名良好的方法供调用。

相关文章:

  • 2022-01-25
  • 2021-09-21
  • 2022-12-23
  • 2022-01-12
  • 2022-01-20
  • 2022-12-23
猜你喜欢
  • 2021-09-18
  • 2021-08-06
  • 2021-11-29
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
相关资源
相似解决方案