当前位置:网站首页>优化代码 —— 减少 if - else
优化代码 —— 减少 if - else
2022-08-09 14:28:00 【何以解忧,唯有..】
一、前言
代码中如果 if-else 比较多,会降低代码的可读性。维护起来也比较麻烦,因此在代码中尽量减少 if-else 的出现频率,或者使用一些常规的手段来替代,增强代码的可读性。
二、优化方案1 —— 提前 return,去除不必要的 else 代码块
如果if-else代码块包含return语句,可以考虑通过提前return,把多余else干掉,使代码更加优雅。
优化前:
if(condition) {
//doSomething
} else {
return;
}
优化后:
if(!condition){
return;
}
//doSomething
三、优化方案2 —— 使用三目运算符
使用条件三目运算符可以简化某些if-else,使代码更加简洁,更具有可读性。
优化前:
int price;
if(condition) {
price = 80;
} else {
price = 100;
}
优化后:
int price = condition ? 80 : 100;
四、优化方案3 —— 使用枚举
在某些时候,使用枚举也可以优化if-else逻辑分支。
优化前:
String orderStatusDesc = "";
if (orderStatus == 0) {
orderStatusDesc = "订单未支付";
} else if (orderStatus == 1) {
orderStatusDesc = "订单已支付";
} else if(orderStatus == 2) {
orderStatusDesc = "已发货";
}
优化前:
// 枚举类
public enum OrderStatusEnum {
UN_PAID(0, "订单未支付"),
PAIDED(1, "已支付"),
SENDED(2, "已发货");
private int status;
private String desc;
OrderStatusEnum(int status, String desc) {
this.status = status;
this.desc = desc;
}
public int getStatus() {
return status;
}
public String getDesc() {
return desc;
}
OrderStatusEnum of(int orderStatus) {
for(OrderStatusEnum temp : OrderStatusEnum.values()) {
if (temp.getStatus() == orderStatus) {
return temp;
}
}
return null;
}
}
有了枚举之后,以上if-else逻辑分支,可以优化为一行代码
String orderStatusDesc = OrderStatusEnum.of(orderStatus).getDesc();
五、优化方案4 —— 优化逻辑结构,正常的业务逻辑走主干代码
将条件反转使异常情况先退出,让正常流程维持在主干流程,可以让代码结构更加清晰。
优化前:
public int test01(int income) {
double rate = 0.13;
if (income <= 0) {
return 0;
}
if (income > 0 && income <=2000) {
return (int) (income * rate);
}
return 0;
}
优化后:
public int test02(int income) {
double rate = 0.13;
if (income <= 0) {
return 0;
}
if (income > 2000) {
return income;
}
return (int) (income * rate);
}
六、优化方案5 —— 策略模式 + 工厂方法替代 if else
边栏推荐
猜你喜欢

RHCE Course Summary
![[Serilog] Simple .NET logging with fully structured events](/img/10/3a1f58946129246e42e2f7b3ca3f9b.png)
[Serilog] Simple .NET logging with fully structured events

What is the cost of small program development and production?Three development methods cost analysis!

拒绝“重复造轮子”,百度EasyDL让你玩转AI定制开发

*3-1 CCF 2014-09-1 Adjacent pairs

simulink仿真pid控制伺服系统

【DevOps】jekins部署(一)

C语言程序设计笔记(浙大翁恺版) 第七章:函数

RHCE Course Summary

30分钟使用百度EasyDL实现健康码/行程码智能识别
随机推荐
猴蝎美人
spacedesk-notebook, tablet, extended screen-solve the problem that the tablet font is too small
Minesweeper game
凡尔赛一波:凭这份阿里面试突击指南,我已经拿了9张Offer
浏览器tab页签上的title图标favicon.icon
同事的接口文档我每次看着就头大,毛病是真的多多多。。。
Recursive implementation of the Tower of Hanoi problem
The title icon favicon.icon on the browser tab
IK学习笔记(2)——TwoBones IK
【DevOps】jekinsBuild step 'Execute shell' marked build as failure
【Message Center】Architecture Preparation
How does the JVM judge that an object is useless
shell提取ip地址
RHCE Course Summary
Use Baidu EasyDL to realize intelligent identification of health code/travel code in 30 minutes
【DevOps】jekins部署(一)
C语言程序设计笔记(浙大翁恺版) 第七章:函数
【微服务】nacos注册中心和配置中心详解
不要小看一个Redis!从头到尾全是精华,阿里Redis速成笔记太香了
Meta 发布 1750 亿聊天机器人,亿万富翁老板小扎被「他」疯狂吐槽!