Recently, I was considering a thought I’d had a couple of times over the years - I felt that I had observed a shift in the way JavaScript was written, from code that stuck mostly to Object Oriented principals to code that embraced a more functional approach. But when I spoke to more experienced devs about this, I was surprised to find that for some of them the opposite was true; JS has always been functional. So why did I think I had seen the opposite?
I think it’s a combination of two factors:
In this article I want to discuss the first reason, and why I think it led me to feel the way I did.
First, I think its worth briefly touching on some of the changes in JS that show how it has in some ways intentionally encourages a more functional approach. Although JS was originally intended to be Object-Oriented in order to match Java, this was always a half-hearted effort1. And as time has gone by, JS has introduced features that do support functional programming in a way that likely wouldn’t have happened earlier on in its lifetime. These features include arrow functions (a nicer syntax for lambda functions) and built-in methods that allow for a more functional approach to programming, such as the map function on arrays.
These were both added in ES5 in 2015, and are common sights in modern JS code, however the need for JS to support such a wide range of devices and platforms means that new features are often slower to be adopted, even with tools like Babel to plug the gap; I think this is why we see these features becoming more and more used over time. However, ES6 would then introduce some significant features intended for use in more OO projects, not least of all classes. With this in mind I think its fair to say that the JS spec was not intentionally being driven towards a more functional paradigm, but rather was attempting to support both options.
In university, the way I was taught software engineering was very object oriented heavy - many of the taught principals behind good code were heavily focused on this approach, and this made sense as the growth in popularity of Object Oriented was directly tied to the increase in scale of software projects. As software moved from an aspect of hardware to an industry in it’s own right, the Object based approach offered both a way to visualise the work for stakeholders and a way to maintain strong mental models of how and where code ran.
However, this led to the idea that object oriented was the “right way” to code - and since many of the most impactful attempts to codify best practices were being made at this time, the two became linked for many people. I started programming after this point, but many of the people I learned from in and out of uni had learned during this period and now taught these principals as gospel. And for myself and others without the original context, that was what it became.
Ultimately this led me to see OO code as “better”, and functional code as an unfortunate shortcut - this was what stuck out to me when writing and reviewing, this was what I wanted to be writing myself. And this is one of the reasons why I thought I saw more code in that style early on, because I was remembering it more.
But I think a more impactful reason was the fact that I’ve never spent so long around recent grads as I did at my first first couple of jobs. As time has gone on, I’ve spent more time working with devs that came into the industry through alternative (non-uni) routes, and as I’ve grown in experience I’ve worked in organisations that haven’t employed juniors at all. And without those juniors making the same assumptions I did, there’s no longer anyone trying to shoehorn object oriented patterns into places they don’t fit the way I used to (and still occasionally do).
As I’ve grown in experience, I’ve found (as we all do) that both paradigms have a place. And once the impression of OO being “better” has worn away (usually due to a few too many bugfixes made 10x more complicated due to a rigid design), I think we all emerge as better for the experience. And more importantly, I’ve realised that OO never went away - I just stopped seeing it as the default and instead understood it as it is: a useful optoin with an enormous historical impact on the way systems are built.