Book Reviews

Programming Elixir by David Thomas

ISBN: 1937785580
Publisher: Pragmatic Bookshelf
Pages: 280

Erlang and its philosophy had a tremendous impact on how I design software. It changed the way I think about scalability, error handling and other technical aspects. More importantly it influenced how I view code. Erlang was the first technology I learned that took a holistic view of systems. As the Erlang family now grows with a new language – Elixir – I set my expectations high. What can Elixir possibly offer to compete with my Erlang focus? In short: everything. And nothing.

On the surface, the first thing that strikes you with Elixir is the syntax. Learning Erlang was a smooth experience. I never had a problem with its Prolog inspired syntax. But I do understand it puts people off. Superficial elegance matters. This is where Elixir wins with its modern, familiar touch. If you've seen Ruby code you'll notice the similarities with Elixir.

A conventional syntax is a strong selling point. But there's much more to Elixir than meets the eyes in your editor. The most important feature is that the language is homoiconic. That means we can traverse and manipulate the source code of Elixir programs using Elixir itself. As a Lisp programmer, this caught my immediate attention. If you haven't experienced a language with built-in meta-programming support then trust me on this one: being able to write programs that write programs is what sets a language apart from the rest. With homoiconicity you'll never miss a feature again. You now have the same powers as the language designer; the power to grow the language to fit your programs. So you miss a feature to simplify some aspect of your program? Just implement it. Want to remove repetitive coding constructs? Sure, just do it, no matter at what level they are. It is a different programming style that takes time to master and appreciate. But once there your programs will thank you.

In this book Dave presents the whole language from scratch. It's a high-paced book that gives you a feeling for what programming Elixir is like. The book takes you from a gentle introduction of the core language all the way to meta-programming wizardry. If you've read Dave's other books (Programming Ruby, The Pragmatic Programmer), you know what to expect: a pragmatic, hands-on tutorial of exceptional quality. Given that I know Erlang, it was a quick read. If you're new to the Erlang world it takes some time to forget most of what you know about concurrency. Once there you'll appreciate the true simplicity of an actor-based system. Dave's clear explanations will help you on that path.

So far I've written a lot about the syntax and the meta-programming Elixir enables. Elixir is more than that. While Erlang has a lot of high-quality libraries, they do suffer from a long history. It's an inevitable side-effect of success; as soon as you're API gets users you're blocked. It's virtually impossible to break backwards compatibility (unless you're a major player with vendor lock-in as business model). The consequence is that Erlang's libraries have some inconsistencies. Elixir addresses that by providing new implementations. Since both languages execute on Erlang's virtual machine you get the best of both worlds; calling Erlang code is as simple as using a native Elixir library. This book introduces you to some of these common libraries and Erlang interoperability. The sample code is clear, well explained and looks idiomatic.

Another aspect of Elixir is the inspiration it takes from Clojure. This isn't really discussed in the book, but if you know your Clojure you'll notice several familiar idioms and library functions. I'd say that the core language is something like Clojure on Ruby syntax. But beneath the surface things look quite differently. As much as I love Clojure, I'd take Erlang's concurrency mechanisms before software-transactional memory any day. The strength of the Erlang/Elixir concurrency paradigm is that there's no real difference between sequential and concurrent programming. You express the natural concurrency of your domain and the run-time deals with the rest. It works surprisingly well in practice and allows you to keep your sanity as your codebase grows.

So what are the Elixir advantages to an Erlang programmer? If I had to start over again I'd prefer Elixir. Its meta-programming support is a killer feature I miss in Erlang. Its libraries look better. But is it worth learning if you already know Erlang? Right now I'm not sure. I'm convinced enough to plan on an Elixir project as soon as my life allows. The language is interesting and motivates the investment in learning. I hope to get started later this year. When I do, this book will be my companion. If you haven't ventured into Erlang land yet, do check out this book. It's a highly recommended read on a technology that simplifies your programming life.

Reviewed June 2014