Fork me on GitHub
#clojure-uk
<
2020-11-16
>
jiriknesl05:11:22

Good morning

djm07:11:43

👋

lsnape09:11:11

\o morning

rlj11:11:24

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.

rlj11:11:56

;; 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?))

rlj11:11:36

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.

cddr14:11:16

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.

cddr14:11:24

That clojure is a "lisp-1" is one of the little things that common lispers grumble about in clojure. https://andersmurphy.com/2019/03/08/lisp-1-vs-lisp-2.html

dharrigan08:11:31

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)"

dharrigan08:11:46

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!)

dharrigan08:11:02

Not something I want to spend much brain cycles on

rlj10:11:45

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

dharrigan09:11:24

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

jasonbell12:11:43

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

thomas12:11:17

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

danm16:11:45

Morning!