0

I have a series of methods, each processing unique business rules, and each return a boolean result. I need ALL the methods executed, but if any one of them returns a true, then additional processing is required on dependent data. So, I can't just do:

boolean bizRuleFailed = false;
bizRuleFailed = methOneRule(data) || methTwoRule(data) || methThreeRule(data);

because it would stop after the first true-result method. So I am doing this:

boolean bizRuleFailed = false;
bizRuleFailed = methOneRule(data) || bizRuleFailed;
bizRuleFailed = methTwoRule(data) || bizRuleFailed;
bizRuleFailed = methThreeRule(data) || bizRuleFailed;

, which is far from ideal. What's a good way to do this instead? Thanks

Timothy Clotworthy
  • 1,960
  • 2
  • 19
  • 42

3 Answers3

1

You could actually use the bitwise | instead of the logical ||. This doesn't short circuit and providing you're comparing boolean values they are otherwise identical.

boolean bizRuleFailed = methOneRule(data) | methTwoRule(data) | methThreeRule(data);
Henry Twist
  • 5,666
  • 3
  • 19
  • 44
1
 bizRuleFailed |= methOneRule(data);
 bizRuleFailed |= methTwoRule(data);
 bizRuleFailed |= methThreeRule(data);

Or shorter

 bizRuleFailed = methOneRule(data) |  methTwoRule(data) | methThreeRule(data);
Devolus
  • 21,661
  • 13
  • 66
  • 113
1

The operator you are using, || is called a Logical Or (or alternatively an Or Else) operator, which means that if the left-hand expression evaluates to true, the right-hand expression will not be evaluated.

Instead, use the standard Bitwise Or operator, |, which will cause all expressions to be evaluated before the result is determined:

bizRuleFailed = methOneRule(data) | methTwoRule(data) | methThreeRule(data);
Martin
  • 16,093
  • 1
  • 29
  • 48