web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

if else代码优化

373 ℃
     

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;
    }
}

uniapp开发axios接口调用解决跨域问题(只需4步)

javascript利用pinyin库把汉字转拼音(不带声调)

javascript语法中不等于null和空字符串的3种判断方式

html网页上如何把文件压缩成zip代码示列(前端HTML/JavaScript和后端Node.js + Express)

javascript语法读取文本文件代码(PHP代码读取文本文件)

标签: 代码优化

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

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

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

当前位置: 网站首页 > js
本文共计2744个字,预计阅读时长19分钟
生活小工具,收录了80多款小工具
上一篇: 抖音联合方正打造免费可商用品牌字体——抖音美好体
下一篇: 推荐一个在线图文转视频、AI 数字人工具——一帧秒创(免费赠送100分钟)
x 打工人ai神器