简体   繁体   English

Java / Scala中的非短路逻辑(布尔)运算符有很好的用途吗?

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

I recently discovered that Java (and Scala) include non-short-circuiting logical operators & , | 我最近发现Java(和Scala)包括非短路逻辑运算符&| , and ^ . ,和^ I previously thought these only worked as bitwise operators. 我之前认为这些只能作为按位运算符。 While maybe there is an argument for ^ , I can't think of very good reasons for using non-short-circuiting logical operators--although sure, I can contrive an example. 虽然可能存在^的论证,但我无法想到使用非短路逻辑运算符的非常好的理由 - 尽管可以肯定,我可以设想一个例子。

Are these operators useful? 这些运营商有用吗? They seem more likely to cause hard-to-catch bugs. 它们似乎更容易导致难以捕获的错误。

scala> def foo = {
     |   println("foo")
     |   true
     | }
foo: Boolean

scala> def bar = {
     |   println("bar")
     |   true
     | }
bar: Boolean

scala> foo || bar
foo
res5: Boolean = true

scala> foo | bar
foo
bar
res6: Boolean = true

They're useful if the right-hand side is a function with side-effects that you want to execute regardless (eg logging). 如果右侧是具有您想要执行的副作用的函数(例如,日志记录),它们将非常有用。 However, I would suggest that that's a bit of a code smell and will certainly be unintuitive to the next guy. 但是,我认为这有点代码味道,对下一个人来说肯定是不直观的。

Hmm. 嗯。 I know they can be incredibly useful for optimizing C/C++ code if used carefully. 我知道如果仔细使用它们对于优化C / C ++代码非常有用。 It might apply to Java as well. 它也可能适用于Java。

The major use in C -- other than actual bit operations -- is to remove a pipeline stall. C中的主要用途 - 除了实际的位操作 - 是删除管道停顿。 The short circuit operators require a branch operation. 短路操作器需要分支操作。 The bitwise operator will compute both sides and removes the chance for a mispredicted branch and the resulting stall. 按位运算符将计算双方并消除错误预测分支和结果停顿的机会。

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. 在以功能样式编写的代码中(Scala肯定鼓励但不需要),副作用表明正在发生一些不寻常的事情。 Unusual things should be clearly indicated in code, rather than by something as subtle as a non-short-circuit boolean operator. 应该在代码中清楚地指出不寻常的东西,而不是像非短路布尔运算符那样微妙的东西。

If you are trying to track answers or input for something, and that depends on both sides of your non-short-circuit boolean running. 如果您正在尝试跟踪某些内容的答案或输入,那么这取决于非短路布尔运行的两侧。

As an example, say you have: 举个例子,假设你有:

if(methodA() & methodB()){
    //some code
}

And within the methodB() method some essential code is running. 在methodB()方法中,一些基本代码正在运行。 If that was a short circuit code ( && ) and methodA() was false, methodB() would never run. 如果那是短路代码(&&)并且methodA()为false,则methodB()将永远不会运行。

That is one of the uses I can think of, at least. 至少,这是我能想到的用途之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM