Why should you try Elm?

Álvaro Sánchez
6 min readApr 21, 2022

--

Elm logo looks cool!
Elm is nice, but not very popular, should I use it?

Users in today’s world are used to have what they want quickly. As a result, web applications are becoming more complex. For this and other reasons, the demand for web developers is getting higher and higher, and there are a lot of good of tools available to help these ingenious workers. React, Vue, Angular, Svelte… They are all popular for the same reason: allowing you to quickly and reliably create web applications. Not for nothing was React at the top of the most popular libraries list last year. So, given the abundance of popular libraries and frameworks on the market today, why should you try Elm?

The Thinking Man
Choosin’ ain’t easy

In this article we will see 5 reasons why you should use Elm as a library for your next project.

1. Learning Elm will make you a better programmer

Actually, learning functional programming is what will make you a better programmer. And Elm is the best choice for this. It’s the most easy one in the family of pure functional languages, and it practically forces you to write clean, concise, and scalable code. Elm masks the intimidating concepts of functional programming such as pattern matching, immutability, and the “almighty monads in a very friendly way. If you don’t believe me, consider the following example:

Divide by zero? No thanks!
A quick glance at Elm

See? We defined the division function above that receives two integers and returns the Maybe type, indicating this function may fail (because it can be divided by zero). Then we print the result on the screen based on this success condition. We don’t leave things to chance, in Elm we work with exception handling explicitly. Isn’t that cool? Well, with Elm your code will always look this clear, and that’s something to take in count. And even if you don’t intend to use functional programming on a regular basis, learning Elm will help you to write more reusable, debuggable, and understandable code.

2. Elm’s syntax is great

Nowadays we’re used to use things like classes, objects, flow control structures, brackets, semicolons, and so on. However, in 1973, ML appeared, with a peculiar syntax that provided pattern matching for function arguments as well as currying. Moreover, despite the fact that none of the ML family languages have entered the “mainstream” category, the truth is that the languages that use ML syntax are very comfortable to program with. To see it in action, let’s compare ML with one of the most popular languages of the 70s, C:

Old languages indeed
A comparison between contemporary languages

As you can see, here we have two functions that do the same thing, give the factorial of a given number. Both are good and have their pros and cons. But to my personal taste, I prefer the elegance offered by ML. So, as ML, Elm decides to move away from all the things mentioned at the beginning of this paragraph. Here’s how the factorial function looks in Elm:

Nice!
👌

It’s completely understandable that you’ll find it hard to work with a very different syntax at first (It was for me). But, in the long run, you’ll end up feeling more at ease with Elm’s ML-like syntax.

3. Elm’s type system is a joy

Seriously, it’s pure joy. It’s so damn good that Elm ensures that you won’t get exceptions at runtime. Too pretty to be true? Yes, but it’s true. Elm’s type system is so strict that leaves no room for loose ends. And it is because of this that Elm has my favorite feature: its incredibly friendly compiler error messages. Take a look at one of the examples provided on the Elm website:

Very friendly 🙂

Here a record (something similar to an immutable object) is created with two fields: first and last. isOver50 is a function that takes a person and returns a Bool indicating if the value of his age field exceeds 50. Now, when passing the hermann record in answer and after Elm infers the types, it’s telling us that we are trying to get the age field from the hermann record, which is non-existent. So as you can see, the compiler catches and warns you about all the lines of code that might give you an error before you even test it in your environment. No more undefined is not a function!

4. Its unidirectional flow of data saves you headaches

Let’s be honest, human beings are disorganized by nature. Many times we do things quickly and on the fly, and then we don’t remember where we put the tools that we now need to get a job done again. And this is also extrapolated to our code.

In the early days of the web, where sites were small and users were undemanding, not much importance was attached to how the code was organized. But we quickly began to realize how difficult it was to maintain and scale those old codebases. To solve this, many development architectures emerged, and the truth is that Elm’s is one of the best out there.

The Architecture of Elm

Elm follows an MVU-based architecture. Our interactions with the frontend send messages that Update the Model. Then the View is rendered according to the new state. A very simple but powerful architecture. By keeping the state of the application centralized and only modifiable through explicit events, it becomes much easier to know where to look in case something does not work as expected. It’s true, Elm wasn’t the only one to make strides in this type of architecture, but in my opinion, Elm’s one of the best right now.

5. Knowledge does not take place

Perhaps the most common reason, but it should not be overlooked. We all have one or two favorite programming languages, but it’s important not to limit yourself entirely. We must keep in mind that there are numerous ways to solve a problem, and usually most of them are valid. Furthermore, there is no better way to get a different perspective on a problem than to learn a new programming language, especially one that deviates from our usual standards (OOP). So, if none of the preceding points persuaded you to try Elm, at the very least do it for the sake of knowledge, and you won’t be disappointed.

Well, if you’ve made it this far, it’s because you’re eager to start learning Elm, right?😃 If so, I’d recommend that you start with its official guide and the Elm Programming book. You can also follow @evancz on Twitter to support him and get the latest news!

There are many more aspects to Elm that we could mention but would make this article too long. In any case, if you liked it, let me know and share it on your social networks so that it helps more people, I would really appreciate it!

Cheers! 😎🤙

--

--