Beautiful Code

Elixir is perhaps the first functional language to support Lisp-style macros with a more natural syntax. This feature, like a template for code, is not always the right tool for everyday users, but macros are invaluable for extending the Elixir language to add the common features all web servers need to support.

For example, web servers need to map routes onto functions that do the job:

 pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :protect_from_forgery
 end
 
 pipeline :api do
  plug :accepts, ["json"]
 end
 
 scope "/", MyApp do
  pipe_through :browser
 
  get "/users", UserController, :index
  ...
 end
 
 scope "/api/", MyApp do
  pipe_through :api
 
  ...
 end

You’ll see this code a little later. You don’t have to understand exactly what it does. For now, know that the first group of functions will run for all browser-based applications, and the second group of functions will run for all JSON-based applications. The third and fourth blocks define which URLs will go to which controller.

You’ve likely seen code like this before. Here’s the point. You don’t have to sacrifice beautiful code to use a functional language. Your code organization can be even better. In Phoenix, you won’t have to read through inheritance chains to know how your code works. You’ll just build a pipeline for each group of routes that work the same way.

You can find an embarrassing number of frameworks that break this kind of code down into something that is horribly inefficient. Consultancies have made millions on performance tuning by doing nothing more than tuning route tables. This Phoenix example reduces your router to pattern matching that’s further optimized by the virtual machine, becoming extremely fast. We’ve built a layer that ties together Elixir’s pattern matching with the macro syntax to provide an excellent routing layer, and one that fits the Phoenix framework well.

You’ll find many more examples like this one, such as Ecto’s elegant query syntax or how we express requests as a pipeline of functions that compose well and run quickly. In each case, you’re left with code that’s easier to read, write, and understand.

We’re not here to tell you that macros are the solution to all problems, or that you should use a DSL when a function call should do. We’ll use macros when they can dramatically simplify your daily tasks, making them easier to understand and produce. When we do build a DSL, you can bet that we’ve done our best to make it fast and intelligent.

Effortlessly Extensible Architecture

The Phoenix framework gives you the right set of abstractions for extension. Your applications will break down into individual functions. Rather than rely on other mechanisms like inheritance that hide intentions, you’ll roll up your functions into pipelines, where each function feeds into the next. It’s like building a shopping list for your requests.

In this book, you’ll write your own authentication code, based on secure open standards. You’ll see how easy it is to tune behavior to your needs and extend it when you need to.

The Phoenix abstractions, in their current incarnation, are new, but each has withstood the test of time. When it’s time to extend Phoenix—whether you’re plugging in your own session store or doing something as comprehensive as attaching third-party applications like a Twitter wrapper—you’ll have the right abstractions available to ensure that the ideas can scale as well as they did when you wrote the first line of code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.7.240