This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-16
Channels
- # aleph (3)
- # announcements (14)
- # babashka (16)
- # beginners (85)
- # calva (6)
- # cider (9)
- # clojure (42)
- # clojure-australia (8)
- # clojure-europe (30)
- # clojure-nl (4)
- # clojure-uk (29)
- # clojuredesign-podcast (7)
- # clojurescript (25)
- # cursive (4)
- # data-science (1)
- # datomic (31)
- # emacs (1)
- # events (1)
- # fulcro (16)
- # instaparse (2)
- # java (37)
- # kaocha (3)
- # malli (3)
- # meander (19)
- # membrane (7)
- # off-topic (13)
- # pathom (4)
- # pedestal (10)
- # re-frame (17)
- # reveal (3)
- # rewrite-clj (1)
- # ring (9)
- # shadow-cljs (17)
- # spacemacs (2)
- # sql (34)
- # tools-deps (88)
- # vim (4)
månmån
morning
morning
morning
Morning
What do people think about re-using (shadowing) names in let
s? 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. https://andersmurphy.com/2019/03/08/lisp-1-vs-lisp-2.html
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!)
sometimes I name things foo
and foo'
and foo''
, etc. like a new version of the same binding