Composing functions together can become extremely fun, especially when they work.
What Does It Mean to Compose?
Composing means to combine more than one thing to build a bigger thing. It’s a general concept in mathematics where you combine two or more functions into a brand new function.
Most programmers have been working with this concept growing up in school, in the form of something like f(g(x)) which is pronounced “f of g of x”.
Let’s talk about composing functions. When we compose functions together, the main goal is to take a function and combine it with another function, so that when both of them are together, it gives us a more enhanced function that helps to produce a value that we want.
There are multiple good reasons why people prefer to compose functions, such as reducing code and providing a more convenient reusable piece of code.
Why Is It Important to Compose?
The doubleTheNums function is responsible for looking at an object and doubling its number value if it’s a number type.
Why should we turn this into a composed function instead? Let’s look at some problems that the current function is having first:
- If the object passed in was deeply nested, the code will just keep awkwardly being pushed down right like a tree shape. But no one has time for that kind of thing.
- If the object passed in was deeply nested, we unnecessarily lose valuable brain energy running out of ideas to name the inner variables (innerObjKey, innerObjKeyValue might lead to deeplyInnerObjKey, deeplyInnerObjKeyValue, innerInnerInnerObjKey, innerInnerInnerObjKeyValue, etc.)
- The code gets repetitive over time. This might confuse us and no one wants to be confused.
- File size is increasing.
You might have also realized that the majority of these composed functions are composed of much smaller, modular functions.
Injecting Callbacks to Be Used in Conjunction With Existing Ones
The possibilities of good uses for composing functions are endless. But to wrap this article up, we’ll go over another useful use case so that you can have a better understanding of how functions can be composed to achieve a variety of scenarios.
In callAll, a .forEach ensured that each function in the pipeline received the event object each time because, by definition, it doesn’t return anything to the caller even if it tried to return one.