It should come as no surprise that I’m a HUGE functional programming fanboy — shameless plug for an older blog post (Link).
I rave about it constantly and never miss an opportunity to mention how great it is, especially in
web development, translation: I think it simplifies the web development process and makes JavaScript, an already
cool programming language, that much cooler.
So what makes functional programming so gosh darn good, you ask?
Well then, generic reader I’m imagining right now, let me tell you how you and your code can benefit from going functional.
Facts
There’s a reason why I didn’t name this article ‘The Sensible Ravings of a Functional Programmer’ because
I’m not, pure and simple.
I’m not using any advanced functional programming techniques, I don’t know all the terminology and lingo in
functional programming and it’s not for lack of trying.
I went through books and examples (and I’ll keep going through books and examples!), a lot of things didn’t stick but
the things that did stick improved my code and coding mindset immensely and I believe will benefit anyone, no matter
the skill level.
Functional programming concepts anyone can start using right now
These are the concepts I believe anyone can learn relatively easily and start using right now and see an immediate return of investment in their code:
- Immutability
- Pure functions
- Function composition
- map, filter and reduce
Now that we saw the list let’s go through it and find out why these things are so badass!
Immutability
My experience with immutability in JavaScript is that it isn’t that important when dealing with primitive data types but is INCREDIBLY useful when working with objects and arrays.
The gist of immutability of objects and arrays in JavaScript is that when you need to update an object or array you don’t mutate it by directly changing an object property or array item but by replacing it completely with a new updated one.
I found out that this is especially useful in modern front-end frameworks like Angular or React that sometimes can’t detect mutations in arrays or objects to re-render the UI and actually recommend an immutable approach to ensure that everything will render properly.
For me, another benefit of the immutable approach is a psychological one. I can’t explain it but writing code with immutability in mind feels more stable and solid than dealing with mutations. Try it and you’ll see.
Pure functions
Pure functions are an effin REVELATION!
After learning about pure functions you’ll never want to write functions in any other way.
The whole thing is pretty simple and straightforward.
When you write functions you pass to them everything they need from the outside scope then the function does what
you need it to do and finally you return a result but there’s a big twist here, the function can’t affect anything
outside it’s scope so the only way for a function to ‘communicate’ with the outside world
(actually do anything and be useful) is with the return
This doesn’t seem like much but this rule forces you to write lean specialized functions that don’t do a lot of things but the things they do, they do well.
This makes pure functions predictable since if you pass to it the same input no matter how many times you will always get the same output. Nice.
Another positive side effect of pure functions is that by their very nature they’re simple, which means they’re easy to reason with, which means developers don’t need to invest too much brain power into thinking what exactly your code does and on top of that they’re easy to test.
Pure functions are cool beans!
Function composition
After you get into the groove of writing pure functions you’ll notice (at least I hope you’ll notice) that you’re writing more generalized functions that can be used all over the place as opposed to functions that are specific to a given situation.
An example would be writing a function that adds whatever numbers you pass to it and returns the result instead of
writing a function called addDucks()
that gets some ‘duck’ objects from the outside scope, accesses their count
property, adds them and assigns the result to some other result
variable perhaps also outside the function scope.
You don’t need to be a genius to see the benefit of writing functions that work everywhere versus functions that depend on having ‘ducks’ somewhere in your code.
The fun part about these generalized functions is that they can be easily stacked, or rather composed by passing the result of one function as input to the next. The REALLY fun part about this approach is that it turns building apps into writing formulas composed of really simple actions (functions) (also, math is fun).
Composing functions is a bit trickier to get right than the rest of the list but even without using functional programming libraries such as lodash or ramda to compose functions and just calling one function after another can lead to much nicer, cleaner and simpler code.
map, filer and reduce
These 3 keep surprising me with their utility and just how useful they can be.
If you work a lot with arrays (and you probably do) knowing how to effectively use map, filter and reduce can save you TONNES of hours of work and will reduce (ha ha, get it? reduce) what would otherwise be complex problems into solutions with simple steps of chained maps / filters / reduces.
You can find more about map, filter and reduce in my other blog post (Link)
Did I just link twice to the same blog post in this article? Yes I did.