DEV Community

Thomas F Nielson
Thomas F Nielson

Posted on • Updated on

Part 1: From OOP to FP...

So I decided to go from Object Oriented Programming (OOP) to Functional Programming (FP) for my bootcamp capstone project.

For the past 10 weeks I have been fully immersed and on a learning path from Vanilla Js -> ReactJs -> Ruby -> Rails.
Graduation is coming soon and the job hunt will start even before my bootcamp completion certificate arrives. It is scary and overwhelming to say the least.

The tech job market has been crazy lately, with massive layoffs from tech giants which means the competition out there is even higher and much more experienced than me, a recent Software Engineering bootcamp grad.

I have a solid background in Mechanical Engineering, that was my previous career, but the whole point of me entering this bootcamp was to give that a spin and move towards the Dev industry instead. More coding, less hands on engineering.

I researched the industry, reached out to dev friends and LinkedIn communities and asked what is the future of development, what language is out there that is buzzin'.
All roads and tips led me to Elixir.

Background story:...
I had heard about Elixir, I was actually told by a family friend to look for that in a bootcamp. Elixir is only 10 years old so it doesn't have the popularity as legacy programming languages have, so finding an "in-person" Elixir focused bootcamp in my area was not an option. I ended up at the School I am today, learning OOP.

Elixir devs are in high demand due to the "lack" of interest on the language, what I have heard as "OOP devs scared of FP". the main difference between the two is DATA.

Elixir by being a FP, data is immutable, it doesn't change, its consistent.

Elixir example:

  • if you would like to try it, make sure you have Elixir installed to enter the console by typing 'iex' on your terminal *

1)
iex(1)> person = %{name: "thomas", age: 30}
%{age: 30, name: "thomas"}
iex(2)> Map.put(person, :age, 31)
%{age: 31, name: "thomas"}
iex(3)> person
%{age: 30, name: "thomas"}

2)
iex(1)> a = [1, 2, 3]
[1, 2, 3]
iex(2)> b = a
[1, 2, 3]
iex(3)> a = [1, 2, 3, 4]
[1, 2, 3, 4]
iex(4)> IO.inspect b
[1, 2, 3]

vs.

Ruby example:

  • if you would like to try it, make sure you have Ruby installed to enter the console by typing 'irb' on your terminal *

1)
2.7.4 :001 > person = {name: "thomas", age: 31}
=> {:name=>"thomas", :age=>31}
2.7.4 :002 > person[:age] = 31
=> 31
2.7.4 :003 > person
=> {:name=>"thomas", :age=>31}

Sure, OOP does support the same functionalities as FP by using non-destructive methods, which is what I have been taught to do the past weeks. I have to almost always make sure I'm using my non-destructive methods to process my data... Elixir erases this problem, for me to have the persistance Ruby shows above, I would have to create a new variable for that value as such:

iex(1)> person = %{name: "thomas", age: 30}
%{age: 30, name: "thomas"}
iex(2)> new_person = Map.put(person, :age, 31)
%{age: 31, name: "thomas"}
iex(3)> person
%{age: 30, name: "thomas"}
iex(4)> new_person
%{age: 31, name: "thomas"}

Now let me brake your brain 🤯

iex(1)> person = "thomas"
"thomas"
iex(2)> person = "sam"
"sam"
iex(3)> person
"sam"

So now you read this a think, wait... I thought data was IMMUTABLE and clearly data is mutating there! I CAN SEE IT!
This is where it gets a little bit more intricate, so here is my best attempt to explain Elixir's data immutability.

The variable person is initially assigned the value "thomas", but then immediately reassigned the value "sam" on the next line.

However, it's important to understand that this doesn't actually change the original value of "thomas". Instead, a new value "sam" is created and assigned to the variable person, while the original value "thomas" still exists in memory, but is no longer referenced by any variable.

🧠... Loading...

It is still an interesting concept to grasp but just like when I first started programming, I wanted to find reason on everything and once I opened my mind to accepting these foreign concepts, I started cruising.

Give Elixir a try, I'm sure you will love it!

Thanks for reading

Top comments (0)