更新時間:2020-11-17 17:49:38 來源:動力節點 瀏覽7383次
if else 是我們寫代碼時,使用頻率最高的關鍵詞之一,然而有時過多的 if else 語句會讓我們感到代碼過于冗雜,這時候我們就需要采取一些方法來簡化多余的if else語句。下面就分享給大家8種if else語句簡化方法。
1.使用 return
我們使用 return 去掉多余的 else,實現代碼如下。
優化前代碼:
if (str.equals("java")) {
// 業務代碼 ! true;
} else {
return ;
}
優化后代碼:
if (str.equals("java")) {
return ;
}
return false;
這樣看起來就會舒服很多,雖然相差只有一行代碼,但真正的高手和普通人之間的差距就是從這一行行代碼中體現出來的。
2.使用 Map
使用 Map 數組,把相關的判斷信息,定義為元素信息可以直接避免 if else 判斷,實現代碼如下。
優化前代碼:
if (t == 1) {
type = "name";
} else if (t == 2) {
type = "id";
} else if (t == 3) {
type = "mobile";
}
我們先定義一個 Map 數組,把相關判斷信息存儲起來:
MaptypeMap = new HashMap<>();
typeMap.put(1, "name");
typeMap.put(2, "id");
typeMap.put(3, "mobile");
之前的判斷語句可以使用以下一行代碼代替了:
type = typeMap.get(ty);
3.使用三元運算符
三元運算符也叫三元表達式或者三目運算符/表達式,不過代表的都是一個意思,優化代碼如下。
優化前代碼:
Integer score = 81;
if (score > 80) {
score = 100;
} else {
score = 60;
}
優化后代碼:
score = score > 80 ? 100 : 60;
4.使用枚舉
JDK 1.5 中引入了新的類型——枚舉(enum),我們使用它可以完成很多功能,例如下面這個。
優化前代碼:
Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
typeId = 1;
} else if ("Age".equals(type)) {
typeId = 2;
} else if ("Address".equals(type)) {
typeId = 3;
}
優化時,我們先來定義一個枚舉:
public enum TypeEnum {
Name(1), Age(2), Address(3);
public Integer typeId;
TypeEnum(Integer typeId) {
this.typeId = typeId;
}
}
之前的 if else 判斷就可以被如下一行代碼所替代了:
typeId = TypeEnum.valueOf("Name").typeId;
5.使用 Optional
從 JDK 1.8 開始引入 Optional 類,在 JDK 9 時對 Optional 類進行了改進,增加了 ifPresentOrElse() 方法,我們可以借助它,來消除 if else 的判斷,使用如下。
優化前代碼:
String str = "java";
if (str == null) {
System.out.println("Null");
} else {
System.out.println(str);
}
優化后代碼:
Optionalopt = Optional.of("java");
opt.ifPresentOrElse(v ->
System.out.println(v), () -> System.out.println("Null"));
6.梳理優化判斷邏輯
和第 4 點比較類似,我們可以通過分析 if else 的邏輯判斷語義,寫出更加易懂的代碼,例如以下這個嵌套判斷的優化。
我們需要盡量把表達式中的包含關系改為平行關系,這樣代碼可讀性更高,邏輯更清晰。
7.使用多態
繼承、封裝和多態是 OOP(面向對象編程)的重要思想,本文我們使用多態的思想,提供一種去除 if else 方法。
優化前代碼:
Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
typeId = 1;
} else if ("Age".equals(type)) {
typeId = 2;
} else if ("Address".equals(type)) {
typeId = 3;
}
使用多態,我們先定義一個接口,在接口中聲明一個公共返回 typeId 的方法,在添加三個子類分別實現這三個子類,實現代碼如下:
public interface IType {
public Integer getType();
}
public class Name implements IType {
@Override
public Integer getType() {
return 1;
}
}
public class Age implements IType {
@Override
public Integer getType() {
return 2;
}
}
public class Address implements IType {
@Override
public Integer getType() {
return 3;
}
}
8.選擇性的使用 switch
很多人都搞不懂 switch 和 if else 的使用場景,但在兩者都能使用的情況下,可以盡量使用 switch,因為 switch 在常量分支選擇時,switch 性能會比 if else 高。
if else 判斷代碼:
if (cmd.equals("add")) {
result = n1 + n2;
} else if (cmd.equals("subtract")) {
result = n1 - n2;
} else if (cmd.equals("multiply")) {
result = n1 * n2;
} else if (cmd.equals("divide")) {
result = n1 / n2;
} else if (cmd.equals("modulo")) {
result = n1 % n2;
}
switch 代碼:
switch (cmd) {
case "add":
result = n1 + n2;
break;
case "subtract":
result = n1 - n2;
break;
case "multiply":
result = n1 * n2;
break;
case "divide":
result = n1 / n2;
break;
case "modulo":
result = n1 % n2;
break;
}
也許你會覺得多幾個else if語句對整體的代碼影響并不大,但養成良好的編碼習慣是從小事看起的。而且我們進行編程本身就要求代碼越簡潔越好,所以學會8種if else語句簡化方法,在節約我們的編碼時間的同時還能有效提高編碼的水平,同時有利于規范代碼,減少維護成本。何樂而不為呢?觀看本站的Java SE教程,教你更多優秀有效的編碼習慣和方法。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習