1. refactoring
| The so-called refactoring means "Readjusting the internal structure of the software without changing its external functions ". Rename: You can change the name of a class, function, or member. Extract Method: encapsulate a code segment into a new function. ---------------------------- Encapsulate field: convert a field to an attribute extract interface: convert an attribute or function to an interface, instead of the current attribute or function to an interface implementation. Promote local variable to parameter: upgrade a local variable to function parameter reorder parameters: sort function parameters remove parameters: delete a function parameter, but use of this parameter in the function will not be deleted. |
Reconstruction function (chapter6)
Extract Method
Premise: indirect variables have been processed through other refactoring methods.
Objective: to narrow down the granularity of functions to increase reuse and enhance the definition of code.
Objective: The function name can well express the "function" to be implemented by the function ". Instead of doing anything.
Inline Method
Is the inverse process of extract method. It is precisely because of these inverse processes that you can reconstruct with confidence.
Premise: the inline function cannot be polymorphism. Unable to express polymorphism after inline.
Purpose: Remove unnecessary indirect information. Or the premise for organizing a group of unreasonable functions.
Objective: Remove unnecessary functions or excessive delegation.
Extract Method Is one of the most common refactoring. When a method looks too long or some code in the method needs to be annotated to understand its purpose, you can consider extracting them as an independent method. For example:
void PrintOwing()
{
double outstanding = 0;
//print banner
Console.WriteLine("**********");
Console.WriteLine("***Owes***");
Console.WriteLine("**********");
//Calculate outstanding
foreach(Order o in orders)
{
outstanding += o.Amount;
}
//print details
Console.WriteLine("Name: " + name);
Console.WriteLine("Amount: " + outstanding);
}
After reconstruction, the code after extraction is as follows:
void PrintOwing()
{
PrintBanner();
outstanding = GetOutStanding();
PrintDetails(outstanding);
}
void PrintBanner()
{
Console.WriteLine("**********");
Console.WriteLine("***Owes***");
Console.WriteLine("**********");
}
void PrintDetails(int outstanding)
{
Console.WriteLine("Name: " + name);
Console.WriteLine("Amount: " + outstanding);
}
int GetOutStanding()
{
double result = 0;
foreach(Order o in orders)
{
result += o.Amount;
}
return result;
}
After reconstruction, the code looks much refreshed. The extraction method can also improve code reusability and code modularization.
* Usage: select the method or attribute to be reconstructed, right-click ---> refactor ---> select the corresponding operation.