help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: To `boundp' or not to `boundp'?


From: Drew Adams
Subject: RE: To `boundp' or not to `boundp'?
Date: Tue, 1 Sep 2015 09:42:31 -0700 (PDT)

> > I often see code like this
> > (when (and (boundp 'xxx-mode) xxx-mode) ...)
> 
> The "proper" way to do that is to use bound-and-true-p.

That certainly is *not* proper for the more general case
(and (boundp 'xxx)  xxx), where the value of `xxx' is
not necessarily Boolean or is not used only as a Boolean.

Well, it works the same, but the _name misleads_ terribly
in this case, even if the doc does let you know that the
variable value (not t or nil) is returned, when bound.

`bound-and-true-p' is defined as the exact _same thing_:

(defmacro bound-and-true-p (var)
  "Return the value of symbol VAR if it is bound, else nil."
  `(and (boundp (quote ,var)) ,var))

So it cannot be more "proper".

And in fact its definition (expansion) is much, much
clearer in the case where the var value is not Boolean.
Unlike (bound-and-true-p VAR), (and (boundp 'VAR) VAR)
is always clear and never misleads.  It makes clear
that you are testing the symbol for variableness and
returning the variable value if bound.

In sum, your "proper" is my "improper, misleading, and
useless."  Why Emacs bothered to introduce this silly
macro, I cannot imagine.  Perhaps it was to save some 
characters when the variable name is long.  Bof.

If they really wanted a macro for this, it should have
been called something that reflects what it does, e.g.,
`var-value-or-nil' or `if-bound-then-value-else-nil'.

> > I personally prefer to do it shorter
> > (when (ignore-errors xxx-mode) ...)
> > Are those equivalent?

Yes, pretty much.

> Are there any pitfalls associated with my approach?

Not really.  But it is not as readable, IMHO.  `boundp'
shouts to a reader that this is test for a variable.




reply via email to

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