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!


About the author: My name is Miikka Koskinen. I'm an experienced software engineer and consultant focused on solving problems in storing data in cloud: ingesting the data, storing it efficiently, scaling the processing, and optimizing the costs.

Could you use help with that? Get in touch at miikka@jacksnipe.fi.

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