if else代码优化

628 ℃

1、使用return

优化前:

if ("java".equals(str)) {
    // 业务代码......
} else {
    return;
}

优化后:

if (!"java".equals(str)) {
    return;
}
// 业务代码......

2、使用Map

优化前:

if (t == 1) {
    type = "name";
} else if (t == 2) {
    type = "id";
} else if (t == 3) {
    type = "mobile";
}

我们先定义一个 Map 数组,把相关判断信息存储起来:

Map typeMap = new HashMap<>();
typeMap.put(1, "name");
typeMap.put(2, "id");
typeMap.put(3, "mobile");

之前的判断语句可以使用以下一行代码代替了:

type = typeMap.get(t);

3、使用三元运算符

优化前:

Integer score = 81;
if (score > 80) {
    score = 100;
} else {
    score = 60;
}

优化后:

score = score > 80 ? 100 : 60;

4、合并条件表达式

优化前:

String city = "广州";
String area = "020";
String province = "广东";
if ("广州".equals(city)) {
    return "guang'zhou";
}
if ("020".equals(area)) {
    return "guang'zhou";
}
if ("广东".equals(province)){
    return "guang'zhou";
}

优化后:

if ("广州".equals(city) || "020".equals(area) || "广东".equals(province)){
    return "guang'zhou";
}

5、使用枚举

优化前:

Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
    typeId = 1;
} else if ("Age".equals(type)) {
    typeId = 2;
} else if ("Address".equals(type)) {
    typeId = 3;
}

优化时,我们先来定义一个枚举:

public enum TypeEnum {
    Name(1), Age(2), Address(3);
    public Integer typeId;
    TypeEnum(Integer typeId) {
        this.typeId = typeId;
    }
}

之前的 if else 判断就可以被如下一行代码所替代了:

typeId = TypeEnum.valueOf("Name").typeId;

6、使用 Optional

优化前:

String str = "java";
if (str == null) {
    System.out.println("Null");
} else {
    System.out.println(str);
}

优化后:

Optional opt = Optional.of("java");
opt.ifPresentOrElse(v -> 
    System.out.println(v), () -> System.out.println("Null"));
小贴士:注意运行版本,必须是 JDK 9+ 才行。

7、 梳理优化判断逻辑

优化前:

// 年龄大于 18
if (age > 18) {
    // 工资大于 5000
    if (salary > 5000) {
        // 是否漂亮
        if (pretty == true) {
            return true;
        }
    }
}
return false;

优化后:

if (age < 18) {
    return false;
}
if (salary < 5000) {
    return false;
}
return pretty; 

8、使用多态

优化前:

Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
    typeId = 1;
} else if ("Age".equals(type)) {
    typeId = 2;
} else if ("Address".equals(type)) {
    typeId = 3;
}

使用多态,我们先定义一个接口,在接口中声明一个公共返回 typeId 的方法,在添加三个子类分别实现这三个子类,实现代码如下:

public interface IType {
    public Integer getType();
}
 
public class Name implements IType {
    @Override
    public Integer getType() {
        return 1;
    }
}
 
public class Age implements IType {
    @Override
    public Integer getType() {
        return 2;
    }
}
 
public class Address implements IType {
    @Override
    public Integer getType() {
        return 3;
    }
}

注意:为了简便我们这里把类和接口放到了一个代码块中,在实际开发中应该分别创建一个接口和三个类分别存储。

此时,我们之前的 if else 判断就可以改为如下代码:

IType itype = (IType) Class.forName("com.example." + type).newInstance();
Integer typeId = itype.getType();

if else 判断代码:

if ("add".equals(cmd)) {
    result = n1 + n2;
} else if ("subtract".equals(cmd)) {
    result = n1 - n2;
} else if ("multiply".equals(cmd)) {
    result = n1 * n2;
} else if ("divide".equals(cmd)) {
    result = n1 / n2;
} else if ("modulo".equals(cmd)) {
    result = n1 % n2;
}

switch 代码:

switch (cmd) {
    case "add":
        result = n1 + n2;
        break;
    case "subtract":
        result = n1 - n2;
        break;
    case "multiply":
        result = n1 * n2;
        break;
    case "divide":
        result = n1 / n2;
        break;
    case "modulo":
        result = n1 % n2;
        break;
}
// java 14
switch (cmd) {
    case "add" -> {
        result = n1 + n2;
    }
    case "subtract" -> {
        result = n1 - n2;
    }
    case "multiply" -> {
        result = n1 * n2;
    }
    case "divide" -> {
        result = n1 / n2;
    }
    case "modulo" -> {
        result = n1 % n2;
    }
}

js用最简单的方法实现2个数组的交叉一对一合并

如何利用js语法将2个数组进行交叉合并

利用js语法把数组根据相同key转换成嵌套数组格式

javascript语法如何把json文件输出到html页面上

js点击按钮从服务器上下载json文件(不是打开文件)

标签: 代码优化

上面是“if else代码优化”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_2107.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

C#语法怎么实现图片马赛克效果?
织梦dede数据库内容替换【安全确认码不正确!】解
nodejs简简单单做一个文件夹上传功能
lodop插件如何把url地址内容打印输出
css3画弹珠,可以滚动!