Wait, what?!”. “Aaah! I get it!”. “WTF?!?”. “I don’t get this. -Me

These were my thoughts when I was reading Elixir’s Getting Started Guide some years back. Seeing the last chapter of those guides has been nothing but a dream to this day. I did though, get a glimpse of what Elixir is about.

Elixir is this functional and concurrent programming language that runs on the Erlang virtual machine (BEAM). It was released in 2011 as an R&D project of Plataformatec (for years, I read this as Platformatec!) by José Valim.

From an object-oriented programming(OOP) point of view, it’s fancy, yet powerful! While everything in Ruby is an object, in Elixir, everything is an expression, how about that.

Somewhere deep inside me, I knew I wanted to dive deep into this new thing and never resurface.

Procrastination got the best of me.

Here’s the story of how I learned to code to code Elixir and Phoenix:

Induced Motivation To Learn To Code

For some time now I’ve been looking for my next Ruby on Rails project to work on, Twitter was the last place I posted to announce I’m looking for a Rails job.

I’ve had a few interviews but not the right match yet, which means I had all the time in the world to myself.

I have a good Ruby friend, Stefan Wintermeyer, who since last year, has been trying to proselytize me into the Elixir ecosystem. Stefan is a Ruby, Elixir, Rails, Phoenix Framework, Asterisk, web performance expert. He’s like a swiss-army knife of web apps with lots of experience so when he first proposed this, I had even more reasons to consider learning Elixir.

When Stefan retweeted my Twitter job post, I wrote back to thank him and he replied:

You are more than welcome. Please think about learning Elixir and Phoenix while waiting for a Rails job.

The premise was that Stefan would allow me to work on one of his Elixir/Phoenix projects.

“OK! This is it. The time has come!”, I said to myself.

So I replied:

OK please give me up to 3 weeks and I’ll get back to you with what I know about Phoenix!

A Safe Paradigm Shift with Mindfulness

I knowingly jumped into uncharted waters when I told Stefan to give me 3 weeks to get back to him. Learning a new programming language and a new framework for that matter in three weeks seemed like a herculean task.

Elixir is a functional programming language, not close to anything I’m used to.

As the vagabond mindfulness practitioner that I am, I resorted once again to meditation expecting an arduous cerebral escapade with Elixir and Phoenix.

With a paradigm shift this huge, it’ll be hard to maintain focus. While reading some Elixir documentation, my mind would always keep analysing and comparing what I was reading with an OOP programmer’s mindset. This was detrimental and not what I wanted.

Mindfulness is what would spearhead me into a flow state. Practising mindfulness has countless benefits to our mental well-being. It empowers you to dump past and future thoughts and focus on the present.

Every so often, I practice it depending on what I’m dealing with at that given moment.

Having previously gone through some of Elixir’s documentation, there was still a bunch of things I remembered even if I didn’t know how they worked then. I then jumped onto Elixir School, which I recommend as a first access point to Elixir. Going through Elixir School gave me enough knowledge of the language to pick a book on Phoenix.

So I got a Phoenix book.

I was very mindful in every step with the Phoenix book I was reading, unperturbed by OOP, classes, messages, inheritance, everything I had learned in the past, how I’ll adapt to “functional” thinking in the future and things happening in my environment.

I’d read my book and anytime my mind wandered, I was quick to notice that I had derailed and gently got myself back on track to what mattered at that given moment; learning Elixir and Phoenix.

By the way, the book in question to which I coded along is Phoenix in Action by Geoffrey Lessel. I was lucky enough to get this book for free from Geoffrey himself. And to be honest, it’s the best introduction to a framework I’ve ever read. Think Michael Hartl’s Rails Tutorial, but on steroids. It teaches the very best of practices with Phoenix and shows you all the stuff you need to understand in the most vivid and fun way possible.

While coding along I made sure to keep frequent commits, perhaps it may help you follow along in case you buy the book. You could also just go through my commits. Since they’re chronological, you’ll get a feel of how the process of building apps with Phoenix is like.

Chapter after chapter, I was able to grasp the wirings of not only Elixir apps but how Phoenix works. And tell you what, I can’t get enough of it!

Falling In Love Again

The pros of Elixir are a cliché now. I’ll not mention them here. Coming from Ruby on Rails, I noticed Phoenix has some nifty goodies.

A few caught my attention. One of them is the idea of Plugs. Plugs are behaviours that accept a connection request. Phoenix is full of plugs in layers, requests go through them and get transformed along the way. This is a very smart idea if you think about it. With an Endpoint being where all requests start, you can insert plugs anywhere along the chain of other plugs to modify or access data. In plugs, you can set cookies, implement authentication and run functions before controller actions among other things as the request runs through the chain of endpoints. You have full control of where, how and what to do along which part of the request cycle you want. Ingenious!

Phoenix in Action, Geoffrey Lessel

Phoenix’s architecture is very clean and explicit. The pattern is comparable to Hanami’s, except that Hanami is built with an OOP language. It sets very clear and strict boundaries between domains, your entities are loosely-coupled.

Phoenix stays out of the way of your application. You can imagine Phoenix as being an extension or plugin to your application. It’s not your application, it’s a part of your application.

Phoenix in Action, Geoffrey Lessel

The framework is fast! I’ve dabbled with lots of frameworks and in the context of building applications, I’ve never seen a request take microseconds(μs) to complete! When I saw this in my console, I was like: “Wait a minute. What the heck is that?!”. It’s speed guys. Most people will probably never need this much swiftness but it’s nice to know it’s there if/when you’ll need it.

Changesets! Oh my! The changeset concept, if you have a background like me, at first, sounds weird and complicated. With changesets, it’s very simple to manipulate data you send to the database. Changesets allow filtering, casting, validation and definition of constraints when manipulating structs. Here let’s assume structs are our data models. Here’s an example of a changeset from Ecto’s (Ecto is the database wrapper and query generator for Elixir, it’s what Phoenix uses by default) documentation:

defmodule User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :name
    field :email
    field :age, :integer

  # Info above left for context.
  def changeset(user, params \\ %{}) do
    |> cast(params, [:name, :email, :age])
    |> validate_required([:name, :email])
    |> validate_format(:email, ~r/@/)
    |> validate_inclusion(:age, 18..100)
    |> unique_constraint(:email)

The cast here is a filter of what data should come through with a request. Any other thing not listed will be rejected. The cool thing with changesets is when you pass something to it that’s not in the cast, it won’t error on you, it’ll just take what’s listed and work with it and ignore the rest. How’s this not something to fall in love with. The other functions are pretty self-explanatory. Changesets spring to action when you’re inserting or manipulating data.

Phoenix also comes with code reloading and a nice UI, you don’t need to configure anything! You can get very productive with Elixir and Pheonix right off the bat!

I could go on and write more cool details about Phoenix but I’d rather you give Elixir and Phoenix a try yourself.

A productive web framework that does not compromise speed or maintainability. – Phoenix Framework

Take that tag line literally.

The one thing I haven’t gotten used to yet though, is the fact that I have to constantly type iex -S mix to preload an app into IEx and mix phx.server to start a server. These commands, among others, are so unrelated visually that I find it slightly hard to remember at will. I did, however, figure out a way to turn them into “siblings” and short enough to commit to memory to keep my productivity unhindered so I wrote about how to create an alias with arguments. The pattern I wrote about could be used for other “hard-to-remember” commands and is applicable to other activities in terminals as well.

Time Report

You’re probably wondering… “Did he do this in one sitting?”. There answer is “No.”. I’m human. I did this over six days with about thirty minutes into the next day. And I have a detailed time report if you’d like to know what exactly I learned in how much time.

Here’s a summarised time report:

I spent a little over 5 hours on covering the basics at Elixir School, and even caught some tiny typos and submitted PRs to fix them. Something that’d be hard if you’re not mindful.

Elixir is such a delight to work with and Phoenix’s architecture has been designed with such ingenuity that it’s easy to reason about everything and be productive. Both tools are filled with so much common sense, you’d think… “Where have I been all this while?”

The teams behind both Elixir and Phoenix have done a great job!

The Road Ahead

Am I an Elixir/Phoenix expert? Not even close. I’ve got a long way to go. I can see myself becoming a well-versed Elixir/Phoenix developer quicker than I did with Ruby on Rails. That’s guaranteed! I can affirm that you’ll like Elixir/Phoenix if you take a small effort to study it, and you should, even if it’ll just be for fun.

Apropos the future, I don’t plan on halting my relationship with my Elixir any time soon. I’m looking to build on what I already know. These are the books on my list that I want to get to next:

Tip: Some of these books have discounts on Elixir Forum and the total cost after discounts won’t break the bank.

At the time of this writing, there are a few things about Phoenix I don’t know yet, namely channels, testing and how to write APIs. Phoenix in Action already covers this.

I believe I’ve pretty much covered the fundamentals of the framework. My goal is to have this covered by the end of the week and get practical with it. I want to peruse multiple print resources and get a diverse opinion from the experts.

Last Update: January 09, 2024