Enchanting Elixir

Why this language?

Elixirs

Enchanting Elixir

Why this language?

Previously, I’ve dicussed my experiences and that I’ve been exposed to a lot of different languages and development environments. When I ran across Seven Languages in Seven Weeks I was intrigued by functional programming. I’d never seen anything like it. Somewhere after this I heard about Elixir and ended up buying Programming Elixir.

Wait, what?!?

To be honest the language baffled me. I had a lot of moments where I would think “okay, I can understand this” followed by “err, what is going on here?” and a lot of “what just happened and how did that result in this?”

I was stuck on multiple function headers — “Why are they all called the same thing?!?” I struggled with differing function return types: {foo, bar} and {:error, “some text”} coming from the same function call. Pattern matching looked like incomprehensible punctuation in odd places. Coming from other languages I assumed = was an assignment and thus didn’t understand it as the match operator.

A new look

Due to an unfortunate accident, my inital dive into Elixir ended before I understood these ideas. However, during the past few years I kept hearing about Phoenix and the its amazing performance. I’d read about the easy concurrency (something that had bit me hard on an iOS project) and the incredible scalability. I was intrigued enough that this year I decided it was time to try again.

Thankfully, all of the things that baffled me the first time around just clicked and made sense. Instead of puzzlement, I experienced understanding. Now my moments were more “wow, that’s very elegant” or “I can’t believe how easy this makes things.” Finally, I understood what makes Elixir special.

In comparison

  • Memory management — It’s just not a thing. Sure, you want to use memory efficiently, but in general there is no mental overhead in managing memory. To be fair, when using NSString with App Kit or UI Kit, I never managed memory, either. Good libraries take a lot of rote overhead away from you.

  • OTP — It’s nice to have a rock-solid supervisor. While you want to write dependable code, it’s nice to know you have backup that will make things right.

  • Pattern matching — Once my brain grasped pattern matching it felt like everything that baffled me before suddenly fell into place. Pattern matching explained the multiple function headers, differing return values, and all the “weird” punctuation around the match operator or function parameters. When I understood how pattern matching can assign variables to data inside maps or other structures I couldn’t believe how easy this is versus any other language I’ve used.

  • with…else — This was a great discovery. Instead of the if…else pyramid of doom, this elegantly ensures success conditions are met and errors can be dealt with while making code readable.

Tools

When I first looked at Elixir I didn’t understand the tools available or how to use them: I had a terminal and a text editor. I got stuck on “how am I supposed to know what is happening?” My stubborness of not wanting to type code line by line into IEx held me back. One day I saw a video of someone using IEx this way and watching the results step by step. Then things clicked.

The next step was discovering VSCode with proper plugins and configuration. Now I had help with function parameters, documentation on hover, formatting, and IEx in the handy terminal.

Conclusion

I’m sure I’ve missed things here, but I hope to have touched on several of the things that I’ve enjoyed while discovering this language. I’m glad I took a second look and got a chance to see OTP, Phoenix, Ecto, LiveView, LiveBook, and many other great libraries.

I aspire to keep growing in knowledge and have a chance to write Elixir with a great team.

See also