Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable short circuit evaluation in Java?

Say I have code like this:

boolean ret = a() && b() && c() && d() && e();

Usually e() is only called if all other calls a()-d() return true. Is there maybe some compiler or JVM option to disable short circuit evaluation, so e() would be called always, regardless of other functions' results?

Basically I am doing UAT of huge system and need to test e(), however setting up environment and scenario that assures all a(), b() etc. return true is extremely painful...

EDIT: ok, I guess using bit AND instead of logical one could provide SOME sort of workaround, however ideally I am looking for a solution that does not require ANY CHANGES in the source code. Both due to formal and technical reason (as i mentioned system is big and we have whole process of promoting and deploying code between staging areas and getting sign-offs). And this is for testing only, production version needs to have lazy evaluation enabled (i.e. use &&)

POST-MORTEM:

  • "Correct" answer is: No, there is not.
  • "Useful" answer: you can change && to &
  • "What I did in the end" answer: debug system remotely, put breakpoint on expression and told eclipse to run e() -_-
like image 618
Kranach Avatar asked Dec 06 '13 11:12

Kranach


People also ask

Does Java have short-circuit evaluation?

Java's && and || operators use short circuit evaluation. Java's & and | operators also test for the "and" and "or" conditions, but these & and | operators don't do short circuit evaluation.

What is short-circuit evaluation Why is it useful?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

What is short-circuit evaluation aka early termination )?

Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR ) you can stop as soon as you find the first condition which satisfies or negates the expression.

What is short-circuit evaluation in context of && and || operators?

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first ...


1 Answers

In order to disable short-circuit use single '&' or '|' rather than two:

boolean ret = a() & b() & c() & d() & e();
like image 107
amatellanes Avatar answered Sep 26 '22 17:09

amatellanes