Replace temporary variables with queries

Source: Internet
Author: User

Replace temporary variables with queries

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


In the previous article, we introduced "inline temporary variables". This article describes how to refactor "replace temporary variables with queries.

Let's learn about This refactoring method.


Open door

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

Solution: extract the expression into an independent function. Replace all reference points of this temporary variable with calls to the new function.

// Double basePrice = _ quantity * _ itemPrice before reconstruction; if (basePrice> 5000) return basePrice * 0.95; elsereturn basePrice * 0.98;
// After reconstruction, if (basePrice ()> 5000) return basePrice () * 0.95; elsereturn basePrice () * 0.98 ;... double basePrice () {return _ quantity * _ itemPrice ;}


Motivation

We all know that temporary variables are temporary and can only be used in the function to which they belong. So they will drive you to write longer functions, because only in this way can you access the required temporary variables. If you replace a temporary variable with a query, all functions in the same class can obtain this information. This will bring you great help, so that you can write clearer code for this class.

This refactoring method is often an essential step before you use the refining function. Local variables make the code difficult to extract, so we should try to replace them with the query type. In a simple case, a temporary variable is assigned only once, or the expression assigned to the temporary variable is not affected by other conditions.


The procedure is simple: (1) Find the temporary variables that are assigned in sequence (if used multiple times, consider splitting them into multiple variables, corresponding to the subsequent reconstruction method ). (2) Declare the temporary variable as final. (3) Compile (ensure that the temporary variable is assigned only once) (4) extract the right part of the equal sign of the statement "assign value to this temporary variable" to an independent function. (First, declare the function as private. In the future, you may find that there are many other places to use it. At that time, it is easy to relax its protection. Make sure that the extracted function has no side effects, that is, the function does not modify any object content) (5) Compile and test. (6) implement the "inline temporary variable" Reconstruction Method for the temporary variable.

We often use temporary variables to save the accumulated information in the loop. In this case, the entire loop can be extracted into an independent function, which can reduce several lines of cyclic logic code in the original function. This method may cause you to worry about performance issues. Just like other performance problems, we don't need to worry about it now, because there is no impact at all. Even if something really goes wrong, you can solve it during the optimization period. If the code is well organized, you will also find a more effective optimization solution. If you do not refactor it, the good optimization solution may be out of touch with you.


Let's start with a simple function:
// Double getPrice () {int basePrice = _ quantity * _ itemPrice; double discountFactor; if (basePrice> 5000) discountFactor = 0.95; elsediscountFactor = 0.98; return basePrice * discountFactor ;}
We want to remove both of the temporary variables. Of course, each request is processed.
Although the code is simple, it is necessary to declare the temporary variables as final and check whether they are assigned only once.
double getPrice() {final int basePrice = _quantity * _itemPrice;final double discountFactor;if (basePrice > 5000)discountFactor = 0.95;elsediscountFactor = 0.98;return basePrice * discountFactor;}
In this way, the compiler will warn once there is a problem. The reason for this is that if a temporary insecure intersection is assigned only once, the reconstruction should not be performed. Next, replace the temporary variable. Do not worry about it every time. First, extract the expression on the right of the value assignment action:
double getPrice() {final int basePrice = basePrice();final double discountFactor;if (basePrice > 5000)discountFactor = 0.95;elsediscountFactor = 0.98;return basePrice * discountFactor;}private double basePrice() {return _quantity * _itemPrice;}
Then, compile and test, and then start to use inline temporary variables. First, replace the first reference point of the Temporary Variable basePrice with the following:
double getPrice() {final int basePrice = basePrice();final double discountFactor;if (basePrice() > 5000)discountFactor = 0.95;elsediscountFactor = 0.98;return basePrice * discountFactor;}
Compile, test, and then proceed to the next step. 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() > 5000)discountFactor = 0.95;elsediscountFactor = 0.98;return basePrice()  * discountFactor;}
After completing the basePrice, extract the discountFactor () in a similar way ():
double getPrice() {final double discountFactor = discountFactor() ;return basePrice()  * discountFactor;}private double discountFactor(){if (basePrice() > 5000)return 0.95;elsereturn 0.98;}
You will find that if you do not replace the temporary variable basePrice with a query type, how difficult it will be to extract discountFactor ()! Finally, getPrice () becomes like this:
// Double getPrice () {return basePrice () * discountFactor ();} after reconstruction ();}

This article mainly introduces the refactoring method-replacing temporary variables with queries. After the code is restructured, the Code becomes quite concise, and the intent expressed by the Code becomes clearer. Once a problem occurs in the code, it is easy to modify because the code is short and clear. I wonder if you like refactoring too! If so, let's learn and make progress together to make the code beautiful!
Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you. (PS: the next article will introduce refactoring notes-Introducing explanatory variables)

Rebuild note articles as follows

Rebuild notes-getting started

Rebuilding notes-bad taste of code (I)

Rebuilding notes-bad taste of Code (Part 2)

Rebuilding notes -- Building a test body

Rebuilding notes -- refining Functions

Rebuilding notes-inline functions

Refactoring notes-inline temporary 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.