-1
if (getNo() != null ? !compareNo(getNo(), b.getNo()) : getNo() != null) {
  return 100;
}

Assuming getNo() is not null, does this mean that return 100 runs if !compareNo(getNo(), b.getNo()) evaluates to true?

What happens to the last expression (after the :)

Shahriar
  • 13,460
  • 8
  • 78
  • 95
stumped
  • 491
  • 3
  • 6
  • 19
  • 3
    Regardless of the outcome, you should really consider expanding this into multiple checks or multiple lines. Less lines of code does not mean better. This piece of code means I have to read it 10 times in order to understand what it's saying. That's bad from a maintenance point of view. – SurfMan Jan 29 '18 at 19:57
  • 2
    Possible duplicate of [What is the Java ?: operator called and what does it do?](https://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do) – Bernhard Barker Jan 29 '18 at 20:00
  • I'm trying to understand someone else's code, so i agree that it's not easy to read. – stumped Jan 29 '18 at 20:00
  • @SurfMan The biggest problem here is that the "else" of the ternary is the same boolean as up front--this could be perfectly readably written as `if(get(No) != null || !compare...` – chrylis -cautiouslyoptimistic- Jan 29 '18 at 20:03
  • I think I get it now, the "if" part is returning a boolean, and the "else" part is doing that too. – stumped Jan 29 '18 at 20:04
  • @chrylis I think you mean `getNo() != null && !compareNo...`. You don't want the second part evaluated if the first part is false. – Bernhard Barker Jan 29 '18 at 20:05
  • @chrylis - probably not. It seems to me that `getNo()` probably reads a number from some kind of stream, or other structure. So consecutive calls to `getNo()` probably don't return the same thing. I think OP needs to make sure that whatever happens, `getNo()` gets called twice. – Dawood ibn Kareem Jan 29 '18 at 20:10
  • Its necessary to use `code` tag to separate it from other text – Shahriar Jan 30 '18 at 08:37

1 Answers1

1

What a mess. Let me refactor things so I can see what's going on:

if (getNo() != null ? !compareNo(getNo(), b.getNo()) : getNo() != null) 
{
  return 100;
}

is the same as

Boolean mainCondition = getNo() != null ? !compareNo(getNo(), b.getNo()) : getNo() != null;
if (mainCondition) 
{
  return 100;
}

is the same as

Boolean mainCondition;
if (getNo() != null) {
  mainCondition = !compareNo(getNo(), b.getNo());
} else {
  mainCondition = getNo() != null;
}
if (mainCondition) 
{
  return 100;
}

but if getNo() is null, then we find ourselves in the else condition, which will set mainCondition to false, so we will not return, i.e. a simpler way to write this is:

if (getNo() != null) {
  if (!compareNo(getNo(), b.getNo())) 
  {
    return 100;
  }
}

or

if (getNo() != null && !compareNo(getNo(), b.getNo())) {
  return 100;
}

The answer is yes: if !compareNo(getNo(), b.getNo()) is true, then the original statement will return 100.

As for what happens to the part after the :, it will not be evaluated unless getNo() is null, however if getNo() is indeed null, then we already know the outcome, so it's quite redundant to include it there.

nvioli
  • 4,137
  • 3
  • 22
  • 38
  • So if getNo() != null, then "!compareNo(getNo(), b.getNo())" evaluates, and if it evaluates to true, then you execute the return, and if it evaluates to false, it doesn't do anything? – stumped Jan 29 '18 at 20:14
  • Pretty much! If it evaluates to false, then we would evaluate the last part of the ternary operator (after the `:`), but we already know that evaluates to false since what's there is exactly the same as the original condition. So the ternary will evaluate to false, so the `return` will not run. As you said, it doesn't do anything. – nvioli Jan 29 '18 at 20:18