Principal Software Engineer I
Then frameworks entered the market in a big way. jQuery, Knockout, Angular, React, Vue, etc. They came slow at first. Now we get a new framework daily.
We also have an infinite number of alternatives such as TypeScript, CoffeeScript, ClojureScript, ELM, etc.
It can be overwhelming and it’s impossible to keep up with everything.
console.log(this) on a daily basis. Who am I now? If you didn’t do everything exactly right, it was a nightmare. Private methods and values had to be prefixed with underscores or even worse wrapped in closures.
Functional Programming gave me a new perspective, it made me see programming in a much different way.
It was weird at first and took me a while to get used to it. Everything was backwards. Immutable. Foreign.
Slowly, I would try to solve code the functional way. Because I was unfamiliar, it took longer and was more of an academic exercise, but I started to get it and get better at it. Then I got why.
var's were replaced with
const. When my code became immutable and my functions became pure,
var disappeared completely.
I remember looking at my code and marveling at not seeing a single
var or even
let, everything became
const. My interest levels started rising.
for loop was one of the first things to go. I started replacing the majority of my
for loops with
reduce. The loops that required something extra were replaced with recursion or libraries like lazy.js. Before you start to mutter the word
break, read this.
for loop is now completely extinct in my codebase. If you do happen to stumble across one, point it out so I can kill it.
if statement was soon the next thing to go. I stopped writing large blocks of code nested inside an if-else. (this is also good practice to do with OOP). The logic was extracted into functions. After this, it became obvious to convert the
if to a simple
It is now almost impossible to find an
if statement in my code. I will rarely use one for readability for other programmers.
for gone, the next feature I eliminated was the
switch. These weren’t used often, but I wanted a functional alternative.
I also really like Ramda’s cond operator as a replacement for switch.
That’s right, you heard me correctly!
this has completely disappeared. If you haven’t been with me up until this point, you should be with me now.
Now, there is only data and functions and we have absolutely no need for
this. I started to see an object as mutable state + functions. I didn’t have a need for mutable state or for functions to be attached to objects, so I disconnected them.
I even wrote an article talking about some of the benefits of decoupling methods from their objects.
Looking back, I now realize OOP added a complexity that was not necessary. I found I could perform the same tasks without having a mutable state.
The code felt lighter because I was no longer passing around these heavy objects. There was just data and functions. These functions now became more reusable now that they weren’t tied to an object.
My code now looks completely different. It consists of a collection of many pure functions, organized into ES6 modules. I use these functions to compose into more complex functions. A large majority of functions are one-line lambda expressions that immediately return a value.
I now see software inputs as data streams and program reactively on those streams.
My understanding of functional programming has given me more options at my disposal to me to solve common problems.
I also learned that functional programming is inclusive and can be used as much or as little as you need in existing projects today. C#’s LINQ is a great example of functional design in an OO language.
Functional Programming can be beautiful.
I will respond to every question and comment, so don’t be shy!
I know it’s a small thing, but it makes my day when I get those follow notifications on Medium and Twitter (@joelnet). Or if you think I’m full of shit, tell me in the comments below.
Create your free account to unlock your custom reading experience.