Subject: Re: bug#67669: 29.1; Drop text suggesting using `and' to replace `if'

From: Xiyue Deng <[email-masked]>
Date: Thu, 07 Dec 2023 09:49:13 -0800

Eli Zaretskii <[email-masked]> writes:

>> From: Xiyue Deng <[email-masked]>
>> Cc: [email-masked]
>> Date: Thu, 07 Dec 2023 01:08:51 -0800
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> > AFAIU, the relevant fragment is:
>> >
>> >      The expression looks like this:
>> >
>> > 	 (if interprogram-cut-function
>> > 	     (funcall interprogram-cut-function string (not replace))))
>> >
>> >      If an ‘interprogram-cut-function’ exists, then Emacs executes
>> >   ‘funcall’, which in turn calls its first argument as a function and
>> >   passes the remaining arguments to it.  (Incidentally, as far as I can
>> >   see, this ‘if’ expression could be replaced by an ‘and’ expression
>> >   similar to the one in the first part of the function.)
>> >
>> > I don't see anything wrong here.  Can you elaborate on why you think
>> > the text in parentheses is inaccurate or incorrect or unclear?
>> 
>> Sorry I wasn't very clear.  I was referring to the part "... could be
>> replaced by an ‘and’ expression similar to _the one in the first part of
>> the function_."  But when I tried to search for `and' in the example
>> code of that section I found no `and' usage at all, which confused me a
>> bit.
>
> I think it means this part earlier in the node:
>
>        (if (fboundp 'menu-bar-update-yank-menu)
>             (menu-bar-update-yank-menu string (and replace (car kill-ring))))

To be super nitpicking, the text said "the one in the _first part_ of
the function", while this code snippet you quoted is the second
statement in the code example.  If we look at the old code (which is
currently marked as ignore in the texi source), the first part is:

  (and (fboundp 'menu-bar-update-yank-menu)
       (menu-bar-update-yank-menu string (and replace (car kill-ring))))

Which IMHO lets the text make more sense.  But anyway.

>
>> It was not until I looked at the texi source that I noticed that
>> the original example code did use `and' which I linked in my first
>> email, and it was later replace by the current one from Emacs 22 which
>> no longer used `and'.  Thus I would consider the text in the parentheses
>> you quoted to be obsolete.
>
> I don't think so.
>
>> And of course as I mentioned in the previous email, IMHO it is probably
>> not a good advice to use `and' to emulate `if'.
>
> I disagree.  I think the discussion of 'if' and 'and' there is
> entirely appropriate and educational, and I personally do use these
> interchangeably in some situations.

Agreed on the point of being educational, and of course in the end
coding style is mostly a matter of personal preference.  However, I
would argue a bit that in such case we wouldn't need `if' or `when' at
all as both can be emulated by `and' and `or' if you are careful enough.
On the other hand, `if' and `when' should exist for a reason.  I quite
like this suggestion[1] that `if' and `when' are used to guard side
effects, whereas `and' and `or' are used for pure functions.
Interestingly the current example you quoted changed from using `and'
(as in the old version I quoted) to `if' probably based on this
reasoning.  Similarly I wouldn't suggest to replace `if' with `and' in
the original example around the text we are discussing:

  (if interprogram-cut-function
      (funcall interprogram-cut-function string (not replace))))

as we are also guarding a side effect here.

Wdyt?

[1] https://emacs.stackexchange.com/a/14204

-- 
Xiyue Deng