Yes tenary:
return x > y ? x : y;
can also be used to avoid if
... else
... constructs. If the result is a Boolean
though it can often just be calculated directly, as in this case.
This is one flaw in the original examples. You should never need to do this
if (foo) {
return true;
} else {
return false;
}
As it can be replaced with just this which is clearly going to be more efficient:
return foo
So e.g. the first example (A) could be simplified in a similar way
if (pX1 > pX2 + pW2 || pX1 + pW1 < pX2) {
return false;
} else {
return !(pY1 > pY2 + pH2 || pY1 + pH1 < pY2)
}
with the whole of the second if
... else
replaced with returning the thing being tested, negated.