In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. - Wikipedia
Seems complicated to understand? Let's start off with a simple example. Assume there are few items on your cart which are out of stock. And you wanted to exclude those and calculate the total only for the in stock items. So you would be performing something like this
Here, we have not used any functions to get our desired output, which means our code can't be reused in the future and it'll be harder for us to test our code.
Now, we are going to perform the following
- Break our solution into smaller functions
- Deal one function at a time
- Put them back together so it works as a whole unit
Let's get started
Perfect! Did you notice what have we achieved here? We have not only created separate functions for reusing them, but also they are pure functions which means they don't have any side effects.
Now, let's tweak our solution using the ES6
See you in another article! Happy Coding!