Ok so I had a weird issue in java and I'm pretty sure this is common to most languages but it was the first time I've encountered something like this. Assume you have two methods.
private boolean foo(int param){ /*some arbitrary stuff*/ }
private boolean bar(int param){ /*some arbitrary stuff*/ }
Each of these methods expect parameters and based on the contents of the parameters they return a true
or false
based on whether or not something happened. Now I'm going to put them to work.
if(foo(21) || bar(321)){
//do some other stuff
}
The idea here is to run foo()
and bar()
and if either of them return true
do the other stuff.
It looked fine, it compiled, but when I ran the code things were funky. It was almost as if bar()
was not running whenever foo()
returned true
. So realizing the logical possibility of that I rewrote my code.
boolean a = foo(21);
boolean b = bar(321);
if(a || b){
//do some other stuff
}
and that fixed my issue. My question is does anyone know why this happens or if there are other practices to ensure the entirety of a conditional is evaluated should it need to?