Reconstruction Method: Introduce Explaining Variable (referencing explanatory variables) and explainingvariable

Source: Internet
Author: User

Reconstruction Method: Introduce Explaining Variable (referencing explanatory variables) and explainingvariable

Total returned directory

6.5 Introduce Explaining Variable (reference explanatory Variable) Summary

You have a complex expression.

Put the results of this complex expression (or part of it) into a temporary variable, and use this variable name to explain the purpose of the expression.

Motivation

Sometimes you may encounter a series of complex continuous expressions. At this time, you may not be able to stand for such a long function or such a complex long function. At this time, you can reference temporary variables to store their results, and divide the results of these long functions into temporary variables to clarify the function. This refactoring method is widely used in conditional logic.

Let's look at a condition judgment:

if(platform.ToUpper().IndexOf("MAC")>-1&&browser.ToUpper().IndexOf("IE")>-1&&  wasInitialized()&&resize>0){   //do something          }

Does it look dizzy? I don't know what this condition determines?

We use this method to reconstruct:

 bool isMacOs = platform.ToUpper().IndexOf("MAC") > -1; bool isIEBrowser = browser.ToUpper().IndexOf("IE") > -1; bool wasResized = resize > 0; if (isMacOs && isIEBrowser && wasInitialized()&& wasResized) {     //do something    }  

The code is clear.

Example

We start with a simple calculation:

double GetPrice(){    return _quantity * _itemPrice - Math.Max(0, _quantity - 500) * _itemPrice *     0.05 +  Math.Min(_quantity * _itemPrice * 0.1, 100.0);}     

This code is simple but hard to understand. You can perform Introduce Explaning Variable and put the calculation result of _ quantity * _ itemPrice into the temporary Variable.

double GetPrice(){    double basePrice = _quantity * _itemPrice;    return basePrice - Math.Max(0, _quantity - 500) * _itemPrice * 0.05 +           Math.Min(basePrice * 0.1, 100.0);}

Then extract the discount.

double GetPrice(){    double basePrice = _quantity * _itemPrice;    double quantityDiscount = Math.Max(0, _quantity - 500) * _itemPrice * 0.05;    return basePrice - quantityDiscount + Math.Min(basePrice * 0.1, 100.0);}

Finally, extract the freight calculation. The final code is as follows.

double GetPrice(){    double basePrice = _quantity * _itemPrice;    double quantityDiscount = Math.Max(0, _quantity - 500) * _itemPrice * 0.05;    double shipping = Math.Min(basePrice * 0.1, 100.0);    return basePrice - quantityDiscount + shipping;}

Although this is done, as we have mentioned earlier, temporary variables only make sense in the function in which they are located, and have great limitations. functions can be used throughout the entire life of objects, and can be used by other objects. So next we will use the Extract Method to refactor the previous example. We also recommend this method.

Use Extract Method to process the above example

The same function:

double GetPrice(){    return _quantity * _itemPrice - Math.Max(0, _quantity - 500) * _itemPrice *     0.05 +  Math.Min(_quantity * _itemPrice * 0.1, 100.0);}     

This time we extract base price calculation, wholesale discounts, and freight to a new function. The final code is as follows:

double GetPrice(){    return GetBasePrice() - GetQuantityDiscount() + GetShipping();}private double GetQuantityDiscount(){    return Math.Max(0, _quantity - 500) * _itemPrice * 0.05;}private double GetBasePrice(){     return _quantity * _itemPrice;}private double GetShipping(){     return Math.Min(GetBasePrice() * 0.1, 100.0);}
Two Comparison methods

Compare the functions generated by Extract Method and Introduce Explaining Variable:

1. The former generates shorter and clearer functions, while the latter generates a large number of temporary variables, making the function longer;

2. The former generates many independent functions. If you want to access a function externally, you can directly call the function. The latter has to re-write the method for its call.

Summary

I personally recommend the Extract Method, because any part of the same object can use the extracted functions as needed. At the beginning, we can declare functions as private. If other objects require them, release the access restrictions of these functions.

In this case, when can we use Introduce to explain the Variable? When the Extract Method requires more work. For example, there is an algorithm that uses a large number of local variables. In this case, the Extract Method is not easy to handle.

 

To Be Continued...

Related Article

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.