Refactoring notes-refactoring Methods (3)

Source: Internet
Author: User
Tags expression final min variables pow split variable

5.Introduce explaining Variable    If you have a complicated expression,put the result of the expression, or par TS of the expression, in a temporary variable with a name that explains the purpose.   introduce explaining Var Iable is particulaly valuable with conditional logic in which it are useful to take each clause of a condition and explain What the condition means a well-named temp.   Another case are a long algorithm,in which each step in the Computa tion can be explained with a temp. mechanics  (1) Declare a final temporary variable,and set it to the result of the complex expression.  (2) R Eplace the expression with the value of the temp. Before:double Price () { return Quantity*itemprice-math.max (0,quantity-500) *itemprice*0.05+math.min (quantity* itemprice*0.1,100.0);} After:double Price () { final double baseprice= quantity*itemprice; final double Quantitydiscount=math.max (0 , quantity-500) *itemprice*0.05;  fInal double shipping=math.min (quantity*itemprice*0.1,100.0);  return baseprice-quantitydiscount+shipping;}

Appendix:using Extract to complete refactoring. The benefit of the using Extract method is this theses methodologies are available to the "the" object that needs them. Although they are private at first,but can relax this if another object needs.

Double Price () {return baseprice ()-quantitydiscount () +shipping ();} Private Double Baseprice () {return quantitydiscount*itemprice;} Private Double Quantitydiscount () {return math.min (quantity*itemprice*0.1,100.0);} Private Double Shipping () {return math.min (quantity*itemprice*0.1,100.0);}

6.Split temporary variable  If a temporary Variable assigned to more than once,but be not a loop Variable nor a colle Cting temporary variable (If the later assignments are of the form i=i+some expression, that indicates. It is a collcet ing temporary variable), you should the use of Split temporary method.any variable with more than one responsibility should to be rep Laced with a temp for each responsibility. michanics:  (1) Change the name of "a" temp at it declaration and its A-assignment.  (2) Declare the new temp as final.  (3) Change all references of the temp up to its second assignment. (4)  declare the temp at its second assignment. before:double getdistancetravelled (int time) { double result; double acc=primaryforce/mass; int Primarytime=math.min (time,delay);  result = 0.5*acc*primarytime*primarytime; int secondaryTime= Time-delay; if (secondarytime>0) {  double primaryvel=acc*delay;  acc= (primaryForce+ Secondaryforce)/maSs;  result+=primaryvel*secondarytime+0.5*acc*secondarytime*secondarytime; } return result;} after:double getdistancetravelled (int time) { double result; final double primaryacc=primaryforce/mass;  int primarytime=math.min (time,delay);  result = 0.5*primaryacc*primarytime*primarytime; int Secondarytime=time-delay; if (secondarytime>0) {  double PRIMARYVEL=PRIMARYACC *delay;   final double secondaryacc= (primaryforce+secondaryforce)/mass;  result+=primaryvel*secondarytime+  0.5*secondaryacc*secondarytime*secondarytime; } return result;}

Appendix:complete refactoringdouble getdistancetravelled (int time) {double: result = 0.5*GETPRIMARYACC () * Math.pow (Getprimarytime (Time), 2);  if (Getsecondarytime (time) >0) {double Primaryvel=getprimaryacc () *delay;  Final double secondaryacc= (primaryforce+secondaryforce)/mass; Result+=primaryvel*getsecondarytime (time) + 0.5*secondaryacc*math.pow (Getsecondarytime (Time), 2); return result;} private int Getprimarytime (int time) {return math.min (time,delay);} Private double Getprimaryacc () {return primaryforce/mass;} private int Getsecondarytime (int time) {return time-delay}

7.Remove Assignments to Parametersbefore:int discount (int inputval, int quantity, int yeartodate) {if (inputval>50) INP utval-=2; if (quantity>100) inputval-=1; if (yeartodate>10000) inputval-=4; return inputval;}

After:int Discount (int inputval, int quantity, int yeartodate) {int result=inputval; if (inputval>50) result-=2; if (qua ntity>100) Result-=1; if (yeartodate>10000) result-=4; return result;}

8.Replace method and Method Object If There was a long method that uses the local variables in such a way this you cannot app The-ly Extract Method.turn the Mothod into its own object, all, and the local variables became in that object. You can then decompose to the other methods on the same object. Michanics: (1) Create a new class, name it after the method. (2) Give the new Class A final field for the "object" hosted the original method and a field for each temporary variable d each parameter int the method. (3) Give the new class A constructor that takes the source object and each parameter. (4) Give the new Class A method named "Compute" and copy the body of the original to it.

Before:class Account{int Gamma (int inputval,int quantity,int yeartodate) {int importantval1= (inputval*quantity) +delta (); int importantval2= (Inputval*yeartodate) +100;if ((yeartodate-importantval1>100)) Importantval2-=20;int Importantval3=importantval2*7;return Importantval3-2*importantval1}}

After:class Account{int Gamma (int inputval,int quantity,int yeartodate) {Return to New gamma (this, inputval, quantity, YearToDate);}

Class Gamma{public Gamma (account account,int inputval,int quantity,int yeartodate) {account=account;  Inputval=inputval;  quantity=quantity; Yeartodate=yeartodate; Final account account; int importantVal1; int importantVal2; int importantVal3; int inputval; int quantity; int yeartodate;

int compute () {int importantval1= (inputval*quantity) +account.delta (); int importantval2= (inputval*yeartodate) +100;      if ((yeartodate-importantval1>100)) Importantval2-=20;int importantval3=importantval2*7; return importantval3-2*importantval1; }}

9.Substitue Algorithmreplace a algorithm with one this is clearer.


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.