[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

`when' vs. `and' / `unless' vs `or'

From: Garreau, Alexandre
Subject: `when' vs. `and' / `unless' vs `or'
Date: Tue, 16 Oct 2018 23:05:11 +0200
User-agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu)

Hi, TL;DR: I’m asking for opinions and individual practice,

I’ve heard `when' and `unless' are for side-effects, because if cond
fails they return nil (or maybe they don’t return in some lisps or
previous emacsen?).

However, there are situations where I could use respectively `and' or
`or' instead, where these works and are not used for side-effects (there
may be none) but only for return value.  Especially anywhere a boolean
is expected, as then nil has full meaning (the condition, or maybe the
body, failed).

Such as: (or stuff (when cond body) stuff) vs. (or stuff (and cond body)
stuff), or (and stuff (unless cond body) stuff) vs. (and stuff (unless
cond body) stuff), but also as a condition for an `if', or even another
`when' or `unless'. Then what should I do?

Should I ban these and only use `when' and `unless' for side-effects,
except when I’m feeling like I should use a progn inside the `and'/`or'
(so I don’t bloat code)… or maybe should I use that progn, to convey
some meaning (this one at least truly is for side-effects)?

Or should I use `when' and `unless' as much as possible, and use `or'
and `and' only when the first argument could be a meaningful non-nil
thing to return?

Maybe there are even other practices.  I’m interested in your opinions
and your practices, as both manual and docstrings are not verbose enough
about this issue.


Personally, I was always seduced by how primitive and basic or
short-circuiting logical constructs, and never understood why these are
implemented in terms of conditionals, rather than the opposite (probably
because in imperative programming short-circuiting is quite relevant and
not trivial and the important thing of the if?), since these are backed
down in primitive logic.

So I tend to like to use them when I can, but I fear this is `(or
cryptical pedantic "a too-smart approach")', as first time I saw them
used like that I bugged a bit (but I can imput that to how and where I
was wrongly teached to see these).

PPS: Sorry for verbosity, one day I need to find a technique to fix that
PPPS: I fear I already asked something among those line a long time ago
but lost both the backup and memory of what was said.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]