Refactoring notes-replacing functions with function objects

Source: Internet
Author: User

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


in the previous article, "Remove the assignment of parameters" was described . This paper introduces the refactoring technique of "substituting function object for function".

Let's learn this refactoring technique.


straight to

Discover: You have a large function in which the use of local variables makes it impossible for you to use the refactoring technique of "refinement function".

Workaround: Put this function into a separate object so that the local variable becomes the field of the object, and then the large function can be decomposed into several small functions in the same object.

Refactoring before class order....double price () {Double baseprice;double secondaryprice;double thirdaryprice;//compute () ...} <
After refactoring class order...double price () {Return to New Pricecalculator (this). Compute ();} Class pricecalculator{double baseprice;double secondaryprice;double thirdaryprice;double Compute () {//...}}


Motive

In the previous article, we have been emphasizing the graceful and moving of small functions. The readability of the code can be greatly improved by extracting the relatively independent code from the large function.

However, the existence of local variables can increase the difficulty of function decomposition. If the local variables in a function are overrun, it is very difficult to decompose the function. The "Replace temporary variable with a query" technique can help relieve the burden, but sometimes it is found that a function that needs to be disassembled cannot be disassembled at all. In this case, you should consider using a function object to solve. The refactoring method in this article will change all local variables into fields of function objects. You can then use the Refinement function to create new functions that will make the original large function disassembly smaller.



Practices
(1) Create a new class that is named for the purpose of the function to be processed. (2) Create a final field in the new class to hold the object where the original large function resides. For each temporary variable and each parameter of the original function, establish a corresponding field in the new class to preserve it. (3) Create a constructor in the new class that receives the original object with all parameters of the original function as its arguments. (4) Create a compute () function in the new class. (5) Copy the code of the original function into the compute () function. Called by the original object field if any function of the source object needs to be called. (6) compile, test. since all local variables are now fields, you can decompose this large function arbitrarily, without having to pass any arguments.

Example
we're from below .The example begins:
Class Account{int Gamm (int value, int quantity, int year2date) {int importValue1 = (Value * quantity) + delta (); int Importv Alue2 = (Value * year2date) + 200;if (year2date-importvalue1 >200) Importvalue2-=50;int importValue3 = importValue2 * 8 ;//......return importvalue3-2 * importValue1;} //.....}
to turn this function into a function object, you first need to declare a new class. Provides a final object in the new class to save the original object, and for each parameter of the function and for each temporary variable, it is also retained in one field.
Class Gamm{private final account _account;private int value;private int quantity;private int year2date;private int IMPORTV alue1;private int importvalue2;private int importValue3;
Next, add a constructor function.
Gamm (account source, int inputval, int quantity, int year2date) {this._account = Source;this.value = Inputval;this.quantit y = Quantity;this.year2date = year2date;}
The original function can now be moved to compute (). Any place in the function that calls the Accout class, you must use the _account field instead.
int Compute () {importValue1 = (value * quantity) + _account.delta (); importValue2 = (Value * year2date) + 200;if (Year2date- ImportValue1 >200) importvalue2-=50;importvalue3 = importValue2 * 8;//......return importvalue3-2 * IMPORTVALUE1;}
Then, modify the old function so that it delegates the work to the newly completed function object.
int Gamm (int value, int quantity, int year2date) {return new Gamm (this,value,quantity,year2date). Compute ();
        the above is the basic principle of the reconstruction method of this article. The benefit is that it is now easy to take the "refinement function" of the compute () function without worrying about the problem of parameter passing.
Use the refinement function without worrying about parameter problems int compute () {importValue1 = (value * quantity) + _account.delta (); importValue2 = (Value * year2date) + 2 00;importantthing (); importValue3 = importValue2 * 8;//......return importvalue3-2 * IMPORTVALUE1;} private void importantthing () {if (year2date-importvalue1 >200) importvalue2-=50;}

This paper mainly introduces the method of reconstruction--replacing function with function object. We don't like the way that the temporary variables are so big, it just confuses us. For many functions of local variables, it is necessary to use the refactoring method of this article to deal with it, transform it into a function object, then convert the temporary variable into the field of the function object, and then other reconstruction methods. 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 the refactoring note-replacement algorithm)

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

Refactoring notes-Introduction of explanatory variables

Refactoring notes-breaking temporary variables

Refactoring notes-Removing the assignment of parameters

Refactoring notes-replacing functions with function objects

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.