当前位置:网站首页>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

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102147101364.html