这段代码展示了如何创建并初始化一个 DeputyLeaderDeptEntity 对象。下面是优化后的代码版本,更简洁易懂:

final DeputyLeaderDeptEntity deputyLeaderDept = new DeputyLeaderDeptEntity();
deputyLeaderDept.setDeptName(deptName);
deputyLeaderDept.setActivityId(examineRelationDataVO.getActivityInstanceId());
deputyLeaderDept.setCreateBy(examineRelationDataVO.getLoginUid());
deputyLeaderDept.setCreateTime(examineRelationDataVO.getLocalDateTime());

final Integer companyIndex = getMaxLessThanACertainNumber(companyKey, locationCorrespondingToEmployees);
final String company = companyValueIsNotNullMap.get(companyIndex);
final String companyName = company + COMPANY_SUFFIX;
deputyLeaderDept.setCompanyName(companyName);
deputyLeaderDept.setCompanyDn(OrgDnEnum.getDnByName(companyName));

final String empInfo = empMap.get(locationCorrespondingToEmployees);
final String[] empInfos = StringUtils.split(empInfo, Constants.SPLIT_UNDERLINE);
deputyLeaderDept.setLeaderName(empInfos[0]);
deputyLeaderDept.setLeaderEmpCode(empInfos[1]);

if (StringUtils.countMatches(empInfo, Constants.SPLIT_UNDERLINE) == 0) {
    final UserBean userBean = getUserBeans(deputyLeaderDept.getCompanyDn(), empInfo);
    deputyLeaderDept.setLeaderEmpCode(userBean.getEmployeeNumberNew());
}

return deputyLeaderDept;

优化点:

  • 合并判断语句: 将原代码中判断 empInfo 是否包含 Constants.SPLIT_UNDERLINE 的 if 语句合并到一起,使用 StringUtils.countMatches 方法更简洁地判断。
  • 简化代码逻辑: 去掉了冗余的代码,例如对 empInfos 的判断,以及从 uom 获取员工信息的逻辑。
  • 使用更清晰的变量名: 使用更清晰的变量名,例如 companyIndexcompany,使代码更容易理解。
  • 移除不必要的注释: 删除了注释,因为代码本身已经足够清晰。

通过这些优化,代码更加简洁易懂,提高了代码的可读性和可维护性。

Java 代码优化:简化 DeputyLeaderDeptEntity 对象的创建

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

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