当前位置:网站首页>优化代码去除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
边栏推荐
- Auto.js pro 开发环境配置
- Watlow signs agreement to acquire EUROTHERM from Schneider Electric
- QT custom delegation
- Typescript - declaration files and built-in objects
- 数组、List、Set、Map、Properties依赖注入格式
- Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事
- [tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction
- How small and micro enterprises build micro official websites at low cost
- Redis from entry to entry
- 玩电子,穷三代
猜你喜欢

Several reasons and solutions of virtual machine Ping failure

How to run Plink software -- three methods

Web3生态去中心化金融平台——Sealem Finance

简单阻抗匹配电路及公式

dc_ Study and summary of labs--lab1

Vscode common shortcuts

Multus CNI deployment and basic use of kubernetes multi network card scheme

Keras deep learning practice (8) -- using data enhancement to improve neural network performance

Swin_ Interpretation of transformer source code

【原创】医鹿APP九价HPV数据抓包分析
随机推荐
MySQL master-slave replication solves read-write separation
Display of successful cases of target customer matching data table
Watlow signs agreement to acquire EUROTHERM from Schneider Electric
[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction
How about opening an account with flush? Is it safe to open an account?
QT custom delegation
C语言内功修炼【整型在内存中的存储】
Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association
重排 (reflow) 与重绘 (repaint)
Template_ Calculate number of combinations
[tcapulusdb knowledge base] tcapulusdb shard relocation introduction
Multus CNI deployment and basic use of kubernetes multi network card scheme
dc_labs--lab1的学习与总结
Question bank and simulation test of 2022 tea artist (intermediate) operation certificate examination
JS sensitive information leak detection tool
C language internal skill cultivation [integer stored in memory]
Blue Bridge Cup_ Pick substring_ Combinatorial mathematics_ Multiplication principle/ Ruler method
SMB anonymous
Opencv_100问_第四章 (16-20)
C automatically generates Oracle table creation statement according to excel