I find that my clojure apps get structurally coupled very rapidly due to the lack of a data API. I have maps with keys that have names which, if mistyped, because exceptions to be thrown or errors. I also notice that it's easy to make mistakes when destructuring a list (for example maybe you destructure the wrong part of the list).
Coming from the Java world, normally I use my IDE to help me to get the "right" data out of minimal, unordered data objects. But clojure map passing seems to be the opposite paradigm from this.
How do clojurians code defensively in the absence of a type system or ide code completion?
Write validators functions for your "schemas" (keys but also type of values etc.) then use thm inside pre- and post- conditions in your code -- since their syntax is little known here is a quick refresher:
(defn foo [x y] ; works with fn too
{:pre [(number? x) (number? y)]
:post [(number? %) (pos? %)]}
(+ (* x x) (* y y)))
They rely on assert
and hence can be disabled. (doc assert)
for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With