Refactoring notes-Refining functions

Source: Internet
Author: User

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


in the previous three articles, the refactoring primer, the bad taste of the code (above), the bad taste of the code (below) were introduced. This article will formally open the journey of refactoring. From the beginning of this article in the following article will be introduced in 92 kinds of refactoring techniques, each refactoring technique will correspond to a code bad taste. In the process of introduction, each kind of refactoring will also correspond to an article, perhaps some of the refactoring techniques are relatively brief, but in order to facilitate the collation or separate it as an article. PS: Anyway, I will insist on sharing these refactoring techniques to everyone, I think someone will benefit from it, but also hope to help you .

Let's learn the refactoring technique of the refinement function.


straight to

Discovery: You have a piece of code that can be organized together and come out independently.

Workaround: Put this code into a separate function and let the function name explain the purpose of the function.


Before refactoring:

public void printowing (double amount) {Printbanner ();//PrintdetailSystem.err.println ("Name:" + _name); System.err.println ("Amount:" + amount);}

After refactoring:

public void printowing (double amount) {Printbanner ();p rintdetails (amount);} private void Printdetails (double amount) {System.err.println ("name:" + _name); System.err.println ("Amount:" + amount);}

Motive

Refinement functions are one of the most commonly used refactoring techniques. When we see a long function or a piece of code that needs to be annotated to make people understand the purpose, in general, we should refactor it to put this code into a separate function.

We all like short, well-named functions. There are three main reasons: first, if the granularity of each function is very small, then the opportunity for the function to be reused is greater, and the second is that it makes the high-level function read as a series of annotations, easy to understand, and third, if the function is fine-grained, then the function's replication will be easier.

You might ask, how long is a function appropriate? For refactoring, length is not a problem, the key is the semantic distance between the function name and the function topic. If refining can enhance the clarity of the code, it is worthwhile to do so even if the function name is longer than the extracted code.


Practices(1) Create a new function that is named according to the intent of the function. (2) Copy the extracted code from the source function into the newly created target function. (3) Carefully examine the extracted code to see when the "scope is limited to the original function" variables (including local variables and source function parameters) are referenced. (4) Check if there is a temporary variable "only for the extracted code snippet". If so, it is also declared as a temporary variable in the target function. (5) Check the extracted code snippet to see if any of the local variable values are changed by it. If a temporary variable value has been modified, see if it is possible to process the extracted snippet into a query and assign the result to the relevant variable. If there are multiple variables that are hard to do or modified, you need to use the explode temporary variable or replace the temporary variable with a query for processing, and then try to refine it. (6) Pass the local variables that need to be read in the extracted code snippet as parameters to the target function.
(7) After all local variables have been processed, the compilation is done.
(8) After successful programming, in the source function, the extracted code snippet is replaced with the call to the target function.
(9) test.


Example

A: no local variables

in the simplest case, the refinement function is a breeze:
//refactoring before public void printowing () {Enumeration e = _orders.elements ();d ouble outstanding = 0.0;//print bannerSystem.err.println ("************************************"); System.err.println ("****************ok******************"); System.err.println ("************************************");//Caculate Outstandingwhile (e.hasmoreelements ()) { Order each = (order) e.nextelement (); Outstanding + = Each.getamout ();} Print DetailsSystem.err.println ("Name:" + _name); System.err.println ("Amout:" + outstanding);} 
After refactoring public void printowing () {Enumeration e = _orders.elements ();d ouble outstanding = 0.0;printbanner ();//Caculate Outs Tandingwhile (E.hasmoreelements ()) {Order each = (order) e.nextelement (); Outstanding + = Each.getamout ();} Print DetailsSystem.err.println ("Name:" + _name); System.err.println ("Amout:" + outstanding);} private void Printbanner () {//Print bannerSystem.err.println ("************************************"); System.err.println ("****************ok******************"); System.err.println ("************************************");}

B: There are local variables

The difficult point of this refactoring technique is that there are local variables, including the arguments to the source function and the temporary variables declared by the source function. Because the scope of a local variable is limited to the source function, you must take extra effort to handle the variables. In some cases, local variables make this refactoring impossible. The simple case of a local variable is that the extracted snippet simply reads the values of those variables without modifying them. In this case, they can be passed as arguments to the target function.
There is a local variable public void printowing () {Enumeration e = _orders.elements ();d ouble outstanding = 0.0;printbanner ();//Caculate OU Tstandingwhile (E.hasmoreelements ()) {Order each = (order) e.nextelement (); Outstanding + = Each.getamout ();} Printdetails (outstanding);} private void Printdetails (double outstanding) {System.err.println ("name:" + _name); System.err.println ("Amout:" + outstanding);}

C: Re-assignment of local variables

If the extracted code snippet assigns a value to a local variable, the problem becomes complex. This is only a case where the temporary variables are discussed. Scenario 1: This variable is used only in the extracted code snippet. In this case, the declaration of the variable is moved directly into the extracted code.
Refinement calculation function public void printowing () {Printbanner ();d ouble outstanding = getoutstanding ();p rintdetails (outstanding);} Private double getoutstanding () {Enumeration e = _orders.elements ();d ouble outstanding = 0.0;while (E.hasmoreelements ()) {Order each = (order) e.nextelement (); Outstanding + = Each.getamout ();} return outstanding;}
Scenario 2: Code other than the extracted code also uses this variable. There are two types of situations here:(1) If the variable is not used after the extracted code snippet, it is only necessary to modify it in the target function. In the following code, the enumeration variable e is only used in the extracted code snippet, so it can be moved throughout the new function.
Private double getoutstanding () {Enumeration e = _orders.elements ();d ouble result = 0.0;while (E.hasmoreelements ()) {Orde R each = (Order) e.nextelement (); result+= each.getamout ();} return result;}
(2) If the variable is also used after the extracted code snippet, you need to have the target function return the value after the variable has changed. In the following code, the double variable outstanding is used inside and outside the extracted code snippet, so the new function that is extracted must be returned to it.
There are parameter refactoring before public void printowing (double previousamount) {Enumeration E = _orders.elements ();d ouble outstanding = Previousa Mount * 1.5;printbanner ();//Caculate Outstandingwhile (e.hasmoreelements ()) {Order each = (order) e.nextelement (); Result + = Each.getamout ();} Printdetails (outstanding);}
After refactoring public void printowing (double previousamount) {Double outstanding = Previousamount * 1.5;printbanner (); outstanding = Getoutstanding (Outstanding);p rintdetails (outstanding);} Private double getoutstanding (double initialvalue) {Double result = Initialvalue;while (E.hasmoreelements ()) {Order each = (Order) e.nextelement (); result + = Each.getamout ();} return result;}
after compiling and testing, the code is then collated as follows:
Final adjustment public void printowing (double previousamount) {Printbanner (); outstanding = getoutstanding (Previousamount * 1.5) ;p Rintdetails (outstanding);}
 This paper mainly introduces the method of reconstruction-refinement function, and makes a brief introduction to different situations. In this article, the examples are relatively simple, for those who have just learned more easily accepted. However, if there are too many temporary variables that can sometimes make refining work difficult, then we need to consider combining other refactoring techniques to deal with them. After learning a number of refactoring techniques, I would like to use you to be sure of the natural clarity. In short, through the study of this article, you will have some help, for example, for a piece of code to do only one thing and not affected by other code, then you can use the refactoring method of this article to refine it into a separate function and so on.
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-inline function)

Refactoring notes-Refining functions

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.