If and unless

Well, the reason I like having both these
was expressed by someone else a day or two
ago. Too lazy to look it up now.

But to paraphrase (and over-generalize):

I use unless for exceptional or incidental logic, and if
for expected or mainstream logic.

Thus when I was working on a little program (which needs
to be refactored, ) to download a file and edit it,
I realized there was no need to re-upload the file if it
was not changed.

So I wrote:

upload unless unchanged

So someone asked, why not:

upload if changed

Well, since the whole point of downloading the file is
to edit it, I expect that 99% of the time the file will
be changed.

But it’s not just that. Writing it this way (to me)
emphasizes that point.

I use ‘unless’ for “the road less travelled,” to invoke
Robert Frost. (The analogy breaks down immediately. :slight_smile:

In fact, I’d say that it’s always just the branch that
occurs less frequently; it’s the one we consider weird
(or the one we want to ignore) even if it happens a lot.
But that’s just me.

And this, of course, is separate from the question of
whether we use the pre- or post- form of ‘if’ (or ‘unless’).

Hal

Hmm. I would have chosen the latter for this case.
It seems more natural.
I find my self using unless when I want to avoid
an explicit ! or not in the boolean test.

I like the your reasoning of using the 99% rule,
but I think if I were to try to follow it, I would
soon slip into using what made more since for
the particular case that what the rule said.

···

On Thu, Sep 12, 2002 at 06:39:49AM +0900, Hal E. Fulton wrote:

So I wrote:

upload unless unchanged

So someone asked, why not:

upload if changed

Well, since the whole point of downloading the file is
to edit it, I expect that 99% of the time the file will
be changed.

But it’s not just that. Writing it this way (to me)
emphasizes that point.


Jim Freeze

Programming Ruby
def initialize; fun; end
A language with class

I agree with Jim.
In my code the available predicate dictates
the use of if and unless.

Less code is better: Maximize LOCNW!

s.

···

On Thu, 12 Sep 2002 03:39:15 GMT, Jim Freeze jim@freeze.org wrote:

On Thu, Sep 12, 2002 at 06:39:49AM +0900, Hal E. Fulton wrote:

So I wrote:

upload unless unchanged

So someone asked, why not:

upload if changed

Hmm. I would have chosen the latter for this case.
It seems more natural.
I find my self using unless when I want to avoid
an explicit ! or not in the boolean test.

I also agree. I use “unless” simply to avoid ! or not.

Regarding the 99% rule, I do not pay that much attention. The situation
is different in a multi if-elsif statements, where I usually put the more
likely cases towards the beginning.

Regards,

Bill

···

=============================================================================
Stefan Schmiedl s@xss.de wrote:

On Thu, 12 Sep 2002 03:39:15 GMT, > Jim Freeze jim@freeze.org wrote:

Hmm. I would have chosen the latter for this case.
It seems more natural.
I find my self using unless when I want to avoid
an explicit ! or not in the boolean test.

I agree with Jim.
In my code the available predicate dictates
the use of if and unless.

Less code is better: Maximize LOCNW!

s.