Macro Rules

Before we write more complex macros, we need to review a couple of rules to temper our expectations. Macros give us awesome power, but with great power comes great responsibility.

Rule 1: Don’t Write Macros

You may hear this rule touted loudly when talking with others about metaprogramming. Often it’s unfounded, but before we get too carried away, we have to remember that writing code to produce code requires special care. It’s easy to get caught in our own web of code generation, and many have been bitten by reckless complexity. When taken too far, macros can make programs difficult to debug and reason about. There should always be a clear advantage when we attack problems with metaprogramming. In many cases, standard function definitions ...

Get Metaprogramming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.