Short circuit against operators without short circuit

I understand the difference below (at least for Java):

if( true || false ) // short-circuiting boolean operator if( true | false ) // non-short-circuiting boolean operator 

But my question is: is there any reason to use the operator without a short circuit when you are dealing with boolean expressions? Is there any kind of utility or use of performance that would not be considered bad practice?

+6
source share
8 answers

One of the reasons why you might want to use the operator without a short circuit is that you are somehow dependent on the side effects of the functions. For instance.

 boolean isBig(String text) { System.out.println(text); return text.length() > 10; } ... if( isBig(string1) || isBig(string2) ){ ... } 

If it doesn't matter to you whether println is running, you should use short circuit operations as described above. However, if you want both lines to be printed always (thus, depending on side effects), you need to use the operator without a short circuit.

Practically speaking, you almost always want to use short circuit operators. Relying on side effects in expressions is usually a bad programming practice.

One exception is code with a very low level or high sensitivity. Short-circuit statements may be slightly slower because they cause branching in program execution. Also, the use of bitwise operators allows you to perform 32 or 64 parallel logical operations as a whole, which is very fast.

+9
source

If your code is performance sensitive enough and operations are cheap enough, using a short circuit can be faster. This is because using || involves branch execution, and failing branch prediction can be very expensive. Where when | performs the calculation and consideration of a variable, which can be much faster, avoiding the failures of branch prediction.

Note. This is micro-optimization, which you rarely see the difference if it is not called many times.

+6
source

short-circuit , which means that they do not evaluate the right side if it is not required. As an example, if && left side is false, there is no need to evaluate the right side. Another way || if left true, no need to evaluate the right side.

non-short pricing both sides always.

Then, obviously, there is an advantage with short-circuit operators.

And the benefit of short circuiting can find the answer here. Are there any good possibilities for short-circuit logical (logical) statements in Java / Scala?

Consider also this example.

  while (status){ // status is boolean value if(!a | result){// a and result are boolean value result=getResult(); // result can change time to time } } 

we need to check both sides.

+5
source

my question is more focused on finding the benefits to use | when dealing only with booleans

Consider the following case

 while ( !credentialvalid() | (loginAttempts++ < MAX) ) { // tell something to user. } 

In this case, | because I also need to increase the counter of attempts :)

+5
source

the only place where you should not use the operator without short circuits is when you want to execute the second operator, which should not be in conditional statements at all

No , there is no performance with a non-short circuit, but there is a complete advantage with a short circuit

+4
source

If you use

  if(true || /*someStatementHere*/) 

then the whole if block will be true, because the first condition is true, so it should not check another

Simply put, the right operand is not evaluated if the first condition gives a result in the case of short circuit operators

+2
source

Third operator

 if( 10 | 11 ) // bitwise operator 

will create a compilation error. Only booleans can be placed inside an if statement.

If you use an IDE, such as Eclipse, it automatically displays the second expression, that is, after || like a dead code for a boolean short circuit operator.

+2
source

For simple Boolean expressions | sometimes faster than || . And sometimes you want WANT expressions to be evaluated evenly (for example, when there is ++ in the index value.

You cannot accidentally "mix" Boolean | and bitwise | , since you cannot mix logical and integer operands. But you can (of course) accidentally use | where you wanted to use || . Just be thankful to him not for C, where you can accidentally use = instead of == .

+2
source

Source: https://habr.com/ru/post/952173/


All Articles