About a year ago, I set out to learn the Elixir programming language. At the time, it was mainly so I could contribute to Pleroma, but I’ve since fallen in love with the language.
To actually learn Elixir, I did a few things. I started by reading through the official Elixir Guide, and after that, following along with the Elixir School lessons. These were useful in giving me a general idea of how the language works.
I strongly believe that the best way to learn a programming language (especially if you already know others) is to just start writing code. Exercism is a resource I’ve found to be quite useful in that process. It has a sequence of programming challenges that increase in difficulty, so it gives you a good feel for what it’s like to use Elixir to actually solve problems and helps shift your brain into thinking about problems in a functional context.
By this point, it was almost December, so I decided I was going to try to do the Advent of Code problems only using Elixir. These challenges were more difficult than the Exercism ones, but they provided the same benefit of letting me get experience actually writing Elixir and solving problems with it an isolated context, without a whole bunch of moving parts.
I knew what I ultimately wanted to do with Elixir was build web apps, so after that I went through the official Phoenix Guide which explains the overall architecture of the Phoenix framework and shows you how a bunch of common patterns and techniques for building webapps with it.
Lastly, and most importantly, I actually started building projects using Elixir. The first one I started was frenzy, an RSS aggregator I built using Phoenix and Ecto. Originally, the project was a couple hundred lines of shoddily written JS. I wrote it even before I started learning Elixir, inteding it to be a stopgap. As I was learning Elixir, I knew this project was what it was building up to, so as read things and did programming exercises, I noticed things that I thought would become useful once I got around to rewriting frenzy in Elixir.
When learning a language, there’s no substitute for actually learning, and this step was by far the most important for me. In addition to all of the algorithmic experience, and general knowledge of how to write Elixir, actually doing this project gave me the pratical knowledge of what it’s like to actually work with this language and these tools. If you’re interested in learning Elixir (or any programming language, really), my biggest piece of advice is to keep in the back of your head something concrete that you want to build with it.
After having learned Elixir, and continuing to use other languages for other projects, there are few key differences that distinguish Elixir from the other languages I know. They’re not just small differences between other programming languages, They’re what distinguishes between languages that are ‘just another language’ and languages that I find truly enjoyable to use.
Firstly, it is a functional language, but not obtusely so. It’s not steeped in terms and ideas that sound like you need an advanced math degree to understand. Of the functional programming languages that I’ve tried to learn, Elixir has been by far the easiest. It uses FP concepts, but in a way that makes it easy to learn coming from an imperative mindset. There’s no worrying about monads and functors and combinators. The hardest change is learning to use recursion by default instead of instinctively reaching for imperative constructs like loops. You don’t have to think about how specifically to structure your code so that FP concepts fit will with it. You can just write your code in a way that feels normal, and the functional aspects will become second nature.
Secondly, Elixir is fantastic for REPL driven development. It comes with a built-in REPL (iex
) which lets you quickly test code fragments, recompile your project, and view documentation.
Like Erlang, which it’s built on, Elixir runs on the BEAM virtual machine, which provides super-robust code-reloading. During the normal course of development, the only time I ever need to restart the running program is when I change the config file. Other than that, I can make a change or alteration and just run recompile
from the iex
session and nearly instantly have the all my latest changes running. Even when there are changes to data types or method signatures or compile-time constants, everything can just get swapped in seamlessly. Compared to the hot-swapping in other environments (nowhere near as good on the JVM, non-existent in many more), this is incredible.
The reason I find this makes such a big difference to the way I code is that lets me speed up my internal development loop immensely. I can very rapidly flip back and forth between writing code and testing it; I never feel like I’m being held up by the tools. I can stay in the flow much longer because there are no lengthy compile times to let me get distracted or start procrastinating.
Compared to something like iOS app development, this is a godsend. Even in small projects where incremental compiles only take a few seconds, the iteration loop is much slower. My usual development cycle goes something like this: 1) make a change, 2) hit build and run, 3) switch to my browser to glance at social media, 4) 30 seconds later switch to the Simulator and hope it’s finished launching. With Elixir projects, I’m generally just switching back and forth between my editor and the terminal and/or web browser to test whatever I’m working on. There are no intermediate steps. When I make a change, there’s no waiting for an app to launch, or for a database connection to be established, or for a network request to be made, or for config files to be read, or for anything else. Generally, it takes me more time to switch windows and type recompile
than it does for the recompilation to actually take place and the change to take effect.
Elixir is a language that I’ve come to find incredibly valuable. It’s very powerful and in the areas where it excels, it’s unique characteristics make it an extremely valuable tool. If you’re thinking about dipping your toes into functional programming, or want to try something new, or even just spend a lot of time doing back end web development,I encourage you to try Elixir.
Comments
Comments powered by ActivityPub. To respond to this post, enter your username and instance below, or copy its URL into the search interface for client for Mastodon, Pleroma, or other compatible software. Learn more.