Refactoring Notes-Refactoring Methods(3)

5.Introduce Explaining Variable
   If you have a complicated expression,put the result of the expression, or parts of the expression , in a temporary variable with a name that explains the purpose.
   Introduce Explaining Variable is particulaly valuable with conditional logic in which it is useful to take each clause of a condition and explain what the condition means a well-named temp.
   Another case is a long algorithm,in which each step in the computation can be explained with a temp.
Mechanics
 (1)Declare a final temporary variable,and set it to the result of part of the complex expression.
 (2)Replace the result part of 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 Method to complete refactoring.
  The benefit of using Extract Method is that theses methodologies are available to any other part of the object that needs them.Although they are private at first,but you can relax that if another object needs them.

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 is not a loop variable nor a collecting temporary variable(If the later assignments are of the form i=i+some expression, that indicates that it is a collceting temporary variable),you should use Split Temporary Method.Any variable with more than one responsibility should be replaced with a temp for each responsibility.
Michanics:
 (1)Change the name of a temp at its declaration and its first 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 refactoring
double getDistanceTravelled(int time){
 double result;
 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 Parameters
Before:
int discount(int inputVal, int quantity, int yearToDate){
 if(inputVal>50) inputVal-=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(quantity>100) result-=1;
 if(yearToDate>10000) result-=4;
 return result;
}

8.Replace Method with Method Object
  If there is a long method that uses local variables in such a way that you cannot apply Extract Method.Turn the mothod into its own object so that all the local variables became fields on that object.You can then decompose the method into 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 that hosted the original method and a field for each temporary variable and 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 method into 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 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 Algorithm
Replace an algorithm with one that is clearer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:– JavaScript 中的患者数据管理系统 患者数据管理系统是为医院开发的 node JS 项目。通过使用此系统,您可以轻松访问患者数据,它具有成本效益,可改善患者护理和数据安全性。不仅如此,它还减少了错误范围。在运行项目之前,您需要下载 node.js。 这个患者数据管理项目包含 javascript、node.js 和 CSS。我们必须让服务器监听端口 3000,并使用 JSON 在客户端和服务器之间交换数据。这个项目会不断询问您有关插件更新的信息,因此请保持互联网畅通。此系统允许您执行 crud 操作。在这里,您是系统的管理员。您还可以添加所需的员工人数。此外,您还可以更新患者记录。该系统功能齐全且功能齐全。 要运行此项目,您需要在计算机上安装NodeJS并使用现代浏览器,例如 Google Chrome、  Mozilla Firefox。ReactJS项目中的此项目可免费下载源代码。有关项目演示,请查看下面的图像滑块。 对于手动安装 1.将主项目文件夹解压到任意目录 2.从 cmd 设置项目目录的路径 3. 输入命令“npm install” 4.完成后输入命令“npm start” 5.现在,您将获得一个 localhost:portnumber,并转到该 URL 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值