原来的代码:
1 List<MeasureResult> byPatientId = this.measureResultRepository.findByPatientId(patientId); 2 if (byPatientId == null || byPatientId.size() < 1) { 3 return ServerResponse.fail("暂无测量结果"); 4 } 5 6 Sort sort = new Sort(Sort.Direction.DESC, new String[] {"createTime"}); 7 PageRequest pageRequest = PageRequest.of(pageNum, 8, sort); 8 9 Page<MeasureResult> measureResultsPage = 10 this.measureResultRepository.findByPatientId(patient.getId(), pageRequest);
可以看到,代码中调用了两个名称相同、参数不同的方法。而第一个方法的返回结果仅仅用在了判断是否有满足条件的数据这个逻辑上,完全可以由第二个方法的返回结果来代替。故修改后代码:
1 Sort sort = new Sort(Sort.Direction.DESC, new String[] {"createTime"}); 2 PageRequest pageRequest = PageRequest.of(pageNum, 8, sort); 3 4 Page<MeasureResult> page = this.measureResultRepository.findByPatientId(patient.getId(), pageRequest); 5 if (!page.hasContent()) { 6 return ServerResponse.fail("暂无测量结果"); 7 }