当前位置:网站首页>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
边栏推荐
- Array, list, set, map, properties dependency injection format
- 数据与信息资源共享平台(五)
- Assembly: assembly mixed with c-faction language and corresponding lldb common instructions
- The definition of the metauniverse and the seven infinite features
- 数据与信息资源共享平台(四)
- 06151020 mysterious code, waiting for you to decipher
- [raise bar C #] how to call the base of the interface
- Online questionnaire system based on php+web+mysql
- Watlow signs agreement to acquire EUROTHERM from Schneider Electric
- 【006】初识字符串
猜你喜欢

IP anti query domain name

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

vulnhub之dc4

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

200 c language words, please collect!

Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事

Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base

Icml2022 | revoir la traduction vocale de bout en bout du texte à partir de zéro

Keras深度学习实战(8)——使用数据增强提高神经网络性能

PwnTheBox,Pwn:tutorial1
随机推荐
Auto.js pro 开发环境配置
【sql语句基础】——增(insert)
[original] analysis of nine price HPV data capture of Yilu app
Question bank and simulation test of 2022 tea artist (intermediate) operation certificate examination
ICML2022 | 從零開始重新審視端到端的語音到文本翻譯
Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
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
但身示你五县非那最土zaiFKMW
smb匿名
2022g1 industrial boiler stoker test questions and online simulation test
Creation of thread pool
"Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry
Laravel8 enables alicloud file upload
Openjudge noi 1.13 15: finding modes in sequences
How to run Plink software -- three methods
IPO can't cure Weima's complications?
DC4 of vulnhub
laravel8 实现阿里云文件上传
js敏感信息泄露检测工具
MySQL相关-0416