Reorganize function----Extra Method (refinement function)

Source: Internet
Author: User

You have a piece of code that can be organized together and independent.

Say this code in a separate function, and let the function name explain the purpose of the function

Example: no local variables

void printowing (double amount) {

Printbanner ();

Printdetail

System.out.println ("Name:" +_name);

System.out.println ("Amount:" +amount);

}

Post-Refactoring code:

void Printowint (double amount) {

Printbanner ();

Printdetail (amount);

}

void Printdetail (double amount) {

System.out.println ("Name:" +_name);

System.out.println ("Amount:" +amount);

}


MotiveExtra Method (Refinement function) is one of the most commonly used refactoring techniques. When you see a function that is too long or a piece of code that requires a comment to understand the purpose, you should put the code into a separate function.
Example: There are local variablesIs that so simple? Where is the difficulty of this refactoring technique? Yes, it is in the local variable, including the arguments to the source function and the zero when declared by the source function. The scope of local variables is limited to source functions, so when I use the extra Method (refinement function), I have to take extra effort to deal with these variables. Sometimes they can hinder me and make it impossible for me to do this refactoring at all.The simplest case of a local variable is that the extracted code snippet simply reads the values of these variables and does not modify them. In this case, you can simply pass them as arguments to the target function.
void printowing{enumeration e=orders.elements (); double outstanding=0.0;Printbanner ();
//calculate Outstandingwhile (e.hasmoreelements) { Order order= (Order) e.nextelement ();outstanding +=each.getamount (); }System.out.println ("Name:" +_name); System.out.println ("Amount:" +outstanding);
}

you can tell the print details are refined into functions with one parameter:void printowing{enumeration e=orders.elements (); double outstanding=0.0;Printbanner ();
//calculate Outstandingwhile (e.hasmoreelements) { Order order= (Order) e.nextelement ();outstanding +=each.getamount (); }Printdetails (outstading);}
void Printdetails (double outstanding) {System.out.println ("Name:" +_name); System.out.println ("Amount:" +outstanding); }
if necessary, you can use this technique to process multiple local variables.

Example: Assigning a value to a local variable
If the extracted code assigns a value to a local variable, the problem becomes complex. This discusses only the issue of temporary variables. If you find that the parameters of the source function are assigned, remove Assignments to Parameters (131) should be used immediately. the assigned temporary variable is also divided into two cases. The simpler case is that this variable is only used in the extracted code snippet. If so, you can move the declaration of the temporary variable into the extracted code snippet and extract it together. Another case is that the code outside the extracted snippet also uses this variable. This is divided into two cases: if the variable is not used after the extracted code snippet, you only need to modify it in the target function, and if the code after the snippet is refined, you need to have the target function return the variable value. The following code can illustrate these different situations.
void Printowing () {enumeration e=_orders.elements (); double outstanding=0.0;Printbanner (); //calculate Outstandingwhile (e.hasmoreelements) { Order order= (Order) e.nextelement (); Outstanding+=order.getamount (); }Printdetails (outstanding);
}
now extract the "Compute" code:void Printowing () {Printbanner (); double outstanding=getoutstanding (); Printdetails (outstanding);
}
Double getoutstanding () {enumeration e=_orders.elements (); double outstanding=0.0;while (e.hasmoreelements) { Order order= (Order) e.nextelement (); Outstanding+=order.getamount (); } return outstading;}




Reorganize function----Extra Method (refinement function)

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.