代码如何优化

  • 1 清晰的结构设计

    写代码之前先想好整个结构是怎么划分的,控制层,服务层,持久层划分,工具类提取,独立功能高内聚封装等,把整个架构设计好,再在这个架构指导下编码填充内容。

  • 2 明确的逻辑步骤

    具体到每一个方法,第一步该做什么,第二步该做什么,这样能梳理出清晰的思路,建议用1,2,3这样的编号在注释里声明步骤;根据这个思路,就可以很自然的想到要在哪里提取方法,最不能忍的就是一个方法特别长,里面还没有注释。

  • 3 良好的代码规范

    规范有很多,需要慢慢从一开始就去养成良好的习惯,比如说写注释和日志


  • 举例

    -修改前:

    @Override
    public String buildHtml(ClassPathResource classPathResource, String filePath, Template templateEntity,
      String wisemarketingDomain, TemplateInfo templateInfo, boolean isExport, String target) throws CException {
      String html, templateDesc, templateTitle, templateKeyWords, templateWidth;
      try {
          html =
              new String(FileCopyUtils.copyToByteArray(classPathResource.getInputStream()), StandardCharsets.UTF_8);
      } catch (IOException e) {
          LOGGER.error("can not read template/index.html", e);
          throw new CException(StatusEnum.NSP_ERR_500003, "can not read template/index.html");
      }
    
      String businessArea = templateEntity.getBusinessArea();
    
      String templatePath = H5Utils.pathSelect(businessArea);
    
      File resourceFile = new File(filePath + templatePath + "/");
      // 如果根据业务领域获取不到,则取默认目录下的模板
      if (!resourceFile.exists()) {
          templatePath = H5builderEnum.DEFAULT_PATH.getValue();
      }
    
      if (H5builderEnum.APP_MARKET.getValue().equalsIgnoreCase(businessArea) && isExport
          && H5builderEnum.APP_MARKET.getValue().equalsIgnoreCase(target)) {
          html = html.replace(H5builderEnum.META_DATA.getValue(), "window.WS_METADATA".replace("\"", ""));
          html =
              html.replace(H5builderEnum.META_JS_PATH.getValue(), H5builderEnum.META_APP_MARKET_JS_PATH.getValue());
          html =
              html.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), H5builderEnum.APP_MARKET_PATH.getValue());
      } else if (isExport) {
          html = html.replace(H5builderEnum.META_DATA.getValue(), "window.WS_METADATA".replace("\"", ""));
          html = html.replace(H5builderEnum.META_JS_PATH.getValue(), H5builderEnum.META_JS_SCRIPT.getValue());
          html = html.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), "".replace("\"", ""));
      } else {
          html =
              html.replace(H5builderEnum.META_DATA.getValue(), JsonUtil.objectToString(templateEntity.getContent()));
          html = html.replace(H5builderEnum.META_JS_PATH.getValue(), "".replace("\"", ""));
          html = html.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), "".replace("\"", ""));
      }
    
      if (StringUtils.isNotBlank(templatePath)) {
          html = html.replace(H5builderEnum.BUSINESS_AREA.getValue(), templatePath.replace("\"", ""));
      }
    
      if (null == templateInfo) {
          templateDesc = JsonUtil.objectToString(templateEntity.getDescription());
          templateTitle = JsonUtil.objectToString(templateEntity.getTitle());
          templateKeyWords = JsonUtil.objectToString(templateEntity.getDescription());
          templateWidth = H5builderEnum.DEFAULT_WIDTH.getValue();
      } else {
          templateDesc = templateInfo.getDescription();
          templateTitle = templateInfo.getTitle();
          templateKeyWords = templateInfo.getKeyWords();
          templateWidth = templateInfo.getWidth();
      }
    
      SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
      String dateString = formatter.format(new Date());
    
      html = html.replace(H5builderEnum.APP_JS_STAMP.getValue(), dateString + "");
    
      html = html.replace(H5builderEnum.DESC.getValue(), templateDesc + "");
    
      html = html.replace(H5builderEnum.TITLE.getValue(), (templateTitle + "").replace("\"", ""));
    
      html = html.replace(H5builderEnum.KEY_WORDS.getValue(), templateKeyWords + "");
    
      html = html.replace(H5builderEnum.TEMPLATE_PATH.getValue(), wisemarketingDomain + "");
    
      html = html.replace(H5builderEnum.IMAGE.getValue(), "images");
    
      if (StringUtils.isNotBlank(templateWidth)) {
          html = html.replace(H5builderEnum.WIDTH.getValue(), templateWidth);
      } else {
          html = html.replace(H5builderEnum.WIDTH.getValue(), H5builderEnum.DEFAULT_WIDTH.getValue());
      }
      return html;
    

    }
    -修改后:

     @Override
     public String buildHtml(String filePath, Template templateEntity, String wisemarketingDomain,
          TemplateInfo templateInfo, boolean isExport, String target, boolean isCdnPath) throws CException {
      // 业务领域
      String businessArea = templateEntity.getBusinessArea();
    
      //1 读取html模板文件
      String modelHtml = getModelHtmlContent(businessArea);
      LOGGER.info("get model html content success");
    
      //2 替换html文件中的元数据和各种引用路径
      //2.1 engine文件路径替换
      String enginePath = H5builderEnum.DEFAULT_PATH.getValue();
      // 查询该业务领域是否有拓展js,css
      H5StaticVo h5StaticVo = h5StaticResourceService.findByBusinessArea(businessArea);
      File resourceFile = new File(filePath + H5Utils.pathSelect(businessArea) + "/");
      // 如果数据库中没有静态资源路径,且H5 Server有engine包
      if (null == h5StaticVo && resourceFile.exists()) {
          enginePath = H5Utils.pathSelect(businessArea);
      }
      modelHtml = modelHtml.replace(H5builderEnum.BUSINESS_AREA.getValue(), enginePath.replace("\"", ""));
    
      //2.2 替换元数据和js路径,区分导出和发布
      if (!isCdnPath){
          // cdn路径替换标识只有发布到内容中心时需要
          modelHtml = modelHtml.replace(H5builderEnum.CDNPATH.getValue(), "".replace("\"", ""));
      }
      if (isExport){
          // 导出
          if (H5builderEnum.APP_MARKET.getValue().equalsIgnoreCase(target)||H5builderEnum.APP_MARKET.getValue().equalsIgnoreCase(businessArea)){
              // 导出至应用市场
              modelHtml = modelHtml.replace(H5builderEnum.META_JS_PATH.getValue(), H5builderEnum.META_APP_MARKET_JS_PATH.getValue());
              modelHtml = modelHtml.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), H5builderEnum.APP_MARKET_PATH.getValue());
          }else {
              // 其他导出
              modelHtml = modelHtml.replace(H5builderEnum.META_JS_PATH.getValue(), H5builderEnum.META_JS_SCRIPT.getValue());
              modelHtml = modelHtml.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), "".replace("\"", ""));
              modelHtml = replaceExtendResource(modelHtml,h5StaticVo);
          }
          modelHtml = modelHtml.replace(H5builderEnum.META_DATA.getValue(), "window.WS_METADATA".replace("\"", ""));
      }else {
          // 发布
          modelHtml = modelHtml.replace(H5builderEnum.META_DATA.getValue(), JsonUtil.objectToString(templateEntity.getContent()));
          modelHtml = modelHtml.replace(H5builderEnum.EXPORT_TO_APP_MARKET.getValue(), "".replace("\"", ""));
          modelHtml = replaceExtendResource(modelHtml,h5StaticVo);
      }
      //2.3 替换描述,title,keyWords,width等信息
      String templateDesc, templateTitle, templateKeyWords, templateWidth;
      if (null == templateInfo) {
          templateDesc = JsonUtil.objectToString(templateEntity.getDescription());
          templateTitle = JsonUtil.objectToString(templateEntity.getTitle());
          templateKeyWords = templateDesc;
          templateWidth = H5builderEnum.DEFAULT_WIDTH.getValue();
      } else {
          templateDesc = templateInfo.getDescription();
          templateTitle = templateInfo.getTitle();
          templateKeyWords = templateInfo.getKeyWords();
          templateWidth = templateInfo.getWidth();
      }
      modelHtml = modelHtml.replace(H5builderEnum.DESC.getValue(), templateDesc + "");
    
      modelHtml = modelHtml.replace(H5builderEnum.TITLE.getValue(), (templateTitle + "").replace("\"", ""));
    
      modelHtml = modelHtml.replace(H5builderEnum.KEY_WORDS.getValue(), templateKeyWords + "");
    
      modelHtml = modelHtml.replace(H5builderEnum.TEMPLATE_PATH.getValue(), wisemarketingDomain + "");
    
      modelHtml = modelHtml.replace(H5builderEnum.IMAGE.getValue(), "images");
    
      modelHtml = modelHtml.replace(H5builderEnum.WIDTH.getValue(), templateWidth + "");
      
      SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
      String dateString = formatter.format(new Date());
      modelHtml = modelHtml.replace(H5builderEnum.APP_JS_STAMP.getValue(), dateString + "");
    
      return modelHtml;
    

    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值