Replace temporary variables with queries

Source: Internet
Author: User

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


the inline temp variable is described in the previous article. This article describes the refactoring technique of replacing temporary variables with queries.

Let's learn this refactoring technique.


straight to

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

Workaround: Extract the expression into a separate function. Replace all reference points for this temporary variable with a call to the new function.

Before refactoring double baseprice = _quantity * _ITEMPRICE;IF (Baseprice >) return baseprice * 0.95;elsereturn Baseprice * 0.98;
After refactoring if (Baseprice () >) return Baseprice () * 0.95;elsereturn baseprice () * 0.98;...double Baseprice () {return  _quantity * _ITEMPRICE;}


Motive

We all know that temporary variables are temporary and can only be used in a function that belongs to them. So they will drive you to write longer functions, because only then can you access the temporary variables you need. If you replace a temporary variable with a query, all functions in the same class will be able to obtain this information. This will give you a lot of help, so you can write more clear code for this class.

This refactoring method is often a necessary step before you apply the refinement function. Local variables make the code difficult to refine, so you should replace them with queries whenever possible. The simple case is that the temporary variable is assigned only once, or the expression assigned to the temporary variable is not affected by other conditions.


Practicesfor the simple case:(1) Find the temporary variable that is assigned only sequentially (if used more than once, consider splitting it into multiple variables, corresponding to the subsequent refactoring method). (2) Declare the temporary variable as final. (3) compile (make sure that the temporary variable is definitely assigned only once)(4) Extract the right portion of the statement "Assign a value to this temporary variable" to a separate function. (the function is declared as Private first.) In the future you may find that there are many places to use it, and it is easy to relax and protect it; ensure that the extracted function has no side effect, that the function does not modify any object content)(5) compile, test. (6) Implement the "inline temporary variable" refactoring method on the temporary variable.

We often use temporary variables to hold the accumulated information in the loop. In this case, the entire loop can be refined into a separate function, which can reduce the number of lines of the original function in the loop logic code. The use of this technique may cause you to worry about performance problems. Just like any other performance problem, we don't have to worry about it now, because it doesn't have any effect at all. Even if something really goes wrong, you can solve it in the optimization period. The code is well organized, and you will find more efficient optimizations, and if not refactored, a good optimization scheme may be missed.


Examplelet's start with a simple function:
Refactoring before double GetPrice () {int baseprice = _quantity * _itemprice;double discountfactor;if (Baseprice > Discountfact) or = 0.95;elsediscountfactor = 0.98;return Baseprice * discountfactor;}
we want to remove all two of these temporary variables. Of course, each time one is processed.
While the code is simple, it is still necessary to declare the temporary variables as final, and check that they are indeed only assigned once.
Double GetPrice () {final int baseprice = _quantity * _itemprice;final double discountfactor;if (Baseprice > Discou) Ntfactor = 0.95;elsediscountfactor = 0.98;return Baseprice * discountfactor;}
this way, the compiler warns if there is a problem. This is necessary because if a temporary uneasy intersection is not just assigned a value, this refactoring should not be done. below start replacing temporary variables, one at a time, don't worry, step by step. first, the right-hand expression of the assignment action is extracted:
Double GetPrice () {final int baseprice = Baseprice (); final double discountfactor;if (Baseprice >) Discountfactor = 0.95;elsediscountfactor = 0.98;return Baseprice * discountfactor;} Private double Baseprice () {return _quantity * _itemprice;}
then, compile and test, and then start using the inline temporary variable. first, replace the first reference point of the temporary variable Baseprice:
Double GetPrice () {final int baseprice = Baseprice (); final double discountfactor;if (Baseprice () >) discountfactor = 0.95;elsediscountfactor = 0.98;return Baseprice * discountfactor;}
compile, test, and then proceed to the next. Since the next one is the last reference point, the declaration of the Baseprice temporary variable is removed:
Double GetPrice () {final double discountfactor;if (Baseprice () >) Discountfactor = 0.95;elsediscountfactor = 0.98; Return Baseprice ()  * discountfactor;}
after completing the Baseprice, the Discountfactor () is extracted in a similar way:
Double GetPrice () {final double discountfactor = Discountfactor (); return Baseprice ()  * discountfactor;} Private double Discountfactor () {if (Baseprice () >) return 0.95;elsereturn 0.98;}
you will find how hard it will be to refine discountfactor () without replacing the temporary variable Baseprice with a query.eventually, GetPrice () becomes this:
After refactoring double GetPrice () {return baseprice ()  * Discountfactor ();}

 This paper mainly introduces the reconstruction technique--replacing the temporary variable with the query. By refactoring the code to be visible, the code becomes fairly concise, and the intent of the code is clearly expressed. Once a problem occurs in the code, it is easier to modify it because the code is short and clear. I think you are also slowly like the refactoring! If it is, then learn and progress together, make the code beautiful!
Finally, I hope this article will be of some help to you. There are questions to leave a message, thank you. (PS: The next article will introduce refactoring notes--introduction of explanatory 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


Replace temporary variables with queries

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.