[Top][All Lists]

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

RE: Always using let*

From: Drew Adams
Subject: RE: Always using let*
Date: Mon, 15 Sep 2014 09:15:14 -0700 (PDT)

> > Would it be OK to always use let*?  I was just bitten by the fact
> > that with let you can not previous variables from the let
> > statement, as is possible with setq. So I am thinking about always
> > using let*, so I do not have to think about it. Or are there good
> > reasons to use let when you do not need let*?
> The most common reason is when you want to use a variable value
> in the cadr of a binding and you do *not* want to pick up the
> variable's newly bound value.  IOW, precisely the opposite use
> case of what you wanted when you were bit.
> (setq c 3)
> (let ((c  (+ c 4)) (b  (* c 42))) ; Use original C value: 3 ...)

I should point out another reason why some people, including me,
might use one or the other: To help humans read the code.

When I use `let' I communicate that none of the bindings depend
on earlier bindings.  (The bindings are independent, which does
not imply that their values are.)  In particular, when a variable
appears in the right side of a binding expression it is not a
variable that is bound by that `let' (it may be the same symbol,
but it is a different variable).

In sum: If I use `let*' then look for a binding dependency.
        If I use `let' then don't bother to look for one.

If the actual behavior of the code does not correspond to that
message then the code is bugged.  The point of such a convention
is that a reader can more easily and quickly understand the code
wrt my intention.

And yes, this does mean that I end up editing the code, changing
some `let's to `let*'s later, and vice versa, as it evolves.

I am *not* trying to minimize my effort when writing code ("so I
do not have to think about it" is misguided, IMHO).  I am instead
trying to minimize (human) reader effort - my code is not just
about me.  And yet I benefit from this more than anyone else, as
I am the main reader.  (And if others can more easily read it
then I benefit from their better bug reports.)

It's easy to write code.  It's harder to understand code you have
not written (just now) and to modify it without screwing things up.

Yes, it's a minor consideration, but it is one that affects how
I code, at least.  YMMV.

reply via email to

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