-6

Lets see this sample.

List <String> lst = getSome ();  // returns null or a list
if (lst == null || lst.contains ("bla") == true)
{
   // go on

That will work - the null-value is catched. But thats only the case because I know the if term is evaluated from left to right and it is not fully evaluated if the result gets unchangeable (1 || x is always 1). At least that is what I see if I let it run.

But it is not appearent at the very first glance and it can depend on behaviours behind the curtain and that may change sometime.

The question is: Is that good style in a language that would allow that?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
chris01
  • 10,921
  • 9
  • 54
  • 93
  • 4
    Which language are you asking for? – François Andrieux Nov 01 '18 at 20:31
  • 2
    What is your question – chevybow Nov 01 '18 at 20:31
  • 1
    If you're asking whether `||` always shortcircuits in Java, it does. – ifly6 Nov 01 '18 at 20:31
  • `c++` has short circuit evaluation. https://stackoverflow.com/questions/628526/is-short-circuiting-logical-operators-mandated-and-evaluation-order – drescherjm Nov 01 '18 at 20:31
  • 4
    At least in c++, depending on operator short circuiting would not generally be considered overly obscure. It's reasonable to expect other developers to be aware of this feature and understand the code. – François Andrieux Nov 01 '18 at 20:32
  • 1
    I wrote some code like that earlier today. Personally I think that sort of expression reads fairly well. – George Nov 01 '18 at 20:49
  • 1
    This question might be a better fit for softwareengineering.SE. For example, there was a recent question [When is short-circuit evaluation bad?](https://softwareengineering.stackexchange.com/questions/371832/when-is-short-circuit-evaluation-bad) – chris Nov 01 '18 at 20:49
  • 2
    The Infinity Foundation has no coding style – user4581301 Nov 01 '18 at 20:49
  • I've removed the c++ tag because the code shown is almost certainly Java and would require a very contrived implementation of `List` to compile in c++. – François Andrieux Nov 01 '18 at 21:02
  • 1
    The Java Language Specification (https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.24) clearly states that "[it] evaluates its right-hand operand only if the value of its left-hand operand is false". If this were ever to change much existing code would have to be rewritten because people rely on this behaviour. – Thomas Kläger Nov 01 '18 at 21:19

1 Answers1

1

Let's look at this example:

private static int personCount = 0;

public static void main(String args[]) {

    boolean firstBool = false;
    if(addPerson(firstBool) || addPerson(true)){
        System.out.println("true");
    }
    System.out.println(personCount);
}

private static boolean addPerson(boolean b){
    personCount++;
    return b;
}

This code outputs 2 but if you change firstBool to true it will output 1.

There is conditional execution of the second function, it might make other people miss the fact that this function won't always be executed a second time. If this function wouldn't affect anything beyond the result of the if clause then it would be perfectly fine, but otherwise it's better to avoid writing the code like this or at the very least add a comment about it.

Short circuiting is an optimization, a person shouldn't have to think about all the optimizations happening in the background when he tries to understand what a piece of code does.

Edit: you can avoid short circuiting by using bitwise operations (& and |) instead of logical operations (&& and ||), that way you can ensure both functions will always be executed.

potato
  • 995
  • 11
  • 19
  • I accidentally posted before finishing writing, edited just now to complete the answer. – potato Nov 01 '18 at 21:00
  • imho its poor naming that obscures your example not the `||`. If you name the method `incrementCounter` it would be obvious that it has sideeffects – 463035818_is_not_an_ai Nov 01 '18 at 21:14
  • Yes, but it wouldn't be obvious for everyone that these side effects will sometimes not happen. – potato Nov 01 '18 at 21:14
  • I edited the example, now it has better naming, hope this demonstrates my point better. – potato Nov 01 '18 at 21:18
  • @drescherjm No, if the first function returns true then the outcome of the `||` operator will inevitably be true so what's on the other side of the `||` operator doesn't get executed. If the first function outputs false, you need to know the result of the other side of the `||` operator to determine the final result. – potato Nov 01 '18 at 21:29
  • Sorry embarrassing user error. Although that shows your point. I would try to avoid these side affects in the short circuiting. – drescherjm Nov 01 '18 at 21:30
  • @drescherjm Agree, added it to the answer. Also added a tip for avoiding short circuiting :) – potato Nov 02 '18 at 14:59