I think many of us who spend a lot of time working on front end solutions in JavaScript have been integrating functional programming techniques into our day to day. I started to do it mostly on faith — a “if there’s smoke there must be fire” kind of thing.
At first, I appreciated being able to cut down on a bit of boilerplate code. I very rarely create counter variables anymore, for instance. Instead of a “for (let i = 0; i <100; i++)” I use a “array.forEach( () =>)”. And actually, the whole “for (let i=…)” construct is often used to filter out objects from a collection or some kind of of transformation on objects in a collection. As a result, I hardly even use array.forEach, I use filter, map and reduce much more.
I love cutting down on the boilerplate and that is indeed its own reward. However, I have discovered, unexpectedly, that I trust my own code more than I ever did before. In fact, when I look back at code I wrote even a year ago, I see it as almost crippled and feeble. I’m exaggerating a bit, but you get my point.
Immutability provides the basis for this trust. If everything you create is created just once and never changes, the risk of unintended side effects is greatly reduced. In JS, we can’t eliminate that kind of thing, but it cuts down on the risk. It’s very comforting.
Richard Eng (not exactly a fan of JavaScript) wrote this post about how much he loves SmallTalk (Smalltalk ruined my life — The cost of a happy addiction). After reading it, I realized how much this newfound enjoyment from FP has affected me. For instance, I was looking at the code behind this reddit post this morning and thinking — “he should really be using const for most of those variables”. I was looking at someone else’s interesting multi-player game that had a bit of code plucking a player object out of a collection and thinking — “he should really use reduce for that and cut down 15 lines of code and 2 loops into a single line and get rid of those otherwise useless looping variables”.
If you’re looking for some reason to pick up some functional programming habits, consider “trust.” It’s a real comfort and has added a bit of joy to this developer’s programming life :).
</end>
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.
To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!