重构代码中的 if/else 嵌套和 switch 语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_41104353/article/details/83004888

不使用else

提前 return

比如有代码:

public int fibonacci(int n){
    if(n <= 2){
	return 1;
    }else{
	return fibonacci(n-1) + fibonacci(n-2);
    }
}

尽量写成:

public int fibonacci(int n){
    if(n <= 2){
	return 1;
    }
    return fibonacci(n-1) + fibonacci(n-2);
}

把可使流程提前中断的代码块(return, throw excetion, continue …)放到 if 中, 提前中断代码。

线性罗列

将多层 if 嵌套的语法写成线性的,就像写规则一样将其一条条罗列出来,比如:

public boolean match(int age, int salary, int pretty) {
    if(age > 18){
        // do something A;
        if(salary > 5000){
            // do something B;
            if(pretty == true){
                return true;
            }
        }
    }
    return false;
}

该写成:

public boolean match(int age, int salary, int pretty) {
    if(age < 18){
        return false;
    }
    // do something A;

    if(salary < 5000){
        return false;
    }
    // do something B;

    return pretty == true;
}

改为do-while

这个好像 linux 源码常用

public void useDoWhile() {
    if (a == 1) { 
        xxx(); 
        if (b == 2) { 
            yyy(); 
        }
    }
}

变成

public void useDoWhile() {
    do { 
        if (a != 1) break; 
        xxx(); 
        if (b != 2) break; 
        yyy(); 
    } while (false); 
}

这个和上面那个是一回事。

表驱动法

代码大全第二版 译序 【初级程序员,请先看第18章“表驱动法”:将复杂的逻辑判断转换为查表.从而简化代码的编写与维护】

该书将表驱动法分为三种:直接访问表,索引访问表和阶梯访问表。

public void tableDriven(int n) {
   if (n == 1 ) {
      System.out.println("n is 1");
   } else if ( n == 2 ) {
      System.out.println("n is 2");
   } else if ( n == 3 ) {
      System.out.println("n is 3");
   }
}
public void tableDriven(int n) {
   String[] nList = new String[3];
   nList[0] = "n is 1"; 
   nList[1] = "n is 2";
   nList[2] = "n is 3";
   System.out.println(nList[n-1]);
}

例子不是那么好。

多态

今天老师讲 策略模式 的时候,刚好也说到用策略模式可以减少 if\else 的使用。不过这里使用的是多态。

public class Duck {
    public static void quack(String DuckType) {
        if (DuckType.equals("MallardDuck")) {
            System.out.println("quack");
        } else if (DuckType.equals("RubberDuck")) {
            System.out.println("squeak");
        } else if (DuckType.equals("DecoyDuck")) {
            System.out.println("couldn't quack");
        }
    }
}
// --------------------------------------  
public void show(String DuckType) {
    Duck.quack(DuckType);
}
public abstract class Duck {
    public abstract void quack(String DuckType) {
    }
}
// -------------------------------------- 下面是另一个类
public class MallardDuck extends Duck {
    public void quack() {
        System.out.println("quack");
    }
}
// --------------------------------------
public class RubberDuck extends Duck {
    public void quack() {
        System.out.println("squeak");
    }
}
// -------------------------------------
public class DecoyDuck extends Duck {
    public void quack() {
        System.out.println("couldn't duck");
    }
}
// -------------------------------------
public void show(Duck duck) {
    duck.quack();
}

参考资料

https://segmentfault.com/q/1010000000330549
https://laravel-china.org/topics/1263/tips-to-make-your-if-else-look-more-beautiful

本文只是个总结,还建议大家看看参考资料的第一个,里面还提到了一些其他的方法。

猜你喜欢

转载自blog.csdn.net/sinat_41104353/article/details/83004888