史蒂夫.喬布斯說(shuō),”復(fù)雜的終極境界是簡(jiǎn)單“,同樣的優(yōu)雅的代碼一定是精簡(jiǎn)明了,可讀性好。
使用LocalDate和LocalDateTime
LocalDate精確到日期,LocalDateTime精確到時(shí)分秒。優(yōu)化前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;
-
}
優(yōu)化后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);
默認(rèn)值使用Optional
優(yōu)化前五行
-
if (null == status) {
-
param.put("status", new ArrayList<String>());
-
} else {
-
param.put("status", status);
-
}
優(yōu)化后一行,使用JDK8的Optional
-
Optional.ofNullable(status).orElse(new ArrayList<String>());
如果是字符串可以用
-
StringUtils.defaultIfEmpty(status,"")
字符串累加
字符串只要不在for循環(huán)里累加,可以直接用+號(hào),因?yàn)榫幾g成字節(jié)碼后會(huì)變成StringBuilder,如果在for循環(huán)里面用+號(hào)會(huì)生成多個(gè)StringBuilder,所以在for循環(huán)里累加最好在循環(huán)外創(chuàng)建StringBuilder。優(yōu)化前五行
-
StringBuffer sblog = new StringBuffer();
-
sblog.append("waterDriven|sellerId=");
-
sblog.append(request.getSellerTaobaoId());
-
sblog.append("|result=");
-
sblog.append(isSuccess);
優(yōu)化后一行
-
String sblog="waterDriven|sellerId="+request.getSellerTaobaoId()+"|result="+isSuccess;
以上場(chǎng)景用逗號(hào)和等號(hào)連接數(shù)據(jù),使用GUAVA的Joiner更精簡(jiǎn),可讀性更好
-
String sblog=Joiner.on("|").withKeyValueSeparator("=").join(ImmutableMap.of("sellerId", request.getSellerTaobaoId(), "result", isSuccess))
LIST TO MAP
優(yōu)化前4行
-
for (AssetsInfoBO assetsInfoBO : request.getAssetsCollectionList()) {
-
AssetsMetaIdMap.put(assetsInfoBO.getAssetMetadataId(), assetsInfoBO.getAssetMetadataId());
-
}
優(yōu)化后1行
-
Map<String, String> AssetsMetaIdMap = request.getAssetsCollectionList().stream().collect(Collectors.toMap(Hosting::getAssetMetadataId, Hosting::getAssetMetadataId));
如果key重復(fù)會(huì)拋出異常
減少不需要的判斷
優(yōu)化前5行
-
String requestId = null;
-
if (null != request.getExtData()) {
-
requestId = request.getExtDataValue(REQUEST_ID_KEY);
-
}
-
return requestId;
優(yōu)化后1行
-
return request.getExtDataValue(REQUEST_ID_KEY);
去掉else
優(yōu)化前5行
-
if (null != result && StringUtils.isNotBlank(no)) {
-
return no;
-
} else {
-
throw new RuntimeException("XX");
-
}
優(yōu)化后4行
-
if (null != result && StringUtils.isNotBlank(no)) {
-
return no;
-
}
-
throw new RuntimeException("XX");
不要返回布爾
優(yōu)化前5行
-
if ("true".equalsIgnoreCase(value.toString())) {
-
invoke = true;
-
} else {
-
invoke = false;
-
}
優(yōu)化后一行
-
invoke = "true".equalsIgnoreCase(value.toString());
使用級(jí)聯(lián)
優(yōu)化前5行
-
ParamBO paramBO = new ParamBO();
-
paramBO.setId(1);
-
paramBO.setName(”ifeve“);
-
paramBO.setOld(7);
優(yōu)化后1行
-
new ParamBO().withId(1).withName("ifeve").withOld(7);
-End-
審核編輯 :李倩
-
代碼
+關(guān)注
關(guān)注
30文章
4802瀏覽量
68737 -
LOCA
+關(guān)注
關(guān)注
0文章
7瀏覽量
7187
原文標(biāo)題:幾個(gè)代碼瘦身優(yōu)化的案例
文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論