Collection pipelines
Most of programming is processing collections. Learn how to do it right
Collections form the backbone of any programs. Working with data is an essential part of the software, and thus an essential part of development.
But they are non-trivial to get right. There are several approaches to choose from, each with their advantages and drawbacks.
In a series of posts, we’ll look into how developers handled collections decades ago, and how it evolved over several milestones.
Avoid the common mistakes, and write code that is easier to understand by knowing how to approach collections effectively.
1. Better collection processing with collection pipelines
In the first post, we’ll look into how collections used to be processed. The for loop is the construct that dominated this area, thanks to its simplicity and its effectiveness.
But simplicity from the view of a machine is far from being simple from the perspective of a fellow developer. For loops, while being the most efficient solution, is prone to getting too complicated.
But what is the alternative?
Learn the basic building blocks of the modern, functional collection pipelines, and how combining them forms complex processing steps.
2. Ditch for loops. Here is a case study to convince you
Simple for loops are simple to understand. In that sense, there is no need for another entirely different kind of collection processing.
But they are written for the machine, and that hardly translates to the specification developers are working from.
In a series of example problems, see how for loops diverge from the written requirements, making them ever harder to understand.
3. Where Array#Extras fall short
Javascript Arrays come with several functions that can be found in modern pipelines. Using them makes the code easy to use and familiar to everybody.
But what if something is missing?
In that case, there is no good option, just bad ones in varying degrees.
Learn the downsides of amending the Array prototype, and why hacking your way to adding them is not the right approach.
4. Demystifying chaining in Javascript
Chaining is a powerful method offered by several libraries. If you’ve ever used Underscore or Lodash, you already know how effortlessly can the functions be composed.
It is half magic, and half ingenuity. But how does it work?
Learning by doing, we’ll iteratively build a simplified chain function from the ground up. You’ll see that it’s not magic, but a few tricks, working together to achieve the desired effect.
Also, learn about the downsides of such function.
5. The silver bullet of collection pipelines: Functional composition
We’ve seen that many approaches to collection pipelines fail, mostly because of the lack of extensibility.
How, then, should such a construct designed to get both the expressiveness and the ability to add new kinds of steps freely?
Fortunately, functional programmers long have the answer. It’s the combination of function composition and higher-order functions. And the best of all, it is entirely doable in Javascript.
Learn how to design a pipeline that has all the advantages of the previous implementations, but none of their drawbacks.
6. Reuse code with domain-specific steps in collection pipelines
Finally, let’s talk about the need for extending a pipeline. Most of the steps fit into a basic building block, and thus trivial to implement.
But what if that’s not the case?
As software gets more complex, so is the need for reusing ever complex parts of it. Soon enough, the reused part no longer fits into a map
or a filter
.
In that case, adding new functions becomes a necessity. And that is the point where having chosen the right implementation pays off.
Learn why it’s usually the case for real-world software, and what is the right strategy to follow.