if (port.equals('8066') || port.equals('8436')) { List attendancePatternList = patternDao.select(new HashMap<>(0)); // do something with attendancePatternList }

这段代码可以简化为:

if (Arrays.asList('8066', '8436').contains(port)) {
    List<AttendancePattern> attendancePatternList = patternDao.select(new HashMap<>(0));
    // do something with attendancePatternList
}

这种方法使用 Arrays.asList() 创建一个包含两个端口号的列表,然后使用 contains() 方法检查 port 是否在列表中。这使得代码更简洁,更容易理解。

其他优化建议:

  • 使用常量代替硬编码的端口号。
  • 考虑使用 switch 语句,如果有多个条件需要判断。
Java 代码优化:条件判断语句的简化

原文地址: https://www.cveoy.top/t/topic/jOfu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录