代码改造实录--循环外能确定的事情别放循环内

原来的代码:

 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         }

 

posted @ 2020-08-13 22:15  GreenMountain  阅读(121)  评论(0编辑  收藏  举报