程式碼重構、優化
阿新 • • 發佈:2021-12-31
關於程式碼的重構、優化,有很多技巧,我在這裡記錄下我遇到的問題
- 可以採用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 comment1 = generateCommentDataFunc.apply(ApproverName2); TechnicalApproverComment comment1 = generateCommentDataFunc.apply(ApproverName3); TechnicalApproverComment sealPersonComment = generateCommentDataFunc.apply(sealPerson); // 通過上面的優化,可以讓人一目瞭然,同一段程式碼執行了4次,幹了相同的工作,只是引數不同