How to optimize the code

  • 1 Clear structure design

    Before writing the code, think about how the entire structure is divided, such as control layer, service layer, persistence layer division, tool class extraction, independent function high cohesion encapsulation, etc., design the entire architecture, and then code and fill content under the guidance of this architecture .

  • 2 Clear logical steps

    Specific to each method, what should be done in the first step and what should be done in the second step, so that a clear idea can be sorted out. It is recommended to declare the steps in the comments with numbers such as 1, 2, 3; according to this idea, you can It is natural to think about where to extract the method. The most unbearable thing is that a method is very long and there is no comment in it.

  • 3 Good code practices

    There are many specifications, and you need to slowly develop good habits from the beginning, such as writing notes and logs


  • For example

    -before fixing:

    @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;
    

    }
    -After modification:

     @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;
    

    }

Guess you like

Origin blog.csdn.net/weixin_42541360/article/details/93972797