• 代码重构、优化 season


    关于代码的重构、优化,有很多技巧,我在这里记录下我遇到的问题

    1. 可以采用lambda function,来解决方法内重复出现的代码,让逻辑更清晰
    // old
    TechnicalApproverComment comment1 =  new TechnicalApproverComment();
    comment1.setCreateByName(ApproverName1.getRealName());
    comment1.setCreateBy(ApproverName1.getPersonnelInfoCode());
    TechnicalApproverComment comment2 =  new TechnicalApproverComment();
    comment2.setCreateByName(ApproverName2.getRealName());
    comment2.setCreateBy(ApproverName2.getPersonnelInfoCode());
    TechnicalApproverComment comment3 =  new TechnicalApproverComment();
    comment3.setCreateByName(ApproverName3.getRealName());
    comment3.setCreateBy(ApproverName3.getPersonnelInfoCode());
    TechnicalApproverComment sealPersonComment =  new TechnicalApproverComment();
    sealPersonComment.setCreateByName(sealPerson.getRealName());
    sealPersonComment.setCreateBy(sealPerson.getPersonnelInfoCode());
    
    // new
    Function<BasePersonnelInfoVO, TechnicalApproverComment> generateCommentDataFunc = (BasePersonnelInfoVO person) -> {
        TechnicalApproverComment comment = new TechnicalApproverComment();
        comment.setCreateByName(person.getRealName());
        comment.setCreateBy(person.getPersonnelInfoCode());
        return comment;
    };
    TechnicalApproverComment comment1 = generateCommentDataFunc.apply(ApproverName1);
    TechnicalApproverComment comment2 = generateCommentDataFunc.apply(ApproverName2);
    TechnicalApproverComment comment3 = generateCommentDataFunc.apply(ApproverName3);
    TechnicalApproverComment sealPersonComment = generateCommentDataFunc.apply(sealPerson);
    
    // 通过上面的优化,可以让人一目了然,同一段代码执行了4次,干了相同的工作,只是参数不同
    
    梦想不多,口袋有糖,卡里有钱,未来有你
  • 相关阅读:
    环境变量不重启生效
    egret list不显示问题
    批处理收集
    吐槽阿里云数据库的备份还原
    一次没清缓存的坑
    imagepng或imagejpeg浏览器无显示问题
    xdebug调试一直等待连接
    Python快速学习07:文本文件的操作
    Python快速学习06:词典
    纸上谈兵: AVL树[转]
  • 原文地址:https://www.cnblogs.com/season-qd/p/15752173.html
Copyright © 2020-2023  润新知