clojure-style-guide icon indicating copy to clipboard operation
clojure-style-guide copied to clipboard

Destructuring in function definition

Open nidu opened this issue 10 years ago • 3 comments

Hello.

Is it recommended to destructure arguments in function definition or in let inside the function? The first approach affects readability though shortens the function. Any thoughts about this? E.g. compare

(defn distance-to-unit 
  [{:keys [scale units-per-pixel] :as viewer} dim value])

and

(defn distance-to-unit 
  [viewer dim value]
    (let [scale (:scale viewer)
          units-per-pixel (:units-per-pixel viewer)]))

nidu avatar Jul 08 '14 05:07 nidu

It depends. Make the function arguments self-documenting for the caller. If the argument is best understood as a viewer, the arglists should show that. I would use:

;;; Good: let binding inside function
(defn distance-to-unit
  [viewer dim value]
  (let [{:keys [scale units-per-pixel]} viewer]
    ...))

;;; Also good: Fix the arglists.
(defn distance-to-unit
  {:arglists '([viewer dim value])}
  [{:keys [scale units-per-pixel]} dim value]
    ...)

In the case of "options", I would do the destructuring in the function definition (and not alter the arglists) so that the keys show up in the function documentation automatically.

ToBeReplaced avatar Jul 08 '14 12:07 ToBeReplaced

@ToBeReplaced i like the approach you provided with let. Definition is cleaner and you don't spread parameters definitions among multiple blocks.

Notice also that when you destructure parameters in function definition - you specify some kind of implicit protocol. You can say from it what keys should input arguments contain.

nidu avatar Jul 08 '14 13:07 nidu

+1 for @ToBeReplaced suggestion for clear arguments.

kliph avatar Jul 10 '14 16:07 kliph