How to split up complex conditions and keep short circuit evaluation?

前端 未结 11 1875
清歌不尽
清歌不尽 2020-12-17 16:55

Sometimes conditions can become quite complex, so for readability I usually split them up and give each component a meaningful name. This defeats short-circuit evaluation ho

11条回答
  •  攒了一身酷
    2020-12-17 17:50

    If your goal is readability, why not simply break the lines and add comments?

        if (args != null                // args not null
            && args.length == 2         // args length is OK
            && !args[0].equals(args[1]) // args are not equal
        ) {
    
            System.out.println("Args are ok");
        }
    

提交回复
热议问题