Ike Naar said:
Ike Naar said:
On 08/24/2013 03:05 PM, Sharwan Joram wrote:
[...]
if ( NULL == parameters[parametercount]){
This is what's known as a "Yoda conndition"
<
http://en.wikipedia.org/wiki/Yoda_Conditions>. I know that a lot of
programmers like them, and for somewhat valid reasons, but personally I
find them jarring and unnecessary. Personally, I'd write that as:
if (parameters[parametercount] == NULL) {
Does it matter? The == operator is symmetric, (X==Y) == (Y==X).
If (X==Y) is jarring and unnecessary, then, for symmetry reasons
(Y==X) is unnecessary and jarring.
[...]
Yes, it matters *to me* (and to plenty of other people). "==" is
commutative as far as the language is concerned, but code should be
written both for the compiler and for the human reader.
X==Y is a poor example for this, since X==Y and Y==X are pretty much
equally readable.
X and Y were meant to be placeholders for arbitrary subexpressions.
Sure, but my argument (whether you agree with it or not) doesn't apply
to arbitrary subexpressions.
I prefer to treat (X==Y) as a mathematical formula (stating that
subjects X and Y have equal values), rather than shoehorn it into
an English sentence that has X, but not Y, as its subject.
How about Yodaness when both operands are non-constant, as in
(sin(alpha) == cos(beta)) ?.
Would it now matter which operand goes on the left side?
The names "alpha" and "beta" might suggest that "alpha" goes first, but
in general it probably doesn't matter -- though it might depend on the
circumstances.
What if it's unknown which of the operands is constant?
Consider the expression (pNeedle == pHaystack).
If we assume that pNeedle is variable and pHaystack is
constant, this expression is, supposedly, perfectly readable.
But if it turns out that pNeedle is constant and pHaystack is
variable, the same expression suddenly becomes Yoda? So Yodaness
is not a syntactic property of the expression itself, but also
depends on additional knowledge that may or may not be obvious from
looking at the expression and its context, and that even may change
at runtime?
Strictly speaking, constness *is* a syntactic, or at least compile-time,
property of an expression, though it's going to depend on contact
outside the expression.
I'm not sure what to make of (pNeedle == pHaystack); normally one search
for a needle *in* a haystack.
Certainly you can construct a continuum of cases, with comparing a
computed value to a literal constant on one end, and comparing the
results of two clearly symmetric expressions on the other. I admit this
is rather vague, and I don't have a well-defined rule for when the order
is important to me.
I think the main purpose of the == operator is to test whether
two values are equal. For that purpose it does not really matter
whether operands are constant or not: it's equality we're
interested in, not constness. Taking constness into consideration
just complicates things unnecessarily; so let that not influence
the way equalities are notated.
But *why* do you want to know whether they're equal? What if, in the
problem domain, what you're really doing is asking a question about the
value of some expression (is it equal to 42?), and not asking a question
about 42 (about which you already have complete knowledge)? Putting the
constant on the right can provide a useful clue to the reader.
By the way, does Yodaness apply to other commutative operators as
well? Which one of (2 * f(x)) and (f(x) * 2) is Yoda, and why?
It matters far less for multiplication. I'm not sure I can clearly
articulate why.
It might help to re-phrase the question to one that is more symmetric
in its operands, e.g. "are ... and ... equal?".
Logically true, but that doesn't make the Yoda form any clearer to me.
I know "==" is commutative, but (0 == strcmp(s1, s2)) is still clumsy
*to me*.
[snip]
Actually the Yoda form can be slightly more readable when the
function call has a long argument list, as in
if (3 == scanf("%g %g %g", &latitude, &longitude, &elevation))
one can immediately see that the returnvalue of scanf is compared to 3,
because the 3 is close to the scanf, whereas in
if (scanf("%g %g %g", &latitude, &longitude, &elevation) == 3)
the 3 and the scanf are far apart.
Ok, that's another valid reason for using Yoda conditions. I'd still put
the 3 on the right side myself; I might split it across two lines if I
thought the distance was a problem, or I might store the result in a
temporary.
But setting that aside for a moment (and avoiding the "!strcmp()"
variant):
It would never occur to me to write:
if (42 == x)
rather than
if (x == 42)
if it weren't for the possibility of confusing "==" and "=".
Would you write (42 == x) yourself? Do you really find it *just*
as natural as (x == 42)?