我现在被一些非常奇怪的课程所困扰,这些课程的逻辑混淆了。以下是生成数据库查询的代码示例:
if(realTraffic.getPvkp() != null) {
//Admission point
if(BeanUtils.isGuidEntity(realTraffic.getPvkp())) {
findParameters +=
" and (" + staticTableName() + ".guidPvkp = '" + realTraffic.getPvkp().getGuid()
+ "' or (" + staticTableName() + ".guidPvkpOut = '" + realTraffic.getPvkp().getGuid()
+ "' and " + staticTableName() + ".requestType = " + RequestBean.TRANSIT_TYPE
+ ")";
if (companyType == CompanyBean.PP_TYPE && !realTraffic.isSkipOther()) {
// TODO - add non-formed
findParameters += " or (" + staticTableName() + ".guidPvkpOut is null "
+ " and " + staticTableName() + ".requestType = " + RequestBean.TRANSIT_TYPE
+ ")";
}
findParameters += ") ";
} else {
// Territorial department
if(BeanUtils.isGuidEntity(realTraffic.getPvkp().getTerritorialDepartment())) {
findParameters +=
" and (Pvkp.guidTerritorialDepartment = '" + realTraffic.getPvkp().getTerritorialDepartment().getGuid()
+ "' or Pvkp.guidFtsDepartment = '" + realTraffic.getPvkp().getTerritorialDepartment().getGuid()
+ "' ) ";
}
}
}
这只是我在方法中进行的大量复杂检查的一部分。问题是 - 如何处理这样的代码 - 它有很多嵌套的 if 和检查。为了使这段代码更简单、更优雅,有哪些常用方法?
UPD:我知道在编写新项目时如何避免此类代码,但是如何处理现有的遗留代码?