史蒂夫.乔布斯说,”复杂的终极境界是简单“,同样的优雅的代码一定是精简明了,可读性好。
使用LocalDate和LocalDateTime
LocalDate精确到日期,LocalDateTime精确到时分秒。优化前14行代码
-
try {
-
SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
-
SimpleDateFormat sdfMins = new SimpleDateFormat("yyyy-MM-dd HHss");
-
Date now = new Date();
-
String today = sdfDay.format(now);
-
String waterStart = today + " 0300";
-
String waterEnd = today + " 0400";
-
Date waterStartTime = sdfMins.parse(waterStart);
-
Date waterEndTime = sdfMins.parse(waterEnd);
-
} catch (ParseException pe) {
-
return XX;
-
}
优化后3行代码
-
LocalDateTime now = LocalDateTime.now();
-
LocalDateTime waterStart = LocalDateTime.of(now.getYear(), now.getMonth(),now.getDayOfMonth(),3,0);
-
LocalDateTime waterEndTime =LocalDateTime.of(now.getYear(), now.getMonth(),now.getDayOfMonth(),4,0);
默认值使用Optional
优化前五行
-
if (null == status) {
-
param.put("status", new ArrayList<String>());
-
} else {
-
param.put("status", status);
-
}
优化后一行,使用JDK8的Optional
-
Optional.ofNullable(status).orElse(new ArrayList<String>());
如果是字符串可以用
-
StringUtils.defaultIfEmpty(status,"")
字符串累加
字符串只要不在for循环里累加,可以直接用+号,因为编译成字节码后会变成StringBuilder,如果在for循环里面用+号会生成多个StringBuilder,所以在for循环里累加最好在循环外创建StringBuilder。优化前五行
-
StringBuffer sblog = new StringBuffer();
-
sblog.append("waterDriven|sellerId=");
-
sblog.append(request.getSellerTaobaoId());
-
sblog.append("|result=");
-
sblog.append(isSuccess);
优化后一行
-
String sblog="waterDriven|sellerId="+request.getSellerTaobaoId()+"|result="+isSuccess;
以上场景用逗号和等号连接数据,使用GUAVA的Joiner更精简,可读性更好
-
String sblog=Joiner.on("|").withKeyValueSeparator("=").join(ImmutableMap.of("sellerId", request.getSellerTaobaoId(), "result", isSuccess))
LIST TO MAP
优化前4行
-
for (AssetsInfoBO assetsInfoBO : request.getAssetsCollectionList()) {
-
AssetsMetaIdMap.put(assetsInfoBO.getAssetMetadataId(), assetsInfoBO.getAssetMetadataId());
-
}
优化后1行
-
Map<String, String> AssetsMetaIdMap = request.getAssetsCollectionList().stream().collect(Collectors.toMap(Hosting::getAssetMetadataId, Hosting::getAssetMetadataId));
如果key重复会抛出异常
-
Exception in thread "main" java.lang.IllegalStateException: Duplicate key 80000
减少不需要的判断
优化前5行
-
String requestId = null;
-
if (null != request.getExtData()) {
-
requestId = request.getExtDataValue(REQUEST_ID_KEY);
-
}
-
return requestId;
优化后1行
-
return request.getExtDataValue(REQUEST_ID_KEY);
去掉else
优化前5行
-
if (null != result && StringUtils.isNotBlank(no)) {
-
return no;
-
} else {
-
throw new RuntimeException("XX");
-
}
优化后4行
-
if (null != result && StringUtils.isNotBlank(no)) {
-
return no;
-
}
-
throw new RuntimeException("XX");
不要返回布尔
优化前5行
-
if ("true".equalsIgnoreCase(value.toString())) {
-
invoke = true;
-
} else {
-
invoke = false;
-
}
优化后一行
-
invoke = "true".equalsIgnoreCase(value.toString());
使用级联
优化前5行
-
ParamBO paramBO = new ParamBO();
-
paramBO.setId(1);
-
paramBO.setName(”ifeve“);
-
paramBO.setOld(7);
优化后1行
-
new ParamBO().withId(1).withName("ifeve").withOld(7);
-End-
审核编辑 :李倩
-
代码
+关注
关注
30文章
4908浏览量
71305 -
LOCA
+关注
关注
0文章
11浏览量
7254
原文标题:几个代码瘦身优化的案例
文章出处:【微信号:AndroidPush,微信公众号:Android编程精选】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
Texas Instruments TPS56425xEVM转换器评估模块 (EVM)数据手册

?TPS92520-Q1 双通道同步降压LED驱动器技术文档总结

泰凌微电子蓝牙信道探测解决方案亮点抢先看
TPSM843A26EVM电源模块评估模块技术解析与应用指南

圣邦微电子推出VCE275X系列轴心磁编码器芯片

?TPS92200同步降压LED驱动器技术文档总结

TMAG5173EVM传感器评估模块技术解析与应用指南

智多晶LLCR技术的工作原理和应用场景

比亚迪将在马来西亚建设组装工厂
中国储能全球占比超40%,独立储能占比46%首超新能源

套刻精度已达2μm,芯碁微装直写光刻设备获头部封测企业订单

事件相机会成为自动驾驶的下一代核心传感器?
SiLM2023CA-DG 智能相位控制的200V半桥驱动芯片
JBD再获亿元融资,“蜂鸟Ⅱ”彩色光引擎即将发布

评论