John Pane wrote:
Part of an experiment I did during my Ph.D. research looked at the readability of unless clauses at the beginning or end of a statement. Users were significantly more accurate when the clause was at the end.

This is consistent with how I use 'unless' clauses in my own code.

I often find it more readable to have something like:

  explode unless fine && dandy;

rather than either of:

  unless (fine && dandy) {


  if (not (fine && dandy)) {

Of course, when a simple explosion can't rectify the situation,
I will sometimes use the more usual statement form:

  unless (the_gangs_all_here) {


But I will think carefully in such cases to make sure that
a straightforward 'if' mightn't be clearer, since it's
by far the more common idiom for shepherding the reader's
attention around optional code.

I do tend to restrict my use of 'if' or 'unless' as
trailing statement modifiers to those times when I
want to draw the reader's eyes to the statement being
modified, rather than to the exceptional condition being
tested.  It's a way of shifting emphasis away from the
definition of the governing condition, and it's clearly a
judgement call when to do this that reasonable programmers
might disagree about over cocktails and pizza.

However, I'm quite happy to assert that the potential
readability of a language is mildly enhanced by the options
to use 'unless' and trailing modifiers, since they take
nothing away from the language, no-one is compelled to use
them, and my cynicism about programmer skill isn't yet so great
that I feel the need to deprive good programmers of useful
tools for expressive nuance that poor programmers might find

There is arguably a case for disallowing 'else' with 'unless'
(which would also be a side-effect of having 'unless' *only*
as a trailing modifier), since otherwise a spurious desire for
lexical symmetry might encourage you to allow constructs such as:

  unless (we_can) {
  else {

which I don't believe I've ever used in any language that allows
it, mainly because it's just an inverted if...else... (whereas
an 'unless' clause is the 'else...' without the 'if...').

Reply via email to