Introduce explaining variable

Source: Internet
Author: User

If (platform. toUpperCase (). indexOf ("MAC")>-1 )&&
(Brower. toUpperCase (). indexOf ("IE")>-1 )&&
WasInitialized () & resize> 0)
{
// Do something
}
==>
Final boolean isMacOs = platform. toUpperCase (). indexOf ("MAC")>-1;
Final boolean isIEBrowser = browser. toUpperCase (). indexOf ("IE")>-1;
Final boolean wasResized = resize> 0;

If (isMacOs & isIEBrowser & wasInitialized () & wasResized ){
// Do something
}

Motivation:

Expressions may be complex and difficult to read. In this case, temporary variables can help you break down expressions into manageable forms.

In conditional logic,Introduce explaining variable(124) special value: You can use this refactoring to extract each Condition Clause and use a well-named temporary variable to explain the meaning of the corresponding Condition Clause. another scenario in which this refactoring is used is that in a long algorithm, temporary variables can be used to explain the meaning of each operation.

Introduce explaining variable(124) is a very common refactoring method, but I have to admit that I don't use it frequently. I almost always try to use it whenever possible.Extract Method(110) to explain the meaning of a piece of code. after all, a temporary variable is meaningful only in the function where it is located, and has many limitations. A function can be useful throughout the entire life of an object and can be used by other objects. but sometimes, when a local variable causesExtract Method(110) when it is difficult, I will useIntroduce
Explaining variable
(124 ).

Practice

1. Declare a final temporary variable and assign the calculation result of some actions in the complex expression to it.

2. Replace the [operation result] In the expression with the above temporary variable.

If this part is repeated in the code, you can replace it one by one each time.

3 compile and Test

4. Repeat the above process to process other parts of the expression

 

Double price (){
// Price is base price-quantity discount + Shipping
Return _ quantity * _ itemprice-
Math. Max (0, _ quantity-500) * _ itemprice * 0.05 +
Math. Min (_ quantity * _ itemprice * 0.1, 100.0 );
}

 

First, I found that the base price is equal to the quantity multiplied by the unit price, so I put the calculation results into a temporary variable:

Double price (){
// Price is base price-quantity discount + Shipping
Final double baseprice = _ quantity * _ itemprice;
Return baseprice-
Math. Max (0, _ quantity-500) * _ itemprice * 0.05 +
Math. Min (baseprice * 0.1, 100.0 );
}

 

Then, I extracted the calculation of the wholesale discount (quantity discount) and assigned the result to the Temporary Variable quantitydiscount:

 

Double price (){
// Price is base price-quantity discount + Shipping
Final double baseprice = _ quantity * _ itemprice;
Final double quantitydiscount = math. Max (0, _ quantity-500 )*
_ Itemprice * 0.05;
Return baseprice-quantitydiscount +
Math. Min (baseprice * 0.1, 100.0 );
}

Finally, I extracted the shipping calculation and assigned the calculation result to the Temporary Variable shipping. at the same time, I can also delete the comments in the code, because now the code can perfectly express its meaning:
Double price (){
// Price is base price-quantity discount + Shipping
Final double baseprice = _ quantity * _ itemprice;
Final double quantitydiscount = math. Max (0, _ quantity-500 )*
_ Itemprice * 0.05;
Final double shipping = math. Min (baseprice * 0.1, 100.0 );
Return baseprice-quantitydiscount + shipping;
}

 

Use extract method to process the above example

Double price (){
// Price is base price-quantity discount + Shipping
Return _ quantity * _ itemprice-
Math. Max (0, _ quantity-500) * _ itemprice * 0.05 +
Math. Min (_ quantity * _ itemprice * 0.1, 100.0 );
}
This time I extracted the base price calculation into an independent function:
Double price (){
// Price is base price-quantity discount + shipping
Return basePrice ()-
Math. max (0, _ quantity-500) * _ itemPrice * 0.05 +
Math. min (basePrice () * 0.1, 100.0 );
}
Private double basePrice (){
Return _ quantity * _ itemPrice;
}
I continue to refine my work, and each time I extract a new function, I finally get the following code:
Double price (){
// Price is base price-quantity discount + shipping
Return basePrice ()-quantityDiscount () + shipping ();
}
Private double quantityDiscount (){
Math. max (0, _ quantity-500) * _ itemPrice * 0.05;
}
Private double shipping (){
Math. Min (baseprice () * 0.1, 100.0 );
}
Private double baseprice (){
Return _ quantity * _ itemprice;
}

I prefer to useExtract Method(110), because any part of the same object can use the extracted functions as needed. in the beginning, I will declare these new functions as private. If other objects need them, I can easily release the access restrictions of these functions. I also found that,Extract Method(110) workload is usually not requiredIntroduce explaining VariaBle (124) comes big.

When should I use it?Introduce explaining variable(124? The answer is:Extract Method(110) if I want to handle an algorithm with a large number of local variablesExtract Method(110) it is not easy. In this case, I will useIntroduce explaining variable(124) Help me clean up the code, and then consider what to do next. After figuring out the code logic, I can always useReplace
Temp with Query
(120) Remove the explanatory temporary variables I introduced. Besides, if I finally useReplace method with method object(135), so the explanatory temporary variables introduced by me also have their value

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.