In Clojure/conj talks Effective Programs and Maybe Not, Rich Hickey pointed that if we don't know something we should leave it out, i.e. we should not have keyword-nil (spec/nilable) pairs in maps.
What is the best way to deal with situations like this:
(defn person
[line]
{:person/name (name line) ;; req
:person/height (height line)}) ;; opt, height may return nil
I came up with these solutions:
Add additional arity to 'opt' fns: keyword and map, so they can conditionally assoc. Compose everything via threading macro. Applicable only if you 'own' those fns.
(defn person
[line]
(->> {:person/name (name line)}
(height line :person/height)))
Check on the call-site and assoc if some. It's really ugly imo, imagine multiple optionals - how to avoid nesting if-lets?"
(defn person
[line]
(let [ret {:person/name (name line)}]
(if-let [height (height line)]
(assoc ret :person/height height)
ret)))