It may seem odd that a book devoted entirely to recursion would contain such statements as “You never need to use recursion. No programming problem requires recursion … In fact, a recursive function might be an overcomplicated solution…” Nevertheless, Al Sweigert goes on to explain the ins and outs of a programming technique you should rarely, if ever, need to use. And he does so masterfully.
Recursion and recursive functions seem to many (including me) to be esoteric mysteries, understandable only to those elite few who are wired to think in a pretty unconventional way. We are taught from a young age that you can’t define a word by using that word, itself. To that idea, recursion says “watch me!”.
In explaining recursion, Sweigert is careful to begin by laying important groundwork. With adequately (but not overly) detailed and approachable explanations and examples, he discusses functions, their operation and features. He spends significant time explaining the call stack, what it does, how it is structured, and how it operates, leading to a discussion of ‘stack overflow’, one of the risks of using recursion. With that out of the way, he then devotes an entire chapter to comparing recursion and iteration, demonstrating that in the vast majority of cases, recursive functions simply aren’t necessary and in some cases perform worse than their iterative counterparts.
The ensuing chapters look at recursion in action, exploring several popular recursive algorithms and techniques. It is in these chapters where we begin to see where recursion is actually a good idea, where it is a good fit (and also where it is not). So, while recursion may be mostly unnecessary, it is absolutely tailor-made for certain functions. Among other things, Sweigert explores traversing tree structures and demonstrates how memoization (yes, I spelled that correctly) can improve the efficiency of some recursive functions. He provides a plethora of examples demonstrating each concept he explores. It is worth noting here that Sweigert provides code examples in both Python and JavaScript (where possible), making it a valuable resource for students of both. The book ends with several projects that build on the lessons that come before, including one that implements the very cool Droste Effect.
For the beginning and intermediate programmer, which is the only demographic to which I can relate, the real value of this book lies not only in its teaching about recursion. Its value also stems from its ability to stretch you just a little bit further, to make think just a little bit differently, to give you confidence that maybe lofty concepts that seem way above your head are within reach. Any book that can achieve that while simultaneously telling you that what you’re learning is mostly unnecessary is well worth the read.
--Reviewer was provided a review copy by the author