本帖最后由 KaiserYao 于 2019-8-18 22:43 编辑
引⾔言 史蒂夫.乔布斯说,”复杂的终极境界是简单“,同样的优雅的代码⼀一定是精简明了了,可读性好 优化 LocalDate和LocalDateTime 优化前 SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdfMins = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String today = sdfDay.format(now); String waterStart = today + " 01:00:00"; String waterEnd = today + " 02:00:00"; Date waterStartTime = sdfMins.parse(waterStart); Date waterEndTime = sdfMins.parse(waterEnd); 优化后 Lombok 优化前 LocalDateTime now = LocalDateTime.now(); LocalDateTime waterStart = LocalDateTime.of(now.getYear(), now.getMonth(),now.getDayOfMonth(),1,0); LocalDateTime waterEndTime =LocalDateTime.of(now.getYear(), now.getMonth(),now.getDayOfMonth(),2,0); public class test { private String abc; private String bcd; public String getAbc() { return abc; } public void setAbc(String abc) { this.abc = abc; } public String getBcd() { return bcd;
} } } public void setBcd(String bcd) { this.bcd = bcd; } @Override public String toString() { return "test{" + "abc='" + abc + ''' + ", bcd='" + bcd + ''' + '}'; 优化后 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> @Data public class test { private String abc; private String bcd; } Optional 优化前 优化后 字符串串累加 优化前 if (StringUtils.isEmpty(phone)) { param.put("status", "119"); } else { param.put("status", phone); } Optional.ofNullable(phone).orElse("119"); //或者 StringUtils.defaultIfEmpty(phone, "119") StringBuffer sb = new StringBuffer(); sb.append("asd"); sb.append(request.getSellerTaobaoId()); sb.append("|123"); 字符串串只要不不在for循环⾥里里累加,可以直接⽤用+号,因为编译成字节码后会变成 StringBuilder,如果在for循环⾥里里⾯面⽤用+号会⽣生成多个StringBuilder,所以在for循环⾥里里累加最 好在循环外创建StringBuilder 优化后 String sblog="asd"+request.getSellerTaobaoId()+"|123; List To Map 优化前 Map<String, String> AssetsMetaIdMap = Maps.newHashMap(); for (AssetsInfoBO assetsInfoBO : request.getAssetsCollectionList()) { AssetsMetaIdMap.put(assetsInfoBO.getAssetMetadataId(), assetsInfoBO.getAssetMetadataId()); } 优化后 减少不不必要的判断 优化前 优化后 return request.getExtDataValue(REQUEST_ID_KEY); 去掉else 优化前 Map<String, String> AssetsMetaIdMap = request.getAssetsCollectionList().stream().collect(Collectors.toMap(Ho sting::getAssetMetadataId, Hosting::getAssetMetadataId)); String requestId = null; if (null != request.getExtData()) { requestId = request.getExtDataValue(REQUEST_ID_KEY); } return requestId; if (null != result & return no; } else { throw new RuntimeException("XX"); } 优化后 不不返回Boolean 优化前 优化后 invoke = "true".equalsIgnoreCase(value.toString()); 最⼩小化判断 优化前 优化后 省略略⽆无意义的赋值 优化前 if (null != result & return no; } throw new RuntimeException("XX"); if ("true".equalsIgnoreCase(value.toString())) { invoke = true; } else { invoke = false; } if (0 == code) { sendMessage("A001", "success", out) } else { sendMessage("A001", "fial", out) } String message = (0 == code ? "success" : "fail"); sendMessage("A001", message, out); public List<User> query(Map<String, Object> params){ List<User> userList = null; try { userList = mapper.queryList(params); } catch (Exception e) { throw new RuntimeException(e.getMessages()) } return userList; } 优化后 public List<User> query(Map<String, Object> params){ try { return mapper.queryList(params); } catch (Exception e) { throw new RuntimeException(e.getMessages()) } } |