是的,这段代码有一些优化空间。

首先,可以将'final List roles = Lists.neWArrayList()'这一行的代码简化为'final List roles = new ArrayList<>()'。

然后,可以将'if (BooleanUtis.isTrue(existAppraise))'和'if (BooleanUtils.isTrue(existExaminer))'这两个判断条件合并为一个条件判断。

最后,可以将'roles.add(UserTypeEnum.APPRAISEE.getValue())'和'roles.add(UserTypeEnum.EXAMINER.getValue())'这两行代码提取出来,避免重复。

下面是优化后的代码:

final List<Integer> roles = new ArrayList<>();

if (BooleanUtis.isTrue(activityAppraiseeService.existByEmpCode(empCode))) {
    roles.add(UserTypeEnum.APPRAISEE.getValue());
}

if (BooleanUtils.isTrue(activityExaminerService.existByEmpCode(empCode))) {
    roles.add(UserTypeEnum.EXAMINER.getValue());
}

return roles;
Java 代码优化:减少冗余,提高效率

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

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