Java 中这么写觉得很麻烦,有什么办法写的好看点吗?

2017-05-09 19:32:02 +08:00
 Adia
if(spec.getVolumeSpecs() == null || spec.getVolumeSpecs().size() ==0 || spec.getVolumeSpecs().get(0) == null || spec.getVolumeSpecs().get(0).getPrimaryStorageInventory() == null){
    return;
}
1750 次点击
所在节点    Java
6 条回复
letitbesqzr
2017-05-09 21:01:59 +08:00
前两个判断随便找个工具类:
spec.getVolumeSpecs() == null || spec.getVolumeSpecs().size() ==0 ->CollectionUtil.isEmpty(spec.getVolumeSpecs())

spec.getVolumeSpecs().get(0) == null 没有意义

spec.getVolumeSpecs().get(0).getPrimaryStorageInventory() == null 看你业务是否有这个必要
SoloCompany
2017-05-09 21:58:04 +08:00
?语法糖的最佳使用场景













只不过 java 没有😏
Adia
2017-05-10 14:05:28 +08:00
@SoloCompany 请问什么语言有?
Michaelssss
2017-05-15 17:21:10 +08:00
。。。。。。。这个时候不就是自己写个 Utils 的最佳实践么。。。
UncleRiver
2017-05-17 15:48:03 +08:00
不应该直接写在 if 里,这么长的条件判断最好是包成一个方法,而且如果是跟 spec 逻辑相关的,可以做成 spec 的一个共有方法,然后直接

if(spec.xxx()){return;}
sonyxperia
2017-05-18 17:13:26 +08:00
写个通用的 util
抽取成一个方法

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/360210

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX