Avoiding Common Pitfalls

As with any powerful tool, it’s easy to cut yourself. Throughout my Elixir experience, a few common mistakes have appeared that are easy to avoid in retrospect but can wreak havoc on your code base over time if ignored. Let’s find out ways to avoid getting caught in your own web of code generation.

Don’t use When You Can import

One of the most common mistakes newly minted metaprogrammers make is treating use as a way to mix in functions from other modules. This tempting idea conflates the concept of a mix-in from other languages, where you can include methods and functions from one module into another. In Elixir, this pitfall looks something like this.

Consider a StringTransforms module that defines a number of string ...

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.