Reconstruction Methods: Extrct Method (extraction function) and extrctmethod
Total returned directory
This section contains three methods:
1. Extract Method (extraction function)
2. Inline Method (Inline Function)
3. Inline Temp (Inline temporary variable)
6.1 Extract Method (Extract function) Overview
You have a piece of code that can be organized together and independent.
Put this code into an independent function and ask the function name to explain the function.
Motivation
If there is a function that is too long or requires a comment to make people understand the purpose of the code, then put this code into an independent function.
Benefits of short and well-named functions:
- Small function granularity, large chance of reuse
- Make high-level functions read as comments
- Functions are fine-grained and easier to be rewritten.
Remember how Long is the function described in the second bad smell-Long Method? The author said: "In my opinion, length is not a problem. The key lies in the semantic distance between the function name and the function ontology. If refining can enhance the Definition of the code, then do it, even if the function name is longer than the extracted Code ."
Example
Public class receept {private List <decimal> Discounts {get; set;} private List <decimal> ItemTotals {get; set;} public decimal CalculateGrandTotal () {decimal subTotal = 0 m; // calculate the total subTotal foreach (decimal itemTotal in ItemTotals) {subTotal + = itemTotal;} // subTotal must be cyclically subtracted from discount, that is, calculate Discount if (Discounts. count> 0) {foreach (decimal discount in Discounts) {subTotal-= discount ;}// calculate Tax decimal tax = subTotal * 0.065 m; subTotal + = tax; return subTotal ;}}
The code after reconstruction is as follows:
Public class receept {private List <decimal> Discounts {get; set;} private List <decimal> ItemTotals {get; set;} public decimal CalculateGrandTotal () {decimal subTotal = CalculateSubTotal (); subTotal = CalculateDiscounts (subTotal); subTotal = CalculateTax (subTotal); return subTotal;} // calculate the total of subTotal private decimal CalculateSubTotal () {decimal subTotal = 0 m; foreach (decimal itemTotal in ItemTotals) {subTotal + = itemTotal;} return subTotal;} // calculate the discount private decimal CalculateDiscounts (decimal subTotal) {if (Discounts. count> 0) {foreach (decimal discount in Discounts) {subTotal-= discount ;}return subTotal ;}// calculate Tax private decimal CalculateTax (decimal subTotal) {decimal tax = subTotal * 0.065 m; subTotal + = tax; return subTotal ;}}
Summary
Is This refactoring method very simple. I believe that most people use this technique. Maybe you do it in your subconscious.
I don't know if your company uses this as a reference in code writing specifications, such as the maximum number of lines in a method. To some extent, this can also enable programmers to strip these complex logics into small methods with clear meanings.
6.2 Inline Method (Inline Function) Overview
The ontology and name of a function are also clear and easy to understand.
Insert the function body at the function call point and remove the function.
Motivation
The indirect layer can make functions easy to understand, but if the function itself is easy to understand, this indirect layer is useless and can be removed.
Example
int GetRating(){ return MoreThanFiveLateDeliveries() ? 2 : 1;}bool MoreThanFiveLateDeliveries(){ return _numberOfLateDeliveries > 5;}
For this function, the MoreThanFiveLateDeliveries method is a useless indirect layer. Because the original function is clear, remove it here.
The code after reconstruction is as follows:
int GetRating(){ return _numberOfLateDeliveries > 5 ? 2 : 1;}
Summary
This method is relatively simple. But do not ignore it because it is simple. When inline functions are used, make sure that the function does not have polymorphism because the subclass cannot override a function that does not exist at all.
6.3 Inline Temp (Inline temporary variable) Summary
You have a temporary variable assigned only once by a simple expression, which hinders other refactoring methods.
Replace all the referenced actions on the variable with the expression itself assigned to it.
Motivation
This method is mostly used as part of the next method Replace Temp with Query.
When used separately, a temporary variable is assigned the return value of a function call, which hinders other refactoring methods.
Example
bool GetBasePrice(){ double basePrice = anOrder.GetBasePrice(); return basePrice > 1000;}
The code after reconstruction is as follows:
bool GetBasePrice(){ return anOrder.GetBasePrice() > 1000;}
Summary
Note the following when replacing a temporary variable with the expression itself,Whether the temporary variable is assigned only once in the function. If not once, do not do this.
To Be Continued...