If operator ternary : else null coalescing??
THIS AND THAT
or if this then that
Most of the code examples below are not programmatically correct. They only illustrate the logic.
In the plain English above, "this" is a condition and "that" is what happens if the condition is true, otherwise nothing does. In computing, the same thing is expressed as:
/* Example 1 * / if (this is true) { /* it may not be * / that happens; }
If / else
/* Example 2 * / if (this is true) { /* it may not be * / that happens; } else { /* if it isn't * / the other does; } /* Example 3 * / if (xyz is true) { /* it may not be * / the other happens; }
It should be easy to see on a 'plain English' basis that both "that" and "the other" can happen even if "this" isn't true because examples 2 and 3 aren't connected. Unlike example 1, something will happen: "that", "the other" or both.
else if
/* Example 4 * / if (this is true) { /* it may not be * / that happens; } else if (xyz is true) { /* may be true * / the other does; }
In example 4, plain English says this can happen and the other can happen but not both, and nothing may happen. You can also say if this is true OR xyz is true, something will happen, otherwise not. You can say if this is true AND xyz is true, something will happen, otherwise not. It's all about conditionals which, in plain English, people use all the time in every day conversation. If it becomes too complicated with lots of conditions, humans often resort to a written agreement like a contract, but computers can manage things easily and that's why they are so powerful where humans struggle.
The Ternary Operator
It would be good to have a job title: 'Ternary Operator'. When the Ternary Operator arrives, people sit up and listen. Unfortunately it is less like plain English so it's only a job title on computers. The Ternary Operator is just another form of if / else.
/* Normal if / else * / if (this is true) { that happens; } else { the other does; } /* Ternary Operator * / (this is true) ? that happens : the other does;
In the non plain English Ternary Operator (i) the "condition" is stated, (ii) the question is asked ? then (iii) two possibilities are stated as "this" or "the other" separated by a colon.
A 'plain English' Ternary Operator might be:
"This is true. So is it? That happens, the other happens." No-one could know what you're saying. "First you say it's true then you say it might not be, then you say "this" happens, "the other" happens. Make your mind up." There is no obvious plain English Ternary Operator.
Ternary, by the way, is one up from Binary.
The Null Coalescing Operator
I would not want a job title as a Null Coalescing Operator. Who'd want to be one? It may be the same work as a Ternary Operator but it sounds too much like Traffic Warden or Bailiff (with due respect to those valuable professions).
/* Ternary Operator * / result = (this is true) ? true : false; /* Null Coalescing Operator * / result = (this is true) ?? true;
In plain English The Null Coalescing Operator might be "The fact. This is true? Is true." with the potential for the fact not being true. It doesn't make much sense verbally and doesn't give scope for "that" or "the other." It's just shorter than:
/* if / else * / if (this is true) { /* it may be * / result = true; } else { /* if it isn't * / result = false; }
Programmatically correct versions with $variables assigned etc:
/* (1) Normal if / else * / $allegation = FALSE; if ($allegation == TRUE) { echo 'Verdict = guilty'; } else { echo 'Verdict = not guilty'; } /* (2) Ternary Operator * / $allegation = FALSE; $result = ($allegation == TRUE) ? TRUE : FALSE; echo 'Allegation = '.$result; /* (3) Null Coalescing Operator * / $allegation = TRUE; $result = ($allegation == TRUE) ?? TRUE; echo 'Allegation = '.$result;
(1) Outputs guilty or not guilty, in this instance "Verdict = not guilty" because the allegation is false.
(2) Outputs "1" or "0", in this instance "0" but would be "1" if the allegation was true.
(3) Outputs "1" but would not output anything if the allegation was false.
The speed difference is negligible in the examples above but if the required logic was, say:
if this
or else this
or else this
or else this
otherwise this
... the Null Coalescing Operator couldn't be used and the Ternary Operator would need to be 'nested' and become (i) slower and (ii) almost impossible to follow in 'plain English'. But they are rather beautiful in my opinion; more so than if / else. Which person said:
CODE IS POETRY
?