Discussion:
`when' vs. `and' / `unless' vs `or'
Garreau\, Alexandre
2018-10-16 21:05:11 UTC
Permalink
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.

PS:

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.
Drew Adams
2018-10-16 22:13:16 UTC
Permalink
Do whatever you like. ;-) Different people use different styles.
There is no standard or even a convention. Common Lisp
suggests some conventions, but there too there is nothing
official.

I use `when' and `unless' when I want to show a human
reader (mainly me) that the code doesn't use or care about
the return value. I don't use them when the returned `nil'
could be important.

I use `and' or `or' when the return value is significant.
(With `not' as needed.)

I use `if' when the true part is a single sexp. If necessary,
to respect this I flip the true and false parts (negating the
condition).

I use `cond' when I have two or more sexps for each of
the true and false parts or I have more than two conditions.

I use `case' when the conditions would just test the
same symbol for equality against different values.

I don't use `pcase' much. Nothing against it, really.

Yes, the way I use `if' etc. can mean that I change which
form I use as the code evolves. I'd rather spend more
time fiddling with the code, in order to have the result
be easier to understand (for a human).

YMMV.
Garreau\, Alexandre
2018-10-16 22:38:54 UTC
Permalink
Post by Drew Adams
Do whatever you like. ;-) Different people use different styles.
There is no standard or even a convention. Common Lisp
suggests some conventions, but there too there is nothing
official.
What is this? it does interest me.
Post by Drew Adams
I use `when' and `unless' when I want to show a human
reader (mainly me) that the code doesn't use or care about
the return value. I don't use them when the returned `nil'
could be important.
Okay, so what about when you care about the result, but absolutely not
about the return value of the condition?

(unless cond body) vs (or cond body) -> `or' might imply you might want
to return cond.
Post by Drew Adams
I use `and' or `or' when the return value is significant.
(With `not' as needed.)
I use `if' when the true part is a single sexp. If necessary,
to respect this I flip the true and false parts (negating the
condition).
So unless it is simple, you always write your big-conditions if as an
imbrication of both `or' and `and'? isn’t this actually less easy to
understand (even if easier to read with indentation)?
Post by Drew Adams
I use `case' when the conditions would just test the
same symbol for equality against different values.
And then comes typecase and its unability to have several types per
clause, or when you want to use an arbitrary test function for case, the
more likely and particular case of when you want that test function to
be `equal', the case where in your bodies you always apply something to
the aforementioned expr (so it looks redundant)…
Post by Drew Adams
I use `cond' when I have two or more sexps for each of
the true and false parts or I have more than two conditions.
…or the case each condition clause is actually to apply something to the
same thing.

I’ve refactored all this with my own typecase, case*, case-equal,
case-apply, case-test… Where case-apply is based on case-equal which is
based on case*, and typecase on case-test. I still need to refactor all
this into a big general-case function upon everything is based by being
a particular case of it.
Post by Drew Adams
I don't use `pcase' much. Nothing against it, really.
YMMV indeed :)
Drew Adams
2018-10-16 23:21:06 UTC
Permalink
Post by Garreau\, Alexandre
Okay, so what about when you care about the result, but absolutely not
about the return value of the condition?
(unless cond body) vs (or cond body) -> `or' might imply you might want
to return cond.
I don't understand the question. I use (or ...) when I want to return
the value of a disjunction, where that value, if non-nil, might be
any non-nil Lisp value. If (or cond body) returns cond then it's
because cond is non-nil? What's the question? (And there is no
"cond" as in condition versus "body" as in body. `or' just returns
the first non-nil arg, or nil if none is non-nil.)
Post by Garreau\, Alexandre
Post by Drew Adams
I use `and' or `or' when the return value is significant.
(With `not' as needed.)
I use `if' when the true part is a single sexp. If necessary,
to respect this I flip the true and false parts (negating the
condition).
So unless it is simple, you always write your big-conditions if as an
imbrication of both `or' and `and'? isn’t this actually less easy to
understand (even if easier to read with indentation)?
Dunno what you mean. Isn't what less easy to understand than
what? What's a "big-condition"?

I use `if' when the code cares about the return value, just like
`or' and `and'. If the code doesn't care about the return value,
and if there is only one condition, then I use `when' or `unless'.
Garreau\, Alexandre
2018-10-17 00:02:28 UTC
Permalink
Post by Drew Adams
Post by Garreau\, Alexandre
Okay, so what about when you care about the result, but absolutely not
about the return value of the condition?
(unless cond body) vs (or cond body) -> `or' might imply you might want
to return cond.
I don't understand the question. I use (or ...) when I want to return
the value of a disjunction, where that value, if non-nil, might be
any non-nil Lisp value. If (or cond body) returns cond then it's
because cond is non-nil? What's the question?
I was just saying in the first case it will return nil while in the
second it will return cond (the first arg), so if you don’t want to
return cond (because it’s not what you want) you’ll end up with (and
(not cond) body).
Post by Drew Adams
(And there is no "cond" as in condition versus "body" as in body. `or'
just returns the first non-nil arg, or nil if none is non-nil.)
It was just to use the same arg names, it doesn’t change anything as you say.
Post by Drew Adams
What's a "big-condition"?
A condition of more than a single sexp, such as (and cond cond) instead
of just cond (which may be (= x 1), so that it would have instead (and
(= x 1) (= x 2)) for instance).
Post by Drew Adams
I use `if' when the code cares about the return value, just like
`or' and `and'. If the code doesn't care about the return value,
and if there is only one condition, then I use `when' or `unless'.
Dunno what you mean. Isn't what less easy to understand than
what?
(if (or cond cond) then else) than, canonically (and (not (and (or cond
cond) then)) else) (not sure about a simplified version), which is the
way to write it without an `if', as you said you didn’t use it if the
condition was more than a single sexp.
Drew Adams
2018-10-17 00:16:14 UTC
Permalink
Post by Garreau\, Alexandre
(if (or cond cond) then else) than, canonically (and (not (and (or cond
cond) then)) else) (not sure about a simplified version), which is the
way to write it without an `if', as you said you didn’t use it if the
condition was more than a single sexp.
As I (think I) said, I use (if (or cond cond) then else), if THEN is a single sexp.

If THEN is not a single step I don't bother to use
(if (or cond cond) (progn THEN1 THEN2...)) else).

---

BTW, for transforming sexps with `not', `and', and `or', for readability
or for performance reasons, I sometimes use `notandor.el'.

https://www.emacswiki.org/emacs/NotAndOr

Also, package `el-search.el' has similar possibilities (and many other
good things).
Garreau\, Alexandre
2018-10-17 01:25:43 UTC
Permalink
Post by Drew Adams
Post by Garreau\, Alexandre
(if (or cond cond) then else) than, canonically (and (not (and (or cond
cond) then)) else) (not sure about a simplified version), which is the
way to write it without an `if', as you said you didn’t use it if the
condition was more than a single sexp.
As I (think I) said, I use (if (or cond cond) then else), if THEN is a single sexp.
Oh sorry! I misunderstood in fact, I must be tired.
Post by Drew Adams
BTW, for transforming sexps with `not', `and', and `or', for readability
or for performance reasons, I sometimes use `notandor.el'.
https://www.emacswiki.org/emacs/NotAndOr
Nice! will it be in elpa one day? ^^

Althought your Package Requirements is a list instead of a list of list
so that blocks automatical installation using package-install-file…
Post by Drew Adams
Also, package `el-search.el' has similar possibilities (and many other
good things).
Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
I’ve dreamt of it I think.
Drew Adams
2018-10-17 01:58:33 UTC
Permalink
Post by Drew Adams
BTW, for transforming sexps with `not', `and', and `or', for readability
or for performance reasons, I sometimes use `notandor.el'.
https://urldefense.proofpoint.com/v2/url?u=https-
3A__www.emacswiki.org_emacs_NotAndOr&d=DwIFaQ&c=RoP1YumCXCgaWH
vlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=kI3P6ljGv6CTHIKju0jqInF6AOwMCYRDQU
mqX22rJ98&m=__OVOB-
tJoLihmI72sPPpAIReKeWG19lS1GOjTGgDGo&s=sXzSuHMBNHG6coBRhaVI6lraH
1aMD3bro_kwvYDmC14&e=
Nice! will it be in elpa one day?
No, no need.
Althought your Package Requirements is a list instead of a list of list
so that blocks automatical installation using package-install-file…
Corrected. Thx.
Post by Drew Adams
Also, package `el-search.el' has similar possibilities (and many other
good things).
Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
I’ve dreamt of it I think.
Remember to. ;-)
Garreau\, Alexandre
2018-10-17 09:00:18 UTC
Permalink
Post by Drew Adams
Post by Drew Adams
BTW, for transforming sexps with `not', `and', and `or', for readability
or for performance reasons, I sometimes use `notandor.el'.
https://urldefense.proofpoint.com/v2/url?u=https-
3A__www.emacswiki.org_emacs_NotAndOr&d=DwIFaQ&c=RoP1YumCXCgaWH
vlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=kI3P6ljGv6CTHIKju0jqInF6AOwMCYRDQU
mqX22rJ98&m=__OVOB-
tJoLihmI72sPPpAIReKeWG19lS1GOjTGgDGo&s=sXzSuHMBNHG6coBRhaVI6lraH
1aMD3bro_kwvYDmC14&e=
Nice! will it be in elpa one day?
No, no need.
I was asking because as I find it useful other might :P but I understand
as it’s experimental until it’s tested to be proved that much useful,
to, for instance, being used in another package, you prefer to delay ^^
Emanuel Berg
2018-10-17 09:49:15 UTC
Permalink
Post by Garreau\, Alexandre
I was asking because as I find it useful
other might :P but I understand as it’s
experimental until it’s tested to be proved
that much useful, to, for instance, being
used in another package, you prefer to delay
^^
I'm sure you are aware of MELPA as well as the
EmacsWiki where people can put whatever they
like. When I came to Emacs I was unaware of
both so what happened was I just put all my
Elisp on my personal web pile. Now I've lost
touch with it just a bit so I don't have the
enthusiasm to go thru it all to decide if
anything is worth putting somewhere else.
--
underground experts united
http://user.it.uu.se/~embe8573
Michael Heerdegen
2018-10-17 15:42:34 UTC
Permalink
Post by Garreau\, Alexandre
Post by Drew Adams
Also, package `el-search.el' has similar possibilities (and many other
good things).
Wow it seems cool, I’ll try it out when I’ve got time if I remember to.
I’ve dreamt of it I think.
If you happen to try it, I'm curious for your feedback, especially if
you find bugs, something doesn't work as expected, the documentation
isn't helpful enough, etc.


Thanks,

Michael.

Loading...