The benefits of drinking the Elixir

Preface

This story is really just about my experiences. If it sounds narcissistic, well, that’s because it is. It’s one of my first forays into writing on technical topics as well, so it’s going to be a little sloppy, and I plan on editing it over time.

Back story

Back in January, I took it upon myself to learn yet another new language, Elixir. It’s not that I mastered a previous language and couldn’t learn anymore, or that I decided this is the language I need to master as, in the words of Trump, “It’s gonna be Yuuuuuge!”, I just get bored easily of using the same language and decided it was time to switch it up!

Why functional programming?

Immutability!

The functional programming paradigm helps reinforce a number of techniques that generally take significant experience to learn in an object-oriented language. Not being able to mutate state freely is the biggest one for me. Now when I’m making modifications and additions in my day to day work, I recognize that my affects can have far reaching implications if I’m not careful.

It may sound odd, since you would think learning an immutable by default language would lead me to incorrectly start assuming all languages are immutable by default. That’s why I also keep up with JavaScript in my free time. There’s no better language out there to remind you what damage careless mutations can cause.

As a side-effect of a mostly mutation free language (puns!), the functions created are generally idempotent. Honestly, you’ll never know how easy it can be to test functions until you’ve made them idempotent. After that, you’ll have a hard time going back the grueling chose of testing side-effect ridden functions!

Why Elixir?

Why I chose Elixir is probably pretty obvious if you look at the site. I’ve worked with Ruby in the past and enjoyed it’s ease of use, and found Elixir to carry that same spirit with it. On top of that, Elixir is built on Erlang, is dynamically typed, comes with a powerful package manager, has a strong web framework, and comes with a slew of practical features that seem to only strengthen it’s friendliness to Developers. One of my personal favorites is DocTest, especially since it makes it’s existence obvious, and it’s extremely helpful for keeping your documentation up to date when updating functionality (it won’t help for new stuff, but hey, nothing is perfect).

In addition to immutability, the language itself has a number of interesting features: chaining with pipelines, processes that make sense, and fault tolerance.

Chaining

Chaining commands logically using |>, the pipeline operator in Elixir, is astounding. As with any function chaining, it helps to prevent crazy looking nested calls. Unlike in some other languages (looking at you JavaScript), everything is explicit; I should note that there are global variables that can be used internally to the functions called in a pipeline, but they are generally constants anyway.

Processes

In Elixir, which is compiled and run on Erlang’s Beam VM, processes are not only not bad, they’re encouraged! To paraphrase/quote Dave Thomas from his amazing book on Elixir:

processes in Elixir are like objects in an object-oriented system
Dave Thomas. Programming Elixir 1.2. The Pragmatic Bookshelf, LLC.

To directly quote the Elixir page on matter:

Elixir’s processes should not be confused with operating system processes. Processes in Elixir are extremely lightweight in terms of memory and CPU (unlike threads in many other programming languages). Because of this, it is not uncommon to have tens or even hundreds of thousands of processes running simultaneously.

It’s hard to think of any other language, outside the Erlang family, that can successfully duplicate this behavior out of the box.

Fault tolerance

On this topic I honestly don’t have too much experience. There is a slogan for Erlang, and is carried over to in Elixir, “let it crash”. To be sure, this does not mean that you should purposefully have you processes crash. What it’s trying to get across is that the system is so fault tolerant that if a process crashes and the system is setup properly, there is no problem with restarting the crashed process to a known good state and continuing to run as if there wasn’t an issue. It can help you sleep at night, knowing your system is safe from self-destruction, which from my understanding is the bane of a startup/MVP’s existence.

Conclusion

While I’m still learning Elixir, it’s proven helpful in understanding a great many topics that I only briefly heard/read of before. At this time, I’ve only written one, useless library for giggles, and to better understand the language. I plan on continuing to study this, and will hopefully be able to build something more meaningful in the future. In the mean time, go learn functional programming!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.