资源预览内容
第1页 / 共90页
第2页 / 共90页
第3页 / 共90页
第4页 / 共90页
第5页 / 共90页
第6页 / 共90页
第7页 / 共90页
第8页 / 共90页
第9页 / 共90页
第10页 / 共90页
亲,该文档总共90页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
,目录,IF/SWITCH语句 复杂的、嵌套的IF/SWITCH语句,循环语句. 多层嵌套的循环语句,经常跟IF语句一起使用,A,B,圈复杂度 从理论上分析这两种坏味道,C,解决之道. 如何解决?值得探讨的课题,D,2,代码的坏味道带来的问题,破窗效应,代码的坏味道带来的问题,破窗效应,代码的坏味道带来的问题,破窗效应,代码的坏味道带来的问题,破窗效应,代码的坏味道带来的问题,冰山一角,代码的坏味道带来的问题,软件成本 Costtotal = costdevelop + costmaintain Costmaintain = Costunderstand + costchange + costtest+costdeploy Costmaintain costdevelop,代码的坏味道之IF语句,1.判断集合不为空,if (employeePositionList != null ,2.判断对象不为空,if (empSalarySet.getEffictiveDate()!= null if (empSalarySet.getInvalDate()!=null & empSalarySet.getInvalDate()!=“) .,代码的坏味道之IF语句,3.判断不为空 byte bufKey = RedisCacheInterceptor. stringSerializer.serialize(session.getId(); byte bufValue = connection.get(bufKey); if (bufValue != null) try return RedisCacheInterceptor. javaSerializer.deserializeInto(bufValue, session); catch (IOException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace(); return null;,代码的坏味道之IF语句,3.判断不为空 if (orgId != null) List employeePositionList =orgDataService .getEmployeePositionByOrgId(getTenantId(), orgId); Map empMap = new HashMap(); List empList = new ArrayList(); if (employeePositionList != null ,代码的坏味道之IF语句,4.判断为空 public ResponseBody SalarySetPage getList(Integer currentPageNo, Integer pageSize, Long orgId) logger.info(“EmpTreatmentController.getList()“); if(currentPageNo = null | currentPageNo = 0) currentPageNo = 1; if(pageSize = null) pageSize = 2; SalarySetPage pager = empSalarySetService.getSalarySetPageByOrgIdx(getTenantId(), currentPageNo, pageSize, orgId); return pager; ,代码的坏味道之IF语句,5.读不懂 public String getDetaliOrgRoute(Long orgIdx, String orgName, String prefix) Organization org = orgDataService.getOrgById(getTenantId(), orgIdx); if(org.getUplink()!=0) Organization fatherOrg = orgDataService.getOrgById(getTenantId(),org.getUplink(); orgName = fatherOrg.getOrgName() + prefix + orgName; return getDetaliOrgRoute(fatherOrg.getIdx(), orgName, prefix); else return orgName; ,代码的坏味道之IF语句,5.难以理解 List list = roleJobService.getOrgRoleTypeByCode(tenantId,cod); if (list = null | list.size() = 0) return true; else for (OrgRoleType rgRoleType : list) if(rgRoleType.getIdx()=main.getIdx() return true; return false; ,代码的坏味道之IF语句,6.多此一举,if (list = null | list.size() = 0) return true; else return false; ,7.可以简化,if(size0) mail.setIsAttached(1); else mail.setIsAttached(0);.,代码的坏味道之IF语句,8.永远不会执行的条件 if (lis != null ,代码的坏味道之IF语句,9.错误的思路 public void uploadPhoto(RequestParam(“file“) CommonsMultipartFile file,Long id, HttpServletResponse response) if (file != null) String errorStr = null; FTPUtil ftpUtil = new FTPUtil(); try if (!fileName.toLowerCase().endsWith(“.jpg“) .,代码的坏味道之IF语句,10.IF语句带来整个逻辑的复杂 public List getEntryOrgAndCount(Long empIdx) StringBuffer sql = new StringBuffer(“ select edt.deptIdx, count(*) as deptCount from eb_deploy_task edt where 1=1 “); if(empIdx != null) sql.append(“ and edt.empIdx = “ + empIdx); sql.append(“ and edt.status = 10 “); sql.append(“ group by edt.deptIdx “); List list = deployTaskDao.findBySQL(sql.toString(); return list; else return null; ,代码的坏味道之IF语句,11.重复的IF语句 if (sex != 0) query.append(“ AND sex:“ + sex ); if (resumeFrom != 0) query.append(“ AND RESUME_FROM:“ + resumeFrom ); if(residencyType != 0) query.append(“ AND RESIDENCY_TYPE:“ + residencyType ); if(contractStatus != 0) query.append(“ AND CONTRACT_STATUS:“ + contractStatus ); if(contractType !=0) query.append(“ AND CONTRACT_TYPE:“ + contractType ); if(pop != 0 ) query.append(“ AND pop:“ + pop ); if(religion != 0) query.append(“ AND RELIGION:“ + religion ); if(race != 0) query.append(“ AND RACE:“ + race ); if(nationality != 0) query.append(“ AND NATIONALITY:“ + nationality ); if(ethnic != 0) query.append(“ AND ETHNIC:“ + ethnic );,代码的坏味道之IF语句,12.IF语句的扩展性问题 public ResponseBody Pager findJobComments(Long companyId,Long jobId,Integer type,Integer pageNo,Integer w) long userId = getUser().getId(); if (pageNo = null) pageNo = 0; Pager pager = null; if(type =0) pager = jobCommentSerice.findMyJobsComments(userId, jobId, type, pageNo,w); return pager; else if(type =1) pager = companyCommentService.findMyCompanyComments(userId, companyId, type,pageNo,w); return pager; return null; ,代码的坏味道之FOR循环,1.不该存在的FOR循环,排序 /* * 按empcode排序方法 * param group * return */ public static List sortByEmpcode( List group) List groupEmp = new ArrayList(); EmployeePosition postion; for (int m = 0; m group.size(); m+) boolean flag = true; postion = group.get(m); for (int n = 0; n groupEmp.size(); n+) if (!compareToEmpcode(postion.getEmp().getEmpCode(), groupEmp .get(n).getEmp().getEmpCode() groupEmp.add(n, postion); flag = false; break; if (flag) groupEmp.add(postion); return groupEmp; ,代码的坏味道之FOR循环,1.不该存在的FOR循环,排序问题 if(jobMains != null ,代码的坏味道之FOR循环,2.多重循环 for (int k = 0; k orgJobMains = orgDataService.getOrgJobMainByOrgId(tenantId, orgs.get(k).idx); if(st
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号