V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Adia
V2EX  ›  Java

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

  •  
  •   Adia · 2017-05-09 19:32:02 +08:00 · 1729 次点击
    这是一个创建于 2548 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if(spec.getVolumeSpecs() == null || spec.getVolumeSpecs().size() ==0 || spec.getVolumeSpecs().get(0) == null || spec.getVolumeSpecs().get(0).getPrimaryStorageInventory() == null){
        return;
    }
    
    6 条回复    2017-05-18 17:13:26 +08:00
    letitbesqzr
        1
    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
        2
    SoloCompany  
       2017-05-09 21:58:04 +08:00 via iPhone
    ?语法糖的最佳使用场景













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

    if(spec.xxx()){return;}
    sonyxperia
        6
    sonyxperia  
       2017-05-18 17:13:26 +08:00
    写个通用的 util
    抽取成一个方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2262 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 04:16 · PVG 12:16 · LAX 21:16 · JFK 00:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.