Adia
V2EX  ›  Java

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

  •  
  •   Adia · May 9, 2017 · 2774 views
    This topic created in 3473 days ago, the information mentioned may be changed or developed.
    if(spec.getVolumeSpecs() == null || spec.getVolumeSpecs().size() ==0 || spec.getVolumeSpecs().get(0) == null || spec.getVolumeSpecs().get(0).getPrimaryStorageInventory() == null){
        return;
    }
    
    6 replies  •  2017-05-18 17:13:26 +08:00
    letitbesqzr
    letitbesqzr
    May 9, 2017
    1
    前两个判断随便找个工具类:
    spec.getVolumeSpecs() == null || spec.getVolumeSpecs().size() ==0 ->CollectionUtil.isEmpty(spec.getVolumeSpecs())

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

    spec.getVolumeSpecs().get(0).getPrimaryStorageInventory() == null 看你业务是否有这个必要
    SoloCompany
    SoloCompany
    May 9, 2017 via iPhone
    2
    ?语法糖的最佳使用场景













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

    if(spec.xxx()){return;}
    sonyxperia
    sonyxperia
    May 18, 2017
    6
    写个通用的 util
    抽取成一个方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2970 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 00:49 · PVG 08:49 · LAX 16:49 · JFK 19:49
    ♥ Do have faith in what you're doing.