C # Reconstruction

Source: Internet
Author: User

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.

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.