3 ms·
Anti-pattern: Overuse and misuse of macros. (Or perhaps not a "pattern", since the manifestations are so varied?) For non-Elixir folks, imagine something that
by Terr_ 2y ago
Anti-pattern: Overuse and misuse of macros. (Or perhaps not a "pattern", since the manifestations are so varied?)
For non-Elixir folks, imagine something that looks like a regular function-call where expressions are evaluated and the results are passed in... but in reality all arguments are symbolic inputs to something that emits new code.
For example, you might see this unassuming line in your editor:
describe_combo(a(foo),b(bar))
But what might actually be compiled is:
# What might actually get compiled
# Runtime results like "a(1)+b(2)=3"
"a(" <> to_string(foo) <> ")+b(" <> to_string(bar) <> ")=" to_string(a(foo)+b(bar))
And if someone in your codebase is an evil misanthrope:
foo = "haha I changed your local variable"
IO.puts("surprise, suckers") # Prints to console
kick_puppies()
Macros are actually part of the core DNA of the language and underpin how some of its syntax works... but with great power comes great potential for WTF.
- josevalim 2y agoElixir macros are hygienic, so you need to be more explicit if you want to override variables as above. You also need to require macros before using them, but once you do you can kick all the puppies! There is also a guide on meta-programming anti patterns: https://hexdocs.pm/elixir/macro-anti-patterns.html https://hexdocs.pm/elixir/macro-anti-patterns.html (and unnecessary use is one of them).