Why use Elm if it's hard?
Tags: elm, architecture, functional, frontend
Published: 2020-08-17 17:45:36

Elm is a programming language for frontend applications that uses pure functions to compile into highly-optimized and safe JavaScript.

# Not Mainstream But Prevailing

Functional programming is probably not the first programming pattern you came across. If we can write frontend applications just fine without it, why do we need to use it?

While it may not be a popular idea, many of the concepts used in Elm is being imitated by other frameworks to achieve the same result. This is because of the predictability it delivers, the maturity in its concepts bring about a stable and usable system. Who hasn't once felt frustrated with JavaScript for its spontaneous performance?

# More Modular Than Monolithic

There was a time when we were reluctant to abandon the monolithic backend architecture, but microservices and serverless architecture are already becoming common use cases. In the case of frontend applications, things had been stuck in one big irreplaceable chunk of JavaScript for a while.

A modular architecture refers to a code stack that consists of minimal modules where majority of the system can be swapped out or plugged in to work with another system.

Elm encourages the use of modular architecture because it fundamentally cannot be "mixed and matched" with many different internal components and libraries, but allow ports and external communication to happen through a programmatic interface.

# Platform Independent Design

Web applications are one of the most cross-platform compatible systems available, thus they are used everywhere including a huge number of mobile apps.

Elm is mostly a frontend language and framework. Adopting a stack that includes Elm can be beneficial even for embedded devices. Native Android and iOS functions can be triggered using [Cordova](https://cordova.apache.org/) and platform-specific functions can be invoked by a background daemon process.

Contrary to popular belief, in embedded devices with limited capabilities, it is more favourable to use languages such as Python for most of the system processes, as opposed to C or C++ which may need to be cross-compiled as the compilation can be very intensive and several GB huge. Only performance critical components should be compiled to the machine.

# Pure Functions

As mentioned, Elm uses functional programming, meaning all data passing through are *immutable*, and contains no side-effect. Data must be passed from one function to the other without the concept of sharing states in different scopes. The functions have no impurities, they are purely what they seem to be, the *same input guarantees the same output*.

What this really means is that the program is 100% predictable within the scope of the entire Elm application. If something does not function as programmed, it is considered a bug in the compiler. This is incredible because with only one built-in function, Elm can render your entire HTML DOM using *lazy* diff comparison. If there are no changes to most of the page, there was never a need to refresh 99% of DOM elements for that 1% difference! The result is a lightweight and fast interface that can run anywhere.

What's more, all these features comes without putting together a whole list of frameworks and plugins, which eventually becomes a burden to maintain.

# Conclusion

This is why a loosely-coupled and modular system is so advantageous. Doing so not only allows a single codebase in every platform, but also naturally gives way for taking the strengths of each and every module, taking the best tool for the right application. What's more important is that the codebase remains stable among all the diversity.