dewilliam
What are your tips on reading source code containing macros?
I am new to elixir. IMO, elixir is easy to use, but reading the source code is comparatively hard due to macros, attributes …, and vscode extensions seem to be of little help resolving definitions and references.
I mean, you can neatly hide the details underlying the interface using elixir, but there can be thousands of complex ways to implement it owing to the flexibility of lang.
I am curious how experienced or any elixir programmers think about this.
First Post!
benwilson512
Most Liked
sodapopcan
Macros are always a contentious feature and that is never going to change. In our case, Elixir wouldn’t be Elixir without macros, so regardless of how one may feel about them, to write Elixir is to accept macros. Key constructs like defmodule, and def are macros! They allow Elixir to stay very close to Erlang while reducing a lot of the boilerplate that comes with writing Erlang (the first chapter of Elixir in Actions gives a good example of this). Otherwise, as you touched on, they allow the language itself to stay incredibly simple while allowing for really flexible extensibility. One of the best things about Elixir is that the language itself doesn’t change much (it’s often repeated how there will likely never be a 2.0, at least not in foreseeable future).
Personally I really like macros but I live by the advice I learned from the Ruby world: any form of metaprogramming should only be used in libraries, never ever in business logic. It does mean you need to somewhat understand macros if you want to read library code, though since Elixir macros are compile-time, running Macro.expand on any piece of macro code will give you exactly what you’re going to get at compile time. It’s an annoying extra step for sure but I know there are editor plugins that will do it for you.
D4no0
Macros are still somewhat a dark region for documentation and debugging, as this is a feature that almost no other language has, we are still learning on how to write correctly macros and document them. Phoenix uses them extensively for all the magic it does (just like rails).
In general the approach in elixir community is to read documentation, because documentation is a first-class citizen, and not code, opposite to how you would do it in golang. A good library/framework should abstract completely the inner workings from the apis it delivers to the developer.
If you want to learn about macros, I would recommend to start with the book Metaprogramming Elixir by Chris McCord, once you understand the concepts on how compile-time code generation works, it will open a lot of doors in terms of designing software.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









