Ternary Operator vs or Operator vs and Operator
The Ternary Operator and the OR Operator
The ternary operator is not reasonable for a situation that returns true or false; if the goal is to get a result of true or false from your decision, it is better to simply evaluate the statement itself.
For example, if it reduces to this statement:
1
|
|
then you are better off just simply using some_condition
by itself, as it will evaluate to either true or false.
If, however, you have a statement that evaluates such as
1
|
|
What you may be asking for and really wanting is the OR Operator, rather than the ternary:
1
|
|
If this doesn’t immediately look familiar, look at this:
1
|
|
Is the same as:
1
|
|
On the other side of the spectrum, when you have
1
|
|
You will want to use the AND operator:
1
|
|
Rather than using the ternary with a false return like:
1
|
|
Remember, the point of either is having an evaluated statement that has the possibility of being false (or nil) as well as the possibility of being true.
If your evaluated statement never has both possibilities, then these aren’t the operators for you at this moment.
In conclusion, we use the ternary operator when we have a simple if..then
operation that does not return either true
or false
itself. We use the or
and and
operator when we need one evaluation or the other. When using the ternary operator, we don’t use it to return true
or false
, there are easier ways to do that.
The discussion starts here, but continues in our Google Plus Post!