提出问题

项目开发中如何简化条件表达式???

解决问题

下面来自《重构》这本书的笔记和自己的一点总结,希望可以节省大家看书时间。

Decompose Conditional(分解条件表达式)

你有一个复杂的条件表达式(if-then-else)语句。从if,then。else三个段落中分别提炼出独立函数。

例一:

  1. public void test(){
  2. int sendDay = 1000;//坚持送了几天
  3. int flowerNum = 9;//花朵数量
  4. int engagementDay = 100000;//约会天数
  5. int callMinuteTimes = 10000;
  6. //重构前
  7. if(engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999){
  8. System.out.println("恭喜你" + "--" + "可以结婚了,姑娘!!");
  9. }
  10. //重构后,例子比较简单,没什么需要解释
  11. if(isLove(engagementDay,callMinuteTimes,sendDay,flowerNum)){
  12. System.out.println("恭喜你" + "--" + "可以结婚了,姑娘!!");
  13. }
  14. }
  15. public boolean isLove(int engagementDay,int callMinuteTimes,int sendDay,int flowerNum){
  16. return engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999;
  17. }

Consilidate Conditional Expression(合并条件表达式)
你有一系列条件测试,都得到相同结果。将这些测试合并为一个条件表达式。并将这个条件表达式提炼成为一个独立函数。

例二:

@Test
public boolean test(){
   int sendDay = 1000;//坚持送了几天
   int flowerNum = 9;//花朵数量
   int engagementDay = 100000;//约会天数
   int callMinuteTimes = 10000;
   //重构前
   if(engagementDay > 100000) return true;
   if(callMinuteTimes > 10000) return true;
   if (sendDay * flowerNum >999999) return true;
   //重构后
   if(isLove(engagementDay,callMinuteTimes,sendDay,flowerNum)){
       return true;
   }
   return false;
}
public boolean isLove(int engagementDay,int callMinuteTimes,int sendDay,int flowerNum){
   return engagementDay > 100000 || callMinuteTimes > 10000 || sendDay * flowerNum >999999;
}

Consolidate Duplicate Conditional Fragments(合并重复的条件片段)

在条件表达式的每个分支上,有着相同的一段代码,将这段重复代码搬移到条件表达式之外。

例三:

//重构前
if(isSpecialDeal()){
   total = price * 0.95;
   send();
}else{
   total = price * 0.98;
   send();
}
//重构后
if(isSpecialDeal()){
   total = price * 0.95;
}else{
   total = price * 0.98;
}
send();

Remove Control Flag(移除控制标记)

在一系列布尔表达式中,某个变量带有控制标记的作用,以break语句或return语句取代控制标记。

例四:

重构前:

@Test
public boolean test(int loveCallNum){
//某个变量带有控制标记的作用
boolean isLove = false;
   for(int i=0;i<loveCallNum;i++){
       if(isLove){
           System.out.println("我们结婚吧!");
       }else{
           if(i == 999){
               isLove = true;
           }
       }
   }
}

重构后:

for(int i=0;i<loveCallNum;i++){
   if(i == 999){
       System.out.println("我们结婚吧!");
       //以break语句或return语句取代控制标记。
       break;
   }
}

Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件表达式)

函数中的条件逻辑使人难以看清正常的执行路径。使用卫语句表现所有特殊情况。

卫语句:就是把复杂的条件表达式拆分成多个条件表达式,比如一个很复杂的表达式,嵌套了好几层的if-then-else语句,转换为多个if语句,实现它的逻辑,这多条的if语句就是卫语句

例五:

重构前:

   int sendDay = 1000;//坚持送了几天
   int flowerNum = 9;//花朵数量
   int engagementDay = 100000;//约会天数
   int callMinuteTimes = 10000;
   //重构前
   if(engagementDay < 100000){
       System.out.println("待定......");
   }else{
       if(callMinuteTimes < 10000){
           System.out.println("待定......");
       }else{
           if(sendDay * flowerNum >999999){
               System.out.println("真爱......");
           }
       }
   }

重构后:下面就是卫语句

if(engagementDay < 100000) System.out.println("待定......");
if(callMinuteTimes < 10000) System.out.println("待定......");
if(sendDay * flowerNum >999999) System.out.println("真爱......");

Replace Conditional with Polymorphism(以多态取代条件表达式)

你手上有个条件表达式,他根据对象类型的不同而选择不同的行为,将这个条件表达式的每个分支放进一个子类内的覆写函数中,然后将原始函数声明为抽象函数。

Introduce Assertion(引入断言)

某一段代码需要对程序状态作出某种假设,以断言明确表现这种假设。

例六:

//获取子任务对象
PcsSubTask pcsSubTask = pcsSubTaskService.findById(subTaskId);
//在这里添加断言,确定pcsSubTask确实不为空
AssertUtils.checkResourceFound(pcsSubTask);