Code
// Before reconstruction Code Note:
//The accountinterest class has a bankaccount instance. the calculateinterestrate () method is called in the accountinterest class, and this method is not dependent on the current live class instance (not necessarily called)
/// <Summary>
/// Simple bank account
/// </Summary>
Public Class Bankaccount
{
Public Bankaccount ( Int Accountage, Int Creditscore, accountinterest)
{
Accountage = Accountage;
Creditscore = Creditscore;
Accountinterest = Accountinterest;
}
Public Int Accountage { Get ; Private Set ;}
Public Int Creditscore { Get ; Private Set ;}
Public Accountinterest { Get ; Private Set ;}
/// <Summary>
/// The removed method is not used (or rarely used) in its live class. Therefore, we consider porting it to accountinterest,
/// Improve code readability and maintainability: Frequently Used classes need to be read and used, and the chance of Code changing to this method is higher, similarly, the most important consideration for modifying this method is the business logic commonly used in its class.
/// </Summary>
/// <Returns> </returns>
Public Double Calculateinterestrate ()
{
If (Creditscore > 800 )
Return 0.02 ;
If(Accountage> 10)
Return 0.03;
Return 0.05;
}
}
/// <Summary>
/// Interest
/// </Summary>
Public Class Accountinterest
{
Public Bankaccount account { Get ; Private Set ;}
PublicAccountinterest (bankaccount account)
{
Account=Account;
}
Public double interestrate
{< br> Get { return account. calculateinterestrate () ;}< BR >}
Public BoolIntroductoryrate
{
Get{ReturnAccount. calculateinterestrate ()< 0.05;}
}
}
Refactoring becomes natural. The code after refactoring is as follows:
/// <Summary>
/// Simple bank account
/// </Summary>
Public Class Bankaccount
{
Public Bankaccount ( Int Accountage, Int Creditscore, accountinterest)
{
Accountage = Accountage;
Creditscore = Creditscore;
Accountinterest = Accountinterest;
}
Public Int Accountage { Get ; Private Set ;}
Public Int Creditscore { Get ; Private Set ;}
Public Accountinterest { Get ; Private Set ;}
}
/// <Summary>
/// Interest
/// </Summary>
Public Class Accountinterest
{
Public Bankaccount account { Get ; Private Set ;}
PublicAccountinterest (bankaccount account)
{
Account=Account;
}
Public DoubleInterestrate
{
Get{ReturnCalculateinterestrate ();}
}
Public Bool Introductoryrate
{
Get { Return Calculateinterestrate () < 0.05 ;}
}
/// <Summary>
/// Methods survive in frequently called classes, and their logic is more closely related to this class.
/// </Summary>
/// <Returns> </returns>
Public Double Calculateinterestrate ()
{
If (Creditscore > 800 )
Return 0.02 ;
If(Accountage> 10)
Return 0.03;
Return 0.05;
}
}
Maintainability is one of the important criteria for measuring code quality. This refactoring is worth considering!