Functional Programming is hot right now. To quote Tim Riley, “FP is hot AF.” But, as a Ruby developer, Object-Oriented Programming (OOP) is a way of life. I tend to reach for classes that represent object properties and messages to solve problems. It makes sense, to me, as everything in Ruby is an object.
Functional programming is an alternative paradigm to OOP. Instead of applications being a composition of objects passing messages, our applications compose functions.
As a developer, I struggled to learn OOP when learning to program. The idea of learning a new paradigm was terrifying.
Naturally, I wrote off functional programming for the “real developers.” After all, I was terrible at math in college. Functional programming, as taught to me, had a strict correlation to mathematical functions.
In my mind, I wasn’t smart enough to learn functional programming
In the community, a new language began to receive a lot of attention. Elixir, created by a former Rails Core Team member, ha syntax inspired by Ruby.
The majority of pros I heard at first dealt with speed. It’s no secret; Ruby isn’t the fastest programming language. “Speed” out of the box was appealing. However, I’ve (personally) yet to run into an unsolvable performance issue in Ruby. Elixir, built on the powerhouse that is Erlang, is fast. But, with speed not being an issue for me, I quickly wrote it off.
However, I started hearing more pros about Elixir being a functional programming language. People were stating they found their code easier to read, maintain, and reason about.
I saw a few Ruby developers build things in Elixir. I was interested but scared that I’d have to give up everything I knew and worked so hard to learn.
Exploring Functional Programming
I spent some downtime a while back exploring a couple of traits of functional programming, pure functions & immutable data. The more I learned about these two principles, the more I began to dislike the way I wrote my code.
People were talking about side-effects, or the lack thereof, in functional programming. I was reading how immutable state and pure functions make for code easier to test. My interest was piqued.
However, I work as a Ruby on Rails developer during the day. I didn’t have any side projects coming in that would allow me to pick up something like Elixir. I had one over a year ago, but time prevented me from working on it.
Not to mention, I’m not ready to stop writing Ruby. The language still excites me to use. I actually still enjoy working in Ruby on Rails, too.
I was nervous that learning a new style of programming meant that I would have to denounce OOP.
That said, I stopped spending time on Elixir or functional programming.
Blending Functional & OOP in Ruby
One day after the birth of our baby, I was on babysitting duty. As I often do, I got comfy on the couch and hit YouTube. I came across this gem. 👇🏻
Piotr’s talk not only helped me understand core functional programming principles but also how I could use them in correlation with OOP in Ruby!
For the first time, I realized that functional programming could co-exist with object-oriented programming.
This talk challenged me to explore this idea more. It led me to find Tim Riley‘s excellent series he wrote on the IceLab blog. Tim and Piotr are both dry-rb core team members. They are sharing a new way to approach web development in Ruby.
Just recently, Luca Guidi (creator of Hanami, a web framework I ❤️) gave a very compelling presentation on what a functional future for Hanami might look like.👇🏻
The idea is that we can take functional programming principles and blend them with OOP. In fact, Ruby makes this quite easy and painless.
The result, as it appears, is code that is cleaner, easier to use, and easier to test.
Resources + Learning More
These three developers are helping me discover the joy of functional programming without requiring me to leave Ruby.
Sure, I’m probably a little drunk on the idea. I’ll admit that. However, I’ve been wanting more out of my code. This is re-igniting my love for programming.
Also, this exploration has me questioning how well I know OOP. So while continuing to learn about functional programming, I’m exploring more into object-oriented programming as well.
In the future, I plan to share more in-depth what I’m learning.
Until then, I want to give you some resources I’ve found valuable in my quest to combine functional programming with OOP.
- Full Stack Fest 2015: Blending Functional and Object-Oriented Programming
- RailsCamp 2017: Functional Web with Ruby
- RubyConf AU 2017: Reinvesting in Ruby
- RedDotRubyConf 2017 – Functional Architecture for the Practical Rubyist
- My Past & Future Ruby
- Inactive Records: The Value Objects Your App Deserves
- Functional Command Objects in Ruby
- Effective Ruby Dependency Injection at Scale
- Better Code With an Inversion of Control Container
- A Change-Positive Ruby Web Application Architecture
- A Conversational Introduction to rom-rb
- Put HTTP in its Place with Roda
Interested in Functional Ruby?
Drop me your email 👇🏻 and I'll send you posts (like this one) as I write 'em.