You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does anyone else have an opinion on the better style in these situations?
Situation A.
;; style A1
(defnf
[{:keys [one two three}]
(+ one two three)
;; style A2
(defnf
[{:keys [:one:two:three}]
(+ one two three)
Situation B. (Namespaced keywords using an alias)
(require '[my.named.space :as space])
;; style B1
(defnf
[{:keys [two three]
::space/keys [one]}]
(+ one two three))
;; style B2
(defnf
[{:keys [::space/one:two:three]}]
(+ one two three))
;; style B3
(defnf
[{:keys [::space/one two three]}]
(+ one two three))
;; style B4
(defnf
[{:keys [my.named.space/one two three]}]
(+ one two three))
I strongly prefer style 1 (A1 and B1) because:
The main point of destructuring is to introduce symbols into the current scope in a concise way. In my opinion it looks wrong to introduce a symbol by writing a keyword. To me it looks almost as strange as writing (fn [:one :two] one).
Destructuring with :keys is the only place that I can think of where you can introduce a symbol using a keyword. So for consistency with all the other places where you can introduce a symbol we shouldn't do it here either.
Some reasons I can think of to prefer style 2 (A2 and B2) are:
Style A2 allows better searching for keyword usage across a project (biggest advantage in my opinion as it's more than stylistic).
Styles B2 and B3 are the most concise of the options in situation B (concision being a core benefit of destructuring). But style B3 is strange because it mixes keywords and symbols, therefore style B2 is preferable.
If style B2 is preferred in situation B, then for consistency we should use style A2 in situation A. Beyond consistency reasons, it reduces the need to refactor if we change that function to accept some namespaced keywords.
Does anyone know if there are many clojure projects that use styles A2 and B2?
Does anyone have an opinion either way or is it just me?
The text was updated successfully, but these errors were encountered:
Yes, definitely. Whenever I see destructuring with keywords, I always have to do a double-take and remind myself it is actually valid, even if very unusual. 👀
Hi,
Does anyone else have an opinion on the better style in these situations?
Situation A.
Situation B. (Namespaced keywords using an alias)
I strongly prefer style 1 (A1 and B1) because:
(fn [:one :two] one)
.:keys
is the only place that I can think of where you can introduce a symbol using a keyword. So for consistency with all the other places where you can introduce a symbol we shouldn't do it here either.Some reasons I can think of to prefer style 2 (A2 and B2) are:
Does anyone know if there are many clojure projects that use styles A2 and B2?
Does anyone have an opinion either way or is it just me?
The text was updated successfully, but these errors were encountered: