Introduction of explanatory variables

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/42417535


the "replace temporary variable with a query" is described in the previous article . This paper introduces the refactoring technique of "introducing explanatory variables".

Let's learn this refactoring technique.


straight to

Discovery: You have a complex expression.

Workaround: Place the result of the complex expression (or part of it) in a temporary variable, explaining the purpose of the expression with this variable name.

Refactoring before if ((Platform.touppercase (). IndexOf ("MAC") >-1) && (Browser.touppercase (). IndexOf ("IE") >-1) &&wasinitialized () && Resize > 0) {//do something}
After refactoring final Boolean ismacos = Platform.touppercase (). IndexOf ("MAC") > -1;final Boolean isiebrowser = Browser.touppercase (). IndexOf ("IE") > -1;final boolean wasresize = resize > 0;if (ismacos && isiebrowser &am p;& wasinitialized () && wasresize) {//do something}


Motive

In some cases, expressions can be so complex that they are difficult to read. In this way, temporary variables can help you break down the expression into a more manageable form.

In conditional logic, the introduction of explanatory variables is more valuable: You can use this refactoring to refine each clause to explain the meaning of the corresponding conditional clause with a well-named temporary variable. Another possible scenario is that for long algorithms, temporary variables can be used to explain the meaning of each step of the operation.

The reconstruction technique of this paper is one of the most common techniques, but it is not so much used. Because in general, we can use the refinement function to explain the meaning of a piece of code. After all, the temporary variable is only meaningful in the function in which it is located, and the function can be useful throughout the life cycle of the object and can be used by other objects. However, when local variables are difficult to use with refinement functions, you can try to use the introduction of explanatory variables.



Practices(1) Declaring a final type of temporary variable, assigning the result of the operation of a part of the complex expression to be decomposed. (2) Replace the part of "Operation result" in the expression with the temporary variable above. (If this part of the substitution is repeated in the code, it can be replaced one at a time)(3) compile, test. (4) Repeat the above process to deal with other similar parts.

Examplelet's start with a simple calculation:
Before refactoring double price () {//prices = Baseprice-quantity Discount + shippingreturn _quantity * _itemprice-math.max (0, _quantity- * _itemprice * 0.15 +math.min (_quantity * _itemprice * 0.25, 100);}
This code is relatively simple, but now it's easier to understand. First, it is found that the reserve price (Baseprice) equals the quantity (quantity) multiplied by the cost of item. You can then put the results of this part into a temporary variable and replace the parameters in the Math.min () function as well.
Double Price () {//prices = Baseprice-quantity discount + shippingfinal Double baseprice = _quantity * _itemprice;return base Price-math.max (0, _quantity-800) * _itemprice * 0.15 +math.min (baseprice * 0.25, 100);}
The calculation of the wholesale discount (quantity discount) is then refined and the result is assigned to a temporary variable.
Double Price () {//prices = Baseprice-quantity discount + shippingfinal Double baseprice = _quantity * _itemprice;final doubl E quantitydiscount = Math.max (0, _quantity-800) * _itemprice * 0.15;return baseprice-quantitydiscount+math.min (basePric E * 0.25, 100);}
Finally, the removal fee (shipping) calculation is refined, and the result of the operation is given to the temporary variable.
After refactoring double price () {//prices = Baseprice-quantity discount + shippingfinal Double baseprice = _quantity * _itemprice;final Double quantitydiscount = Math.max (0, _quantity-800) * _itemprice * 0.15;final Double shipping = math.min (Baseprice * 0. return baseprice-quantitydiscount + shipping;}

using the refinement function to deal withfor the above code, the intent of the action is usually not explained by a temporary variable, but rather the refinement function is preferred. let's start from the beginning:
Before refactoring double price () {//prices = Baseprice-quantity Discount + shippingreturn _quantity * _itemprice-math.max (0, _quantity- * _itemprice * 0.15 +math.min (_quantity * _itemprice * 0.25, 100);}
now, the reserve price calculation is refined into a separate function.
Double Price () {//prices = Baseprice-quantity discount + shippingreturn baseprice ()-math.max (0, _quantity-800) * _ITEMPRI CE * 0.15 +math.min (baseprice () * 0.25, 100);} Private double Baseprice () {return _quantity * _itemprice;}
continue refining, refining a new function at a time. Finally get the code as follows.
After refactoring double price () {//prices = Baseprice-quantity discount + shippingreturn baseprice ()-quantitydiscount () + Shipping ();} Private double Baseprice () {return _quantity * _itemprice;} Private double Shipping () {return math.min (Baseprice () * 0.25, 100);} Private double Quantitydiscount () {return Math.max (0, _quantity-800) * _itemprice * 0.15;}


 This paper mainly introduces the method of reconstruction--introducing explanatory variables. The refactoring method is mainly used when the refinement function takes a lot of effort. For example, if you have an algorithm with a large number of local variables, it is not easy to use the refinement function. At this point, you can use the method of this article to organize the code, and then think about what to do next, once the code logic is clear, you can use the query instead of temporary variables to the intermediate introduced in the temporary variables removed.
I think you would prefer to refine the function, because for any part of the same object, you can take these extracted functions according to your own needs. These new functions are declared private at the outset, and access control for these functions can be easily freed if other objects also need them.
Finally, I hope this article will be of some help to you. There are questions to leave a message, thank you. (PS: Next article will introduce refactoring notes-decomposition of temporary variables)

The refactoring Note article is as follows

Refactoring Notes-Introductory article

Refactoring notes-bad taste of code (UP)

Refactoring notes-bad taste of code (bottom)

Refactoring notes-building a test body

Refactoring notes-Refining functions

Refactoring notes-inline functions

Refactoring notes-Inline temporary variables

Refactoring notes-replacing temporary variables with queries



Introduction of explanatory variables

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.