Pune Elixir Meetup — May 2019

Wanna know my experience of getting introduced to elixir after working with Ruby and Rails for more than 2.5 years?

Why in the first place?

Why did I think of learning elixir?

I had been working on a project since 2016 and had a seamless backend application running on Rails. I was happy with the system’s performance until 2018 when the application really started to scale. The increasing Sidekiq jobs, cron jobs running over each other started choking up the whole system. There was a real need to run the jobs parallelly and without using up most of the system resources.

It’s a bad feeling when your production application chokes.

Apart from scaling the Infrastructure, there was a need to perform some very high optimizations at the application level. To make code and query optimizations and to make the application really perform the processing of jobs in parallel. Therefore, I started searching for alternatives to integrate into the system to increase its efficiency.

It’s then I came across Elixir which has already created a lot of buzz in the Ruby community due to its performance and fault tolerance.

The first thing that anybody learns about a language for the first time is its syntax. I was still thinking in Ruby when I started learning elixir’s syntax. Being from the Ruby background It was a mike drop moment as my confidence swayed up to very high levels after learning the basic syntax of elixir and seeing the similarities between the two.

The reason was, Elixir’s Syntax is so similar to Ruby.

The method definition (Ruby vs Elixir)

Method Definition in Ruby vs Elixir

Variable Assignment (Ruby vs Elixir)

Variable Assignment in Ruby vs Elixir

Hash vs Map (Ruby vs Elixir)

Hash in Ruby vs Maps in Elixir

Array vs List (Ruby vs Elixir)

Array in Ruby vs List in Elixir

String Interpolation (Ruby vs Elixir)

String Interpolation in Ruby vs Elixir

These were some of the similarities that made elixir very similar to Ruby.

But soon after digging a bit, I was struck with the realization of how little I know about Elixir and the architecture it is based upon.

The Paradigm Shift !!

The Realization

The similarities between Ruby and Elixir remained at the syntax level only. These two languages are different from each other in many ways starting from the architecture level.

For example:

The difference in Looping (Ruby vs Elixir)

Notice, how in this example we are calling each method ON an Array Object in Ruby. Whereas, we are Passing an array in each function of a module in Elixir.

The difference in function calling (Ruby vs Elixir)

Similarly, we are calling reverse method ON a string object in Ruby. Whereas, we are passing "hello" (a string) in the reverse function of a module in Elixir.

The differences that were immediately noticeable were:

No Classes No Objects Modules being used in the current file are explicitly included. State/Data Structure is passed into functions as arguments. Immutability

The Two Paradigms

I realized that one of the major differences between the two languages is the programming paradigms that these languages work on. And that is the reason for the above-mentioned differences between the two languages.

Ruby works on Object Oriented Programming ( OOP ) Paradigm whereas Elixir works on Functional Programming ( FP ) Paradigm.

Go ahead and read the definitions on Wikipedia for these two paradigms. Those definitions are understandable if you know the two paradigms but may not give a clear understanding if you do not know these concepts already.

Let me explain them in my own words. Essentially, OOP and FP both paradigms have the same goal of creating programs but their way of achieving their goal is different. They say that a program consists of two important components:

Data (which is handled by data structures in our programs) Behaviour (which we normally write in methods/functions)

OOP works on the concept of bringing the Data and Behaviour at the same place. This place in OOP is called an Object.

FP says that the data and the behavior of a program are different and should be kept separately. This makes it completely different from OOP.

Since we cannot keep the data and the behavior in FP at a single location, we cannot create objects and hence no classes in FP. Rather, FP works on a different pipeline like model where you have a pipeline of functions where you provide a data structure as an input and get a modified data structure as the output.

Functional Programming in Elixir

In contrast to Ruby where OOP is followed by creating Classes and instantiating them by creating their Objects and calling methods on those objects to change their data.

Elixir follows the functional programming paradigm by creating:

Modules Functions (Pure functions ideally)

Let’s take an example to understand this. Suppose you have a bicycle shop. You want to drop down the rates of those bicycles by $100 whose manufactury date is more than 6 months old.

Let’s see how this can be done in ruby.

Ruby Code

To do this in Ruby, you would have to keep cycle_id , cost and manufacture_date (Data) and their methods reduce_cost and old_bycicles (Behaviour) in a class called Behaviour . This class tells us how bicycles (Objects) will be created and how their data will be stored.

Ruby Code

To reduce the cost of an array of bicycles you will call .each method ON an Array Object of bicycle objects and eventually call reduce_cost method ON each bicycle object.

To do the same thing in Elixir.

Elixir Code

Here, instead of creating a class or objects, we are using a module Bicycle in elixir to enclose a set of function(s) bicycles_with_reduced_cost .

We are passing an array of old_bicycles and passing it to bicycles_with_reduced_cost function which is processing it and returning a NEW array of bicycles in new_bicycles . The new_bicycles will be a different array from the old_bicycles .

Immutability in Elixir

You will be able to compare it with them old_bicycles and see the differences. This is because of immutability in Elixir. This way of checking the differences and seeing if a certain method was called is not possible in Ruby natively because the objects in Ruby are mutable and can change themselves. Therefore, the only way to check if a method was called in Ruby is to check the flow of the code whereas you could easily compare the data structure before and after calling a function in Elixir to verify the same.

There are a lot of other interesting features in Elixir that I found will be really helpful for Ruby developers.

Pipe Operator Pattern Matching Scaling through processes OTP (Open Telecom Platform)

But let’s keep the discussion on these for some other time. :)

What is the final Outcome?

Should we say goodbye to Ruby (Or OOP) and adopt Elixir (Or FP) for the future?

Well, the answer to this question is that it depends on the kind of application you are building. I saw a wonderful line in this blog and Quoting because it deserves to be mentioned. ;)

Michael Fogus, author of “Functional JavaScript”, suggests in his blog post “FP vs OO, from the trenches”. That when he deals with data about people, FP works well, but when he tries to simulate people, OOP works well.

❤️ Like, Share or Leave A Comment!

If you enjoyed this post, don’t forget to give it a 👏🏼, share it with a friend you think might benefit from it and leave a comment! Stay tuned for more exciting blogs on Flutter, Elixir, React, Angular, Ruby, etc.

Thanks !!