Refactoring -- move method (move the method to the class that is often called)

Source: Internet
Author: User
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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.