[sclug] Standard of language

Ed Davies sclug at edavies.nildram.co.uk
Fri Sep 25 08:57:54 UTC 2009


Funny, I replaced some JavaScript a bit like that the other day.  It
was doing some validation on a parameter and was like:

  if (valid-in-one-way) {
    // That's good, do nothing.
  } else
  if (valid-in-another-way) {
    // That's also good, still do nothing.
  } else {
    throw "The sky's falling";
  }

It happened that changes elsewhere simplified the conditions so
that:

  if (!valid-in-one-way && !valid-in-another-way)
    throw "The sky's falling";

became readable.  I choose this form purely for compactness, not
because I thought there was anything particularly wrong with the
original which I'd use again in appropriate circumstances.

As for the "== false" bit: that rather depends on the language.
If the only possible values are true and false then I'd prefer
"!" but if there's any chance of another value then an explicit
test for false seems like a good idea to me, not depending on
the other value being interpreted as true.  In general I differ
with many C/C++ programmers in not liking the punning of non-
boolean values as booleans.  Where appropriate I'd write:

  while (*s != '\0') ...

rather than

  while (*s) ...

Ed.




More information about the Sclug mailing list