Reconstruction Method Replace Temp with Query (Replace temporary variables with queries), replacequery

Source: Internet
Author: User

Reconstruction Method Replace Temp with Query (Replace temporary variables with queries), replacequery

Total returned directory

6.4 Replace Temp with Query (Replace temporary variables with queries) Summary

Your program saves the computation results of an expression with a temporary variable.

Extract this expression into an independent function. Replace all reference points of this temporary variable with calls to the function. Then, the new function can be used by other functions.

Motivation

The problem with temporary variables is that they are only visible to the current function. If you access this variable in a similar place, you must re-write the expression to get the variable. In this way, you will inadvertently complicate your function. If you replace a temporary variable with a query, all functions of the same type can obtain this information.

So if you want to use the Extract Method, Replace Temp with Query is an essential step. The Inline Temp we introduced earlier is actually part of this technique. The difference between the two is that Inline TempAlready has the expression itself, You only need to make a simple replacement,The expression must be used to remove the temporary variable.. Replace Temp with Query is more comprehensive, which contains the extracted expression to the function itself, and then replaces the reference point (Inline Temp ). If you replace all the temporary variables with a query, the structure and logic of your class will be very clear, which will be more conducive to your refactoring and optimization.

Example

Suppose there are the following simple functions:

double GetPrice(){   int basePrice = _quantity * _itemPrice;   double discountFactor;   if (basePrice > 1000)   {       discountFactor = 0.95;   }   else   {       discountFactor = 0.98;   }   return basePrice * discountFactor;}    

We will replace the basePrice and discountFactor temporary variables.

Replace basePrice first, extract the expression on the Right of basePrice, use Inline Temp to replace all reference points of basePrice, and remove the declaration of the temporary basePrice variable.

Now the code becomes like this:

double GetPrice(){   double discountFactor;   if (GetBasePrice() > 1000)   {      discountFactor = 0.95;   }   else   {      discountFactor = 0.98;   }   return GetBasePrice() * discountFactor; }private int GetBasePrice(){   return _quantity * _itemPrice;}

Then extract GetDiscountFactor () in a similar way ();

private double GetDiscountFactor(){   if (GetBasePrice() > 1000)   {       return 0.95;   }   return 0.98;}

The final code is like this.

double GetPrice(){   return GetBasePrice() * GetDiscountFactor();}private double GetDiscountFactor(){    if (GetBasePrice() > 1000)    {        return 0.95;    }   return 0.98;}private int GetBasePrice(){   return _quantity * _itemPrice;}

We can see that this refactoring method improves the clarity of the function itself and makes it easier for us to perform later refactoring.

Summary

We often use temporary variables in the loop to save the accumulated information. In this case, we can extract the entire loop. If you need to accumulate several values in the loop, repeat the loop for each accumulated value. In this way, you can easily replace all temporary variables with queries.

Of course, in this way, we may worry about performance issues. Originally, I defined a temporary variable to be queried only once. Now I want to perform a query every time. We don't have to worry about this. The purpose of refactoring is to make the program clearer. It is not too late to make specific optimization after a program is clearer. Furthermore, according to the principle, this query statement is unlikely to cause performance problems in your system. If it is because of this statement, you can put the variable back.

 

To Be Continued...

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.