Imperative to declarative: loops into pipelines
Say what you want from a list, and let the pipeline do the walking.
Watch the narrated video
Every step
Problem
Steps 1–4Refactor
Steps 5–8Payoff
Steps 9–10Recap
Steps 11–12The code: before and after
Before
data class Order(val customer: String, val status: String, val total: Int)
class SalesReport {
fun shippedRevenue(orders: List<Order>): Int {
var sum = 0
for (o in orders) {
if (o.status == "SHIPPED") sum += o.total
}
return sum
}
fun topCustomers(orders: List<Order>): List<String> {
val shipped = mutableListOf<Order>()
for (o in orders) {
if (o.status == "SHIPPED") shipped.add(o)
}
shipped.sortByDescending { it.total }
val names = mutableListOf<String>()
for (o in shipped) {
if (names.size == 3) break
if (o.customer !in names) names.add(o.customer)
}
return names
}
}
object DailyDigest {
fun run(report: SalesReport) {
val orders = listOf(
Order("ana@shop.com", "SHIPPED", 120),
Order("qa@test.shop", "SHIPPED", 999),
Order("bo@shop.com", "PENDING", 80),
Order("cy@shop.com", "SHIPPED", 45),
Order("ana@shop.com", "SHIPPED", 60),
)
println(report.shippedRevenue(orders))
println(report.topCustomers(orders))
}
}After
data class Order(val customer: String, val status: String, val total: Int)
class SalesReport {
fun shippedRevenue(orders: List<Order>): Int {
return orders
.filter(::countsAsSale)
.sumOf { it.total }
}
fun topCustomers(orders: List<Order>): List<String> {
return orders
.filter(::countsAsSale)
.sortedByDescending { it.total }
.map { it.customer }
.distinct()
.take(3)
}
private fun countsAsSale(o: Order): Boolean {
return o.status == "SHIPPED"
&& !o.customer.endsWith("@test.shop")
}
}
object DailyDigest {
fun run(report: SalesReport) {
val orders = listOf(
Order("ana@shop.com", "SHIPPED", 120),
Order("qa@test.shop", "SHIPPED", 999),
Order("bo@shop.com", "PENDING", 80),
Order("cy@shop.com", "SHIPPED", 45),
Order("ana@shop.com", "SHIPPED", 60),
)
println(report.shippedRevenue(orders))
println(report.topCustomers(orders))
}
}Transcript, step by step
Problem
Step 1: A sales report in loops Before
This sales report works on a list of orders, and each order is a data class with a customer email, a status and a total. Shipped revenue walks the list and adds the total of every shipped order to a running sum. Top customers copies the shipped orders into a list, sorts them by total, and walks them again to collect three different customers. The daily digest builds today's orders and prints both numbers. One of those orders comes from a test account at test dot shop, and it's the biggest order of the day.
Step 2: Leave out test orders Code smell
Finance says test orders, from customers at test dot shop, must not count in either number. So the check goes into the if in shipped revenue, and into the if in the first loop of top customers. This code is imperative, because it spells out how to walk the list step by step, so you have to replay the loop in your head to learn what it's for. You can see it in the running sum here, the temporary shipped list here, and the break and the in check here.
Step 3: The rule, twice Code smell
The rule for what counts as a sale is now written twice, so the next rule has to be found and added twice too. And in top customers, the filter, the ranking, the repeat check and the stop at three are tangled across two loops. So you replay both loops line by line to see which one decides what counts as a sale, and where a new condition belongs.
Step 4: Back it out, then pipelines Plan
Let's back that change out. Declarative code says what you want, so we'll turn each loop into a collection pipeline, a chain of operations where each one has one job, and add the rule after that.
Refactor
Step 5: Shipped revenue as a pipeline Replace Loop with Pipeline
Shipped revenue now returns a pipeline that starts from the orders. Filter keeps the shipped ones, and sum of takes each total and adds them up, so it does the map and the sum in one call. The running sum is gone, because sum of keeps that total for you, and the digest still prints twelve twenty-four.
Step 6: Filter and sort in one chain Replace Loop with Pipeline
In top customers, the first loop and the sort become one pipeline. Filter keeps the shipped orders, and sorted by descending ranks them by total from high to low. Nobody fills a temporary list by hand anymore, because each call hands back a new list, and the second loop still reads from shipped, so the output stays the same.
Step 7: The second loop joins Replace Loop with Pipeline
Now the second loop joins the same pipeline. Map takes each order's customer, distinct drops the repeats, and take three keeps the first three names. The output doesn't change, because distinct keeps a name the first time it sees it, as the in check did, and take three does what the break did.
Step 8: Name the sale rule Extract Function
The status test now sits in two filters, so we extract it into one function called counts as sale. Both pipelines pass it by name with a function reference, first in shipped revenue and then in top customers. Now the diagram shows both functions asking one place what a sale is.
Payoff
Step 9: Test orders, one edit Payoff
Now Finance's rule goes in again, and this time it's one edit inside counts as sale. Neither pipeline changed, because both of them ask counts as sale and don't care what it checks. So the digest now prints two twenty-five, with Ana and Cy as the top customers.
Step 10: Why it was easy Payoff
The running sum became sum of here, the temporary list became a sorted chain here, and the break and the in check became distinct and take here. Top customers is now four jobs on four lines, so you can see at a glance that a new sale rule belongs in the filter. Giving that rule one home for both pipelines came from Extract Function, and the pipelines made the spot easy to find.
Recap
Step 11: Where we started, where we ended Before / after
On the left, shipped revenue keeps a running sum, and top customers fills a temporary list, checks for repeats, and breaks at three. On the right, each function is one pipeline, and each line in it does one job. And counts as sale holds the rule for both of them, so Finance's next rule is one edit too.
Step 12: When to reach for it Takeaways
Reach for a pipeline when a loop filters, transforms or totals a collection, and you want each job readable on its own line. Skip it when each step depends on the one before, like a running balance that stops at the first overdraft, because the loop says that more plainly than a fold. You'll spot it by a temporary list filled in a loop, a running total, or a break or flag that stops early. Kotlin's any already works this way, because it says what you're asking and stops at the first match, the way a flag and a break do.