Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?

后端 未结 4 1619
栀梦
栀梦 2020-12-07 00:42

I recently discovered that Java (and Scala) include non-short-circuiting logical operators &, |, and ^. I previously thought thes

4条回答
  •  粉色の甜心
    2020-12-07 01:22

    Using the non-short-circuit boolean operators implies that the operands have side effects. If they had no side effects, then the programmer could have used the short-circuit versions with no change in functionality.

    In code written in a functional style (which Scala surely encourages but does not require), side effects are an indication that something unusual is going on. Unusual things should be clearly indicated in code, rather than by something as subtle as a non-short-circuit boolean operator.

提交回复
热议问题