欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

    如何写好的代码.ppt

    • 资源ID:5474832       资源大小:962.55KB        全文页数:70页
    • 资源格式: PPT        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    如何写好的代码.ppt

    如何写好的代码,数融信息技术有限公司冯国平,什么是好的代码,问题:什么是好的代码?什么是坏的代码?,什么是好的代码,代码要人能够读懂-Martin Fowler任何一个傻瓜都能写出机器能懂的代码,好的程序员应该写出人能读懂的代码。-Martin Flowler 重构,什么是好的代码,代码是给人看的-Harold Abelson程序必须是写给人看的,仅仅偶尔才在机器上执行。-Harold Abelson等人,什么是好的代码,程序是人-Steve McConnell编写程序首先为人,其次为计算机。-Steve McConnell,什么是好的代码,写烂代码是危险的-Martin Golding编程的时候,总是想着那个维护你代码的人是一个只读你住在什么地方的、有着暴力倾向的精神病患者。-Martin Golding,什么是好的代码,结论好的代码有很多评价标准,但最重要的标准是“易于理解,人能读懂!”,代码的坏味道,什么是代码的坏味道是一个形象的比喻,由Martin Flower提出。代码坏味道:是指在代码之中潜在问题的警示信号。并非所有的坏味道所指示的确实是问题,但是对于绝大多数坏味道,均很有必要加以查看,并作出相应的修改。,代码的坏味道,hard code1.读不懂qps.add(qpbInvalid);getMulQueryParaBeans(“status”,“1,3”,qps,false);List organizations=orgDataService.getOrgAll();List employees=orgDataService.getEmployeePositionAll(000001);/temp2.引入bug隐患commonService.update(user);MailAccount mailAccount=mailAccountService.getMailAccount(userId);if(mailAccount!=null)user.setMailAccount(mailAccount);mailManageService.connectReceive(mailAccount);setUser(user);setCurLanguage(zh_CN);,代码的坏味道,hard codeString replayInfo=-,代码的坏味道,hard code3.难以扩展if(!filepath.endsWith(.jpg),代码的坏味道,hard code4.解决之道.引入常量 List employees=orgDataService.getEmployeePositionAll(000001);/tempprivate 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)errorStr=图片宽高不能超过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.getRecommendOrDeliver().equals(1)fromTo=1;elsefromTo=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.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 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.removeLinkman(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)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(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.saveOrUpdate(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(jobTarget,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.addObject(educationExperinces,educationService.findbyResumeId(id);model.addObject(jobExperiences,jobExperiencesList);model.addObject(projectExperiences,projectService.findbyResumeId(id);model.addObject(skills,resumeService.getSkills(id);model.addObject(trainningExperiences,trainningService.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.getPrivateAttachments(id);,代码的坏味道,重复代码 model.addObject(printColumns,resumeService.getPrintColumns(id);model.addObject(basicData,basicDataService.getStaticDataByCodeTypeAndLanguage(resumeService.get(id).getLanguage(),RESUME_BASICDATA_CODE);model.addObject(curLanguage,resumeService.get(id).getLanguage();return model;public ModelAndView inResumeGraduate(Long id)ModelAndView model=new ModelAndView(/resume/resumeGraduate);model.addObject(resume,resumeService.get(id);model.addObject(personal,resumeService.getPersonal(id);model.addObject(jobTarget,jobTargetMap);model.addObject(languages,resumeService.getLanguages(id);model.addObject(languageAchieve,resumeService.getLanguageAchieve(id);model.addObject(selfRecommends,resumeService.getSelfRecommends(id);model.addObject(educationExperinces,educationService.findbyResumeId(id);model.addObject(skills,resumeService.getSkills(id);model.addObject(trainningExperiences,trainningService.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.getPrivateAttachments(id);model.addObject(printColumns,resumeService.getPrintColumns(id);model.addObject(rewards,resumeService.getRewards(id);model.addObject(dutys,resumeService.getDutys(id);,代码的坏味道,重复代码 model.addObject(practices,resumeService.getPractices(id);model.addObject(basicData,basicDataService.getStaticDataByCodeTypeAndLanguage(resumeService.get(id).getLanguage(),RESUME_BASICDATA_CODE);model.addObject(curLanguage,resumeService.get(id).getLanguage();return model;2.功能重复JobCommentController.java/*上传简历评论附件*param request*return*/RequestMapping(value=/uploadJobComment,method=RequestMethod.POST)public void uploadComment(RequestParam(file)CommonsMultipartFile file,HttpServletResponse response)if(file!=null)FTPUtil ftpUtil=new FTPUtil();try ftpUtil.connectServer();String fileName=file.getFileItem().getName();String filePath=FTP_JOB_COMMENT_DIR+UUID.randomUUID().toString()+.+FilenameUtils.getExtension(fileName);Long fileSize=file.getFileItem().getSize();ftpUtil.upload(file.getInputStream(),filePath);response.setContentType(text/html;charset=UTF-8);PrintWriter writer=response.getWriter();,代码的坏味道,重复代码writer.print(fileName:+fileName+,filePath:+filePath+,fileSize:+fileSize+);writer.close();catch(IllegalStateException e)logger.error(e.getMessage(),e);catch(IOException e)logger.error(e.getMessage(),e);finally ftpUtil.closeServer();ResumeCommentController.java/*下载简历评论附件*param request*return*/RequestMapping(value=/downloadResumeComment,method=RequestMethod.GET)public ModelAndView downloadAttach(String fileName,String filePath,Long fileSize,HttpServletResponse response)HttpServletRequest request=getRequest();BufferedInputStream bis=null;BufferedOutputStream bos=null;FTPUtil ftpUtil=new FTPUtil();try,代码的坏味道,重复代码request.setCharacterEncoding(UTF-8);response.setContentType(text/html;charset=utf-8);response.setContentType(application/x-msdownload;);response.setHeader(Content-disposition,resumecomment;filename=+new String(fileName.getBytes(utf-8),ISO-8859-1);response.setHeader(Content-Length,String.valueOf(fileSize);ftpUtil.connectServer();InputStream input=ftpUtil.downFile(filePath);bis=new BufferedInputStream(input);bos=new BufferedOutputStream(response.getOutputStream();byte buff=new byte2048;int bytesRead;while(-1!=(bytesRead=bis.read(buff,0,buff.length)bos.write(buff,0,bytesRead);catch(Exception e)e.printStackTrace();finally try if(bis!=null)bis.close();if(bos!=null)bos.close();ftpUtil.closeServer();catch(IOException e),代码的坏味道,重复代码e.printStackTrace();return null;3.抽象功能重复model.addObject(officeRequire,officeRequirList.size()0?officeRequirList.get(0):null);model.addObject(remuneration,remunerationList.size()0?remunerationList.get(0):null);model.addObject(jobSetting,jobSettingList.size()0?jobSettingList.get(0):null);if(pJobSearch.getEducationalBackground()!=null,代码的坏味道,重复代码 if(pJobSearch.getPosition()!=null,代码的坏味道,重复代码4.数据交换if(companys=null|companys.size()=0)Company company=companyService.findCompanyByUId(userId);Map companyInfo=new HashMap();companyInfo.put(companyId,company.getId();companyInfo.put(companyName,company.getCompanyName();companyInfo.put(logoPath,company.getLogoPath();companys.add(companyInfo);,代码的坏味道,重复代码5.解决之道提取方法 if(companyId=null)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(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.saveOrUpdate(company);,代码的坏味道,重复代码 private String getCompIdx(Object companyId)String corpIdx=companyId.toString();int companyIdStrLength=corpIdx.length();for(int i=0;i 6-companyIdStrLength;i+)corpIdx=0+corpIdx;return comIdx;if(companyId=null)Company c=new Company();c.setUserId(userId);companyService.save(c);companyId=c.getId();c.setCorpIdx(getCompIdx(companyId);companyService.saveOrUpdate(c);else Company company=companyService.get(companyId);if(company.getCorpIdx()=null)company.setCorpIdx(getCompIdx(companyId);companyService.saveOrUpdate(company);,代码的坏味道,重复代码model.addObject(officeRequire,officeRequirList.size()0?officeRequirList.get(0):null);model.addObject(remuneration,remunerationList.size()0?remunerationList.get(0):null);model.addObject(jobSetting,jobSettingList.size()0?jobSettingList.get(0):null);private Object getFirstMember(List list)return list.size()0?list.get(0):null;model.addObject(officeRequire,getFirstMember(officeRequirList);model.addObject(remuneration,getFirstMember(remunerationList);model.addObject(jobSetting,getFirstMember(jobSettingList);,代码的坏味道,重复代码if(pJobSearch.getPosition()!=null,代码的坏味道,重复代码private boolean isPositive(Integer number)return number!=null,代码的坏味道,重复代码if(companys=null|companys.size()=0)Company company=companyService.findCompanyByUId(userId);Map companyInfo=new HashMap();companyInfo.put(companyId,company.getId();companyInfo.put(companyName,company.getCompanyName();companyInfo.put(logoPath,company.getLogoPath();companys.add(companyInfo);,代码的坏味道,重复代码Jodd APIpublic void bean2Map(Object bean,String properties)Map companyInfo=new HashMap();for(String property:properties)companyInfo.put(property,BeanUtil.getProperty(bean,property);return companyInfo;if(companys=null|companys.size()=0)Company company=companyService.findCompanyByUId(userId);companys.add(bean2Map(company,new String“companyId”,”companyName”,”logoPath”);,代码的坏味道,过长方法public ResponseBody int login(String username,String password,String checkcode,HttpSession session,HttpServletRequest request)String code=(String)session.getAttribute(validateCode);if(checkcode=null|!code.equals(checkcode)return 0;User user=this.us.findUnique(from User as u where u.userName=?,username);if(user=null)return 1;/待删除this.userId=user.getId();if(!password.equals(user.getPassword()return 2;/保存登录信息user.setBeforeOperateTime(user.getOperateTime();user.setBeforeOperateHost(user.getOperateHost();user.setOperateTime(new Date();,代码的坏味道,过长方法user.setOperateHost(getRequest().getRemoteAddr();if(user.getBeforeOperateTime()=null)user.setBeforeOperateTime(user.getOperateTime();if(user.getBeforeOperateHost()=null)user.setBeforeOperateHost(user.getOperateHost();commonService.update(user);MailAccount mailAccount=mailAccountService.getMailAccount(user.getId();if(mailAccount!=null)user.setMailAccount(mailAccount);mailManageService.connectReceive(mailAccount);setUser(user);return 3;,代码的坏味道,过长方法解决之道提取方法private boolean checkCode(HttpSession session)String code=(String)session.getAttribute(validateCode);return checkcode!=null,代码的坏味道,过长方法解决之道提取方法user.setOperateTime(new Date();user.setOperateHost(getRequest().getRemoteAddr();if(user.getBeforeOperateTime()=null)user.setBeforeOperateTime(user.getOperateTime();if(user.getBeforeOperateHost()=null)user.setBeforeOperateHost(user.getOperateHost();commonService.update(user);private void setMailAccount(User user)MailAccount mailAccount=mailAccountService.getMailAccount(user.getId();if(mailAccount!=null)user.setMailAccount(mailAccount);mailManageService.connectReceive(mailAccount);,代码的坏味道,过长方法解决之道提取方法 public ResponseBody int login(String username,String password,String checkcode,HttpSession session,HttpServletRequest request)if(!checkCode(session)return 0;User user=null;if(!checkUser(user)return 1;if(!checkPassword(password,user)return 2;saveLoginInfo(user);setMailAccount(user);setUser(user);return 3;,代码的坏味道,过长方法,代码的坏味道,过长方法,代码的坏味道,过大类public class CompanyController extends UploadControllerJSONMessage save(CompanyName company)JSONMessage collectCompany(Long companyId)JSONMessage findCompany(String companyName)JSONMessage findOtherCompany(String companyName,Long companyIdOfJob)public ModelAndView getCompanyByCompanyId(Long id)public ModelAndView getCompany()JSONMessage saveCompany(Company company)JSONMessage saveCompanyContact(CompanyContact companyContact)JSONMessage removeCompanyContact(CompanyContact companyContact)JSONMessage getCompanyMap(Long companyId,Long id,Long linkman)JSONMessage saveCompanyMap(CompanyMap companyMap),代码的坏味道,过大类JSONMessage getCompanyMap(Long id)public ModelAndView getFormMap(Long id)public void uploadPhoto(RequestParam(file)CommonsMultipartFile file,Long id,HttpServletResponse response)public void showPhoto(String photoid,HttpServletResponse response)private void showLogoNotGif(String filepath,FTPUtil ftpUtil,HttpServletResponse response)throws IOException public ModelAndView showRecommend()public ModelAndView showJobList(PJob pJob,Integer pageNo,Long curResumeId)Boolean desployTask(String empIdx,String corpIdx,Long resumeIdx,Long jobIdx,String jobName,String jobDept,String jobCompName,String userCorpIdx,String opinion,String fileName,Integer fileSize,String filePath,String proxyUrl,Integer master,Integer flowType)public ModelAndView report(Long companyId)throws UnsupportedEncodingException public void uploadComment(RequestParam(file)CommonsMultipartFile file,HttpServletResponse response),代码的坏味道,过大类解决之道合并方法JSONMessage findCompany(String companyName)JSONMessage findOtherCompany(String companyName,Long companyIdOfJob)public ModelAndView

    注意事项

    本文(如何写好的代码.ppt)为本站会员(sccc)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开