当前位置:网站首页>优化代码去除if-else
优化代码去除if-else
2022-06-10 21:48:00 【Li_XiaoJin】
临走之前还在做需求,最近在做余量查询,还有一卡多号遗留的一些问题。发现以前的一些代码全是if-else,借着这次机会进行优化掉,记录一下。 所以代码review很重要啊,避免“臭味”代码。 相关代码已脱敏处理。。。
1、提前 return,去除不必要的 else
如果 if-else 代码块包含 return 语句,可以考虑通过提前 return,把多余 else 干掉,使代码更加优雅。
修改前:
if(condition){
//doSomething
}else{
return ;
}
修改后:
if(!condition){
return ;
}
//doSomething
2、使用条件三目运算符
使用条件三目运算符可以简化某些 if-else,使代码更加简洁,更具有可读性。
优化前:
if ("1".equals(resourceType)) {
resourceType = "a";
} else if ("2".equals("resourceType")) {
resourceType = "b";
} else {
resourceType = "c";
}
优化后:
resourceType = "1".equals(resourceType) ? "a" : "2".equals(resourceType) ? "b" : "c";
3、使用枚举
优化前:
if ("A".equals(resourceType)) {
map.put("priorty", 99 - i);
} else if ("B".equals(resourceType)) {
map.put("priorty", 89 - i);
} else if ("C".equals(resourceType)) {
map.put("priorty", 79 - i);
} else {
map.put("priorty", i);
}
优化后:
引入枚举类:
public enum PriortyEnum {
A(99, "A"),
B(89, "B"),
C(79, "C");
private int piorty;
private String resourceType;
PriortyEnum(int piorty, String resourceType) {
this.piorty = piorty;
this.resourceType = resourceType;
}
public static int getpiorty(String resourceType) {
for (PriortyEnum priortyEnum : PriortyEnum.values()) {
if (priortyEnum.getResourceType().equals(resourceType)) {
return priortyEnum.getPiorty();
}
}
return 10;
}
public int getPiorty() {
return piorty;
}
public void setPiorty(int piorty) {
this.piorty = piorty;
}
public String getResourceType() {
return resourceType;
}
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
}
引入枚举类后,可以优化为一行代码。
调用:
map.put("priorty", PriortyEnum.getpiorty(resourceType) - i++);
4、使用 Optional
// TO DO
5、使用策略+工厂模式
//TO DO
剩余两个以后遇到加上......
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/优化代码去除if-else
边栏推荐
- 存储引擎分析
- 盲盒商城APP系统开发的软件特点和盲盒功能介绍
- js敏感信息泄露检测工具
- Html+php+mysql login registration page
- Reading notes on the way of Huawei data
- 2022 t elevator repair test question simulation test question bank and online simulation test
- IPO can't cure Weima's complications?
- Swin_ Interpretation of transformer source code
- Implementation of simply untyped lambda calculus
- [applet] vant sliding cell adds the function of clicking other positions to close automatically
猜你喜欢

Swin_ Interpretation of transformer source code

IP anti query domain name

Reflow and repaint

Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association

Multus CNI deployment and basic use of kubernetes multi network card scheme
![[original] analysis of nine price HPV data capture of Yilu app](/img/f2/c792367beea0956fe69aad5d35581a.png)
[original] analysis of nine price HPV data capture of Yilu app
![Authoritative guide to Web3 technology stack [2022]](/img/76/0f64604f5e5355300f5ec498ea23e1.png)
Authoritative guide to Web3 technology stack [2022]

Interpretation of dataset class of mmdetection

Niuke: expression evaluation

Déploiement et utilisation de base de la carte multi - réseau kubernets
随机推荐
[raise bar C #] how to call the base of the interface
图像拼接摄像头拼接笔记
Web3 ecological decentralized financial platform sealem Finance
分布式基础
中银证券开户有什么风险吗?安全的吗?
1.Tornado简介&&本专栏搭建tornado项目简介
原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布
2022g1 industrial boiler stoker test questions and online simulation test
Storage engine analysis
Display of successful cases of target customer matching data table
Blue Bridge Cup_ Pick substring_ Combinatorial mathematics_ Multiplication principle/ Ruler method
Digital twin: third person mouse operation
Play electronics, poor three generations
smb匿名
Déploiement et utilisation de base de la carte multi - réseau kubernets
Keras深度学习实战(8)——使用数据增强提高神经网络性能
kubernetes 二进制安装(v1.20.15)(六)部署WorkNode节点
Whale conference sharing: what should we do if the conference is difficult?
Simulated 100 questions and simulated examination of G2 utility boiler stoker examination in 2022
SMB anonyme