Benefits of using short-circuit evaluation

后端 未结 15 1495
余生分开走
余生分开走 2020-12-05 16:23
boolean a = false, b = true;
if ( a && b ) { ... };

In most languages, b will not get evaluated because a is fals

15条回答
  •  时光取名叫无心
    2020-12-05 16:54

    A useful short circuit I use is something like this:

    if (a != null && a.equals(somevalue)) {
        ... do something.
    }
    

    This is, in my opinion very readable, and functions quite well. Generally, I try too avoid to much nesting because it leads to ugly code.

    All my opinion though.

提交回复
热议问题