《如何写好的代码》由会员分享,可在线阅读,更多相关《如何写好的代码(70页珍藏版)》请在金锄头文库上搜索。
1、如何写好的代码,数融信息技术有限公司 冯国平,什么是好的代码,问题: 什么是好的代码? 什么是坏的代码?,什么是好的代码,代码要人能够读懂-Martin Fowler 任何一个傻瓜都能写出机器能懂的代码,好的程序员应该写出人能读懂的代码。 -Martin Flowler 重构,什么是好的代码,代码是给人看的-Harold Abelson 程序必须是写给人看的,仅仅偶尔才在机器上执行。 -Harold Abelson等人,什么是好的代码,程序是人-Steve McConnell 编写程序首先为人,其次为计算机。 -Steve McConnell,什么是好的代码,写烂代码是危险的-Martin G
2、olding 编程的时候,总是想着那个维护你代码的人是一个只读你住在什么地方的、有着暴力倾向的精神病患者。 -Martin Golding,什么是好的代码,结论 好的代码有很多评价标准,但最重要的标准是 “易于理解,人能读懂!”,代码的坏味道,什么是代码的坏味道 是一个形象的比喻,由Martin Flower提出。 代码坏味道:是指在代码之中潜在问题的警示信号。 并非所有的坏味道所指示的确实是问题,但是对于绝大多数坏味道,均很有必要加以查看,并作出相应的修改。,代码的坏味道,hard code 1.读不懂 qps.add(qpbInvalid); getMulQueryParaBeans(“s
3、tatus”, “1,3”, qps, false); List organizations = orgDataService.getOrgAll(); List employees = orgDataService .getEmployeePositionAll(“000001“);/temp 2.引入bug隐患 commonService.update(user); MailAccount mailAccount = mailAccountService.getMailAccount(userId); if (mailAccount != null) user.setMailAccount
4、(mailAccount); mailManageService.connectReceive(mailAccount); setUser(user); setCurLanguage(“zh_CN“);,代码的坏味道,hard code String replayInfo =“-,代码的坏味道,hard code 3.难以扩展 if (!filepath.endsWith(“.jpg“) ,代码的坏味道,hard code 4.解决之道 .引入常量 List employees = orgDataService.getEmployeePositionAll(“000001“);/temp pr
5、ivate static final String BOSS_EMPLOYEE_CODE = “000001“; List employees = orgDataService.getEmployeePositionAll(BOSS_EMPLOYEE_CODE);/temp,代码的坏味道,hard code .引入国际化处理 String replayInfo =“-,代码的坏味道,hard code .读配置 if (!filepath.endsWith(“.jpg“) ,代码的坏味道,魔法数字 1.读不懂 for (int i = 0; i 200 | height 200) errorS
6、tr = “图片宽高不能超过200像素“; ftpUtil.deleteFile(filePath); ,代码的坏味道,魔法数字 job.setStatus(0); / 默认未发布 job.setInvalid(0); / 默认未删除 3.类型 if(linkman=1) return companyContactDao.setLinkman(companyId,id,linkman); else if(linkman=0) return companyContactDao.removeLinkman(companyId,id,linkman); if(jobDeliver.getRecomm
7、endOrDeliver().equals(1) fromTo = 1; else fromTo = 2; resumeJob.setInvite(1); resumeJob.setFromTo(2); resumeJob.setReading(0); resumeJob.setInvalid(0); resumeJob.setIsReply(0L);,代码的坏味道,魔法数字 if(checkcode = null | !code.equals(checkcode) return 0; User user = this.us.findUnique(“from User as u where u
8、.userName = ?“, username); if(user = null) return 1; /待删除 this.userId = user.getId(); if(!password.equals(user.getPassword() return 2; 4.可以允许的数字 for (int i = 0; i 0 ,代码的坏味道,魔法数字 5.解决之道 引入常量 if (width 200 | height 200) errorStr = “图片宽高不能超过200像素“; ftpUtil.deleteFile(filePath); private static final int
9、 DEFULT_IMG_SIZE = 200; if (width DEFULT_IMG_SIZE | height DEFULT_IMG_SIZE) errorStr = “图片宽高不能超过”+ DEFULT_IMG_SIZE +”像素“; ftpUtil.deleteFile(filePath); ,代码的坏味道,魔法数字 5.解决之道 使用枚举类型 if(linkman=1) return companyContactDao.setLinkman(companyId,id,linkman); else if(linkman=0) return companyContactDao.remo
10、veLinkman(companyId,id,linkman); enum OperationType modify,delete; if(linkman= OperationType.modify) return companyContactDao.setLinkman(companyId,id,linkman); else if(linkman= OperationType.delete) return companyContactDao.removeLinkman(companyId,id,linkman);,代码的坏味道,重复代码 1.完全重复 if (companyId = null
11、) Company c = new Company(); c.setUserId(userId); companyService.save(c); companyId = c.getId(); String corpIdx = companyId.toString(); int companyIdStrLength = corpIdx.length(); for (int i = 0; i 6 - companyIdStrLength; i+) corpIdx = “0“ + corpIdx; c.setCorpIdx(corpIdx); companyService.saveOrUpdate
12、(c); else Company company = companyService.get(companyId); if (company.getCorpIdx() = null) String corpIdx = companyId.toString(); int companyIdStrLength = corpIdx.length(); for (int i = 0; i 6 - companyIdStrLength; i+) corpIdx = “0“ + corpIdx; company.setCorpIdx(corpIdx); companyService.saveOrUpdat
13、e(company);,代码的坏味道,重复代码 public ModelAndView open(Long id,HttpServletRequest request, HttpServletResponse response) throws TException Resume resume = resumeService.get(id); model.addObject(“resume“, resumeService.get(id); model.addObject(“personal“, resumeService.getPersonal(id); model.addObject(“job
14、Target“,jobTargetMap); model.addObject(“languages“, resumeService.getLanguages(id); model.addObject(“languageAchieve“, resumeService.getLanguageAchieve(id); model.addObject(“additional“, resumeService.getAdditionalInfo(id); model.addObject(“selfRecommends“, resumeService.getSelfRecommends(id); model
15、.addObject(“educationExperinces“, educationService.findbyResumeId(id); model.addObject(“jobExperiences“, jobExperiencesList); model.addObject(“projectExperiences“, projectService.findbyResumeId(id); model.addObject(“skills“, resumeService.getSkills(id); model.addObject(“trainningExperiences“, trainn
16、ingService.findbyResumeId(id); model.addObject(“certificates“, resumeService.getCertificates(id); model.addObject(“others“, resumeService.getOthers(id); model.addObject(“attachments“, resumeService.getAttachments(id); model.addObject(“privateSpace“, resumeService.getPrivateSpace(id); model.addObject(“privateAttachments“, resumeService.getPriv