zoukankan      html  css  js  c++  java
  • 代码改造实录--循环外能确定的事情别放循环内

    原来的代码:

     1         for (MeasureResult measureResults : page.getContent()) {
     2             MeasureResultsPageVo measureResultsPageVo = new MeasureResultsPageVo();
     3 
     4             BeanUtils.copyProperties(measureResults, measureResultsPageVo);
     5             measureResultsPageVo.setCreateTime(measureResults.getCreateTime().getTime());
     6             patient = patientInfoService.getPatientInfo(measureResults.getPatientId());
     7 
     8             String path = CommUtil.getURL(request);
     9             measureResultsPageVo.setUrl(CommUtil.getHeadPhoto(path, patient.getPhoto()));
    10             measureResultsPageVo.setName(measureResults.getUserName());
    11             measureResultsPageVo.setState(measureResults.getConditionState());
    12             measureResultsPageVo.setDegree(measureResults.getSideEffectsCondition());
    13             measureResultsPageVo.setCreateTime(measureResults.getDate().getTime());
    14 
    15             measureResultsPageVoList.add(measureResultsPageVo);
    16         }

    可以在循环外就确定的有:

    一、 String path = CommUtil.getURL(request);因为request是整个方法的入参,无论循环多少次都是一样的结果,为何不放循环外呢!

    二、 patient = patientInfoService.getPatientInfo(measureResults.getPatientId()); 因为measureResults本来就是通过patientId查询得来的,每条数据的patientId都是一致的,也应该放在循环外。而且方法内循环前就已经获取了patient对象值,故可以改为:

     1         String path = CommUtil.getURL(request);
     2         String url = CommUtil.getHeadPhoto(path, patient.getPhoto());
     3         for (MeasureResult measureResults : page.getContent()) {
     4             MeasureResultsPageVo measureResultsPageVo = new MeasureResultsPageVo();
     5 
     6             BeanUtils.copyProperties(measureResults, measureResultsPageVo);
     7             measureResultsPageVo.setCreateTime(measureResults.getCreateTime().getTime());
     8 
     9             measureResultsPageVo.setUrl(url);
    10             measureResultsPageVo.setName(measureResults.getUserName());
    11             measureResultsPageVo.setState(measureResults.getConditionState());
    12             measureResultsPageVo.setDegree(measureResults.getSideEffectsCondition());
    13             measureResultsPageVo.setCreateTime(measureResults.getDate().getTime());
    14 
    15             measureResultsPageVoList.add(measureResultsPageVo);
    16         }
  • 相关阅读:
    try catch finally
    类的小练习
    易混淆概念总结
    C#中struct和class的区别详解
    Doing Homework again
    悼念512汶川大地震遇难同胞——老人是真饿了
    Repair the Wall
    Saving HDU
    JAVA-JSP隐式对象
    JAVA-JSP动作
  • 原文地址:https://www.cnblogs.com/GreenMountain/p/13499245.html
Copyright © 2011-2022 走看看