Refactoring notes-Inline temporary 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/42167015


Inline functions are described in the previous article. This article introduces the refactoring technique of " inline temporary variable".

Let's learn the refactoring technique together. (PS: Although some refactoring techniques are simple to look at, they can be used in conjunction with other refactoring techniques to produce similar effects.)


straight to

Discovery: You have a temporary variable that is assigned only once by a simple expression, and it interferes with other refactoring techniques.

Workaround: Replace any reference action on the variable with the expression itself that assigns the value to it.

Before refactoring double baseprice = Singleorder.baseprice (); return (Baseprice > 1000)
Return after refactoring (Singleorder.baseprice () > 1000)


Motive

Inline temporary variables Most of the cases are used as part of "Replace temporary variables with queries" (described in the next article), and the real motivation is to appear in replace temporary variables with queries. The only case where an inline temporary variable is used alone is when you find that a temporary variable is given a return value for a function call. In general, such temporary variables do not cause any harm or can be safely placed there. However, if this temporary variable interferes with other refactoring techniques (such as refining functions), you should associate the contents.


Practices(1) Check the statement assigned to the temporary variable to ensure that the expression to the right of the equal sign has no side effects. (2) If the temporary variable is not declared final, declare it as final and compile. (This can check if the temporary variable is really only assigned once)(3) Locate all reference points for the temporary variable and replace them with the expression "assign a value to a temporary variable." (4) after each modification, compile and test. (5) After all the reference points have been modified, delete the declaration and assignment statement for the temporary variable.
(6) compile, test.


This paper mainly introduces the reconstruction technique--the inline temporary variable, which is similar to the inline function and is relatively simple to deal with, which is mainly used in combination with other reconstruction techniques. Although the technique is relatively simple, it seems to have to be used in some cases, since using it later will make other refactoring methods more convenient. 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--replace temporary variables with queries)


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.