Write more macros

If you’re a Clojure programmer, it’s likely that you don’t write many macros. Everybody is always warning against writing too many macros. Those people are wrong: macros are great and you should write more of them.

Here are some good uses for macros:

  1. Control structures keep your code simple.
  2. with- macros keep your resource usage tidy.

I dislike def macros, because I often need the corresponding non-def function macros and I usually have to read the source to figure out how they differ. That said, they’re okay when they look like this:

(declare foo)

(defmacro deffoo [x & args]
  `(def ~x (foo ~@args)))

You could even write a macro for defining def macros!


Comments or questions? Send me an e-mail.


Want to get these articles to your inbox? Subscribe to the newsletter: