Fork me on GitHub

Good morning


\o morning


What do people think about re-using (shadowing) names in lets? I've got into the habit of naming a variable after the function that produced it. It seems 'cleaner' (no extra arbitrary names) but you can get some confusing errors if you slip up.


;; reuse name
(let [age-in-years    (age-in-years person (now))
      body-mass-index (body-mass-index person)
      smoker?         (smoker? person)]
  (risk-of-heart-disease age-in-years body-mass-index smoker?))

;; or... arbitrary abbreviations
(let [age    (age-in-years person (now))
      bmi    (body-mass-index person)
      smokes (smoker? person)]
  (risk-of-heart-disease age bmi smokes))

;; or... systematic prefix (something like a-, an-, the-, is-)
(let [the-age-in-years    (age-in-years person (now))
      the-body-mass-index (body-mass-index person)
      is-smoker           (smoker? person)]
  (risk-of-heart-disease the-age-in-years the-body-mass-index is-smoker))

;; or... systematic prefix (based on 'p' for person, in this case)
(let [p-age-in-years    (age-in-years person (now))
      p-body-mass-index (body-mass-index person)
      p-smoker?         (smoker? person)]
  (risk-of-heart-disease p-age-in-years p-body-mass-index p-smoker?))


I suppose in this case there's a good argument for avoiding the let (and this problem) altogether. But in general, I sometimes find it hard to come up with the arbitrary name.


Yeah, shadowing sometimes leads to gotchas but like you say good names are indeed scarce and sometimes hard to think of. Don't think there's a right answer.


That clojure is a "lisp-1" is one of the little things that common lispers grumble about in clojure.


I shadow at times, because I generally try to keep my functions very small, thus I can easily trace with my eyes the code flow and I can do that mental switch when reading the code to say "ah, that name now means this (and since it's scoped to the form, it won't escape)"


So, personally, I don't have a problem with shadowing. It actually can be a mental challenge to name things (as that old truism in computer science teaches us!)


Not something I want to spend much brain cycles on


Those are pretty much my thoughts, too. Cheers both.

Rachel Westmacott09:11:54

sometimes I name things foo and foo' and foo'', etc. like a new version of the same binding


that's a nice idea, keeping the name with only a very subtle difference - quite mathematical too! I like that!


@thomas am I still in Strictly? I’ve not kept up.


No, I think you failed in the dance off last week.