Adia
V2EX  ›  Java

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

  •  
  •   Adia · May 9, 2017 · 2491 views
    This topic created in 3318 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
        1
    letitbesqzr  
       May 9, 2017
    前两个判断随便找个工具类:
    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  
       May 9, 2017 via iPhone
    ?语法糖的最佳使用场景













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

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