Reconstruction Method 04: replace temp with query (replace temporary variables with queries)

Source: Internet
Author: User

 

YourProgramSaves the computing effect of an expression as a temporary variable.Extract this expression into an independent function. Replace all reference points of this temporary variable with calls to the new function. After that, the new function can be called by other functions.

DoubleBaseprice = _ quantity * _ itemprice;

If(Base price> 1000)

{

ReturnBaseprice * 0.95;

}

Else

{

ReturnBaseprice * 0.98;

}

 

 

If(Base price ()> 1000)

{

ReturnBaseprice () * 0.95;

}

Else

{

ReturnBaseprice () * 0.98;

}

.........

Private IntBaseprice ()

{

Return_ Quantity * _ itemprice;

}

 

Motivation: The problem with temporary variables is that they are temporary and can only be used within the function. Temporary variables are only visible within the function, 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 more clearly for this classCode.

Replace temp with Query(Replace temporary variables with queries)Extract Method(Extract function) an essential step before. Local variables make the code hard to be extracted, so you should try to replace them with the query type.

This refactoring method is relatively simple: the temporary variable is assigned only once, or the expression assigned to the temporary variable is not affected by other conditions. Other cases are tricky, but they may also happen. You may need to useSplit Temporary Variable(Break down temporary variables) orSeparate Query Form Modifier(Separating query functions from modifying functions) Simplifies the situation and replaces temporary variables. If the temporary variables you want to replace are used to collect results) such as the accumulated values in the loop, you need to copy some program logic (such as loops) to the query function.

Practice:1Find the temporary variable assigned only once. If a temporary variable is assigned more than once, useSplit Temporary VariableBreak down temporary variables into multiple variables.

2Declare the variableConst.

3, Compile. This ensures that temporary variables are assigned only once.

4, Extract the right part of the equal sign of the "assign value to this temporary variable" statement to an independent function. First, declare the functionPrivate. You may find that more classes need to be used in the future. It is easy to relax and protect it. Make sure that the extracted function has no side effects, that is, the function does not modify any object content. If it has side effects, performSeparate Query Form Modifier(Separate query functions from modify functions).

5Compile and test.

6, Implemented on the variableInline temp(Inline temporary variables ).

We often use temporary variables to save the accumulated information in the loop. In this case, this loop can be extracted into an independent function, which also allows the original function to reduce the number of disturbing loop logic. Sometimes, you may accumulate several values in a loop. In this case, you should repeat the loop for each accumulated value so that all the temporary variables can be replaced with the query. Of course, the loop should be very simple, and copying the code will not bring danger.

With this technique, you may be worried about performance issues. Like other problems, we don't care about it now, because it will not have any impact at all. If the performance is really faulty, you can solve it during the optimization period. The code is well organized and you can often find more effective optimization solutions. If reconstruction is not used, a good optimization solution may be out of touch with you. If the performance is too bad, it is easy to put the temporary variables back.

 

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.