Refactoring Day 2 : Move Method

The refactoring today is pretty straightforward, although often overlooked and ignored as being a 
worthwhile refactoring. Move method does exactly what it sounds like, move a method to a better 
location. Let’s look at the following code before our refactoring: 
 1: public class BankAccount 
 2: { 
 3: public BankAccount(int accountAge, int creditScore, 
 AccountInterest accountInterest) 
 4: { 
 5: AccountAge = accountAge; 
 6: CreditScore = creditScore; 
 7: AccountInterest = accountInterest; 
 8: } 
 9: 
 10: public int AccountAge { get; private set; } 
 11: public int CreditScore { get; private set; } 
 12: public AccountInterest AccountInterest { get; private set; } 
 13: 
 14: public double CalculateInterestRate() 
 15: { 
 16: if (CreditScore > 800) 
 17: return 0.02; 
 18: 
 19: if (AccountAge > 10) 
 20: return 0.03; 
 21: 
 22: return 0.05; 
 23: } 
 24: } 
 25: 
 26: public class AccountInterest 
 27: { 
 28: public BankAccount Account { get; private set; } 
 29: 
 30: public AccountInterest(BankAccount account) 
 31: { 
 32: Account = account; 
 33: } 
 34: 
 35: public double InterestRate 
 36: { 
 37: get { return Account.CalculateInterestRate(); } 
 38: } 
 39: 
 40: public bool IntroductoryRate 
 41: { 
 42: get { return Account.CalculateInterestRate() < 0.05; } 
 43: } 
 44: } 
 
The point of interest here is the BankAccount.CalculateInterest method. A hint that you need the Move 
Method refactoring is when another class is using a method more often then the class in which it lives. If 
this is the case it makes sense to move the method to the class where it is primarily used. This doesn’t work 
in every instance because of dependencies, but it is overlooked often as a worthwhile change. 
In the end you would end up with something like this: 
 1: public class BankAccount 
 2: { 

 3: public BankAccount(int accountAge, int creditScore,AccountInterest accountInterest) 

 4: { 
 5: AccountAge = accountAge; 
 6: CreditScore = creditScore; 
 7: AccountInterest = accountInterest; 
 8: } 
 9: 
 10: public int AccountAge { get; private set; } 
 11: public int CreditScore { get; private set; } 
 12: public AccountInterest AccountInterest { get; private set; } 
 13: } 
 14: 
 15: public class AccountInterest 
 16: { 
 17: public BankAccount Account { get; private set; } 
 18: 
 19: public AccountInterest(BankAccount account) 
 20: { 
 21: Account = account; 
 22: } 
 23: 
 24: public double InterestRate 
 25: { 
 26: get { return CalculateInterestRate(); } 
 27: } 
 28: 
 29: public bool IntroductoryRate 
 30: { 
 31: get { return CalculateInterestRate() < 0.05; } 
 32: } 
 33: 
 34: public double CalculateInterestRate() 
 35: { 
 36: if (Account.CreditScore > 800) 
 37: return 0.02; 
 38: 
 39: if (Account.AccountAge > 10) 
 40: return 0.03; 
 41: 
 42: return 0.05; 
 43: } 
 44: } 
 
Simple enough! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值