Michael Dyrynda
Home Blog Podcasts
 
Review: Adam Wathan's Refactoring to Collections June 20th, 2016

A Bit of Theory

As with any new programming technique, I find it helps to first be introduced to the theory behind it. What is this new technique, why should I use it, and when will it help me?

In order to best understand collections, Adam first explains the differences between declarative and imperative programming; he introduces higher order functions and introduces readers to the foundational functions behind collections, through examples of how you would implement imperative loops as declarative, higher order functions.

The style of writing is simple to follow, and the explanations ought to be clear enough for most readers with a basic level of programming knowledge to follow along with.

A Lot of Practice

Learning a new technique isn't all about the theory, though, and for some learning through doing may help things stick with them better.

The real guts of this book are heavily focussed around explaining collection pipelines through examples you might encounter in the real world. Whilst the specifics themselves don't align with implementations I've written in the past, there were a number of pre-collection examples that looked like code I've written in the past.

Starting with an imperative example, breaking it down into components, and looking how you might refactor that into its declarative counterpart really helps to understand the path from loop to collection in different situations.

Afterword

Being a predominantly PHP developer, the examples in this book are implemented using Laravel's Collection implementation. Don't let that fool you, though, as the concepts talked about can easily be reproduced in other languages. The book focuses on concepts, using PHP as a means to implement them.

I came across PHP's array_* functions a couple of years ago myself, had sprinkled them into my own code previously, and have subsequently been leveraging Laravel's Collection class more and more. This book is a good desk reference when you look at a problem and can't quite figure out how to tackle a refactor.

Readability of code is something I've become fonder of in the last couple of years, and working with code that reads well is often more important to me than saving a couple of megabytes of memory, or shaving a few milliseconds from a response.

I hope Adam does address this in a follow-up blog post; whilst never writing another loop again is a nice goal to have - much like anything - there are a time and a place for doing so. Considering the size of the dataset you're working with and whether collections are the appropriate way to work with it is important. Keep in mind that passing data through a collection pipeline will sit in memory until the full operation is complete. The longer that pipeline, the more likely you are to hit a memory limit.

Adam loves a challenge; if there's a way to get code into a collection pipeline, he'll find it. If you're stuck with a refactoring, get in touch with him on Twitter with a gist of your before and where you got stuck.

Last - and certainly not least - the book itself. You can find the book on Adam's website here. There are a number of purchase options to choose from starting with the book and set of exercises to refactor yourself.

I'm a real developer ™
Michael Dyrynda

@michaeldyrynda

I am a software developer specialising in PHP and the Laravel Framework, and a freelancer, blogger, and podcaster by night.

Proudly hosted with Vultr

Syntax highlighting by Torchlight