当前位置:网站首页>Optimize code to remove if else
Optimize code to remove if else
2022-06-10 23:10:00 【Li_ XiaoJin】
Before leaving, I was still making demands , Recently doing stock query , There are also some problems left over by the one card number . I found that some of the previous codes were all if-else, Take this opportunity to optimize , Make a note of . So the code review It's very important , avoid “ Stink ” Code . Relevant codes have been desensitized ...
1、 advance return, Remove unnecessary else
If if-else The code block contains return sentence , Think ahead of time return, Put the superfluous else kill , Make the code more elegant .
Before the change :
if(condition){
//doSomething
}else{
return ;
}
After modification :
if(!condition){
return ;
}
//doSomething
2、 Use conditional binomial operators
Using conditional trinomial operators can simplify some if-else, Make the code more concise , More readable .
Before optimization :
if ("1".equals(resourceType)) {
resourceType = "a";
} else if ("2".equals("resourceType")) {
resourceType = "b";
} else {
resourceType = "c";
}
After optimization :
resourceType = "1".equals(resourceType) ? "a" : "2".equals(resourceType) ? "b" : "c";
3、 Use enumeration
Before optimization :
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);
}
After optimization :
Introduce enumeration class :
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;
}
}
After introducing enumeration classes , Can be optimized to a line of code .
call :
map.put("priorty", PriortyEnum.getpiorty(resourceType) - i++);
4、 Use Optional
// TO DO
5、 Use policy + Factory mode
//TO DO
The remaining two will be met later, plus ......
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/ Optimize code removal if-else
边栏推荐
- 原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布
- SMB anonyme
- 玩电子,穷三代
- Bottom layer communication protocol ① of single chip microcomputer -- synchronous and asynchronous, parallel and serial, full duplex and half duplex, simplex, level signal and differential signal
- 【原创】医鹿APP九价HPV数据抓包分析
- Multus CNI deployment and basic use of kubernetes multi network card scheme
- UE4骨骼动画新手入门
- 视频融合云服务EasyCVR集群录像事件查询无效是什么原因?
- MySQL组合索引不被命中使用的情况
- [original] analysis of nine price HPV data capture of Yilu app
猜你喜欢

软件测试入门之软件测试的概念与过程(精辟内容)

DC4 of vulnhub

ICML2022 | 從零開始重新審視端到端的語音到文本翻譯

Online questionnaire system based on php+web+mysql

Auto. JS Pro development environment configuration

数据与信息资源共享平台(六)

【006】初识字符串

C automatically generates Oracle table creation statement according to excel

DC2 of vulnhub
![[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
随机推荐
Assembly: assembly mixed with c-faction language and corresponding lldb common instructions
数据与信息资源共享平台(九)
爬虫学习知识
2022最新软件测试面试题常问的30道【持续更新~】
Is it safe to open a BOC securities account? Is the risk high?
项目实训13——界面补充
Array, list, set, map, properties dependency injection format
数据与信息资源共享平台(六)
How Photoshop opens, edits and exports pictures in webp format
Informatics Olympiad all in one 1280: [example 9.24] skiing | openjudge noi 2.690: skiing | Luogu p1434 [show2002] skiing
DC4 of vulnhub
【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association
【接口教程】EasyCVR如何通过接口设置平台级联?
[gmm+kde] target tracking simulation based on GMM and KDE kernel estimation of MATLAB
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
How about opening an account with flush? Is it safe to open an account?
中银证券开户有什么风险吗?安全的吗?
06151020 mysterious code, waiting for you to decipher
SMB anonyme