The pros and cons of metaprogramming
Let’s take a look at the router example again:
blog/lib/blog_web/router.ex
defmodule BlogWeb.Router do # .. pipeline :browser do plug :fetch_session plug :accepts, ["html"] end scope "/", BlogWeb do pipe_through [:browser] get "/posts", PostController, :index end end
In the preceding code example, we can easily define a /posts
route that accepts html
requests and routes requests to the PostController.index/2
action. The code is concise, standardized, and easy to digest.
Here are the pros of metaprogramming. Metaprogramming can do the following:
- Hide the complexity of implementation under a simple and concise layer of abstraction. This is often referred to as a DSL.
- Increase developer productivity because the amount of code that needs...