Functional Design (Addison-Wesley) |
Author: Robert C. Martin This book sets out to explain the principles, patterns and practices of functional design, and why functional programming is a good thing. The author, Bob Martin, is well known as one of the authors and signatories to the agile manifesto. In this book, he bases his examples on Clojure because it's a simple enough language not to get in the way of learning functional programming. The first section of the book is titled Functional Basics, and Martin starts by setting out his definition that functional programming is more than just programming using functions, it is programming without assignment statements and without variables. Chapter one defines immutability, and looks at the problem with assignments, arguing that assignment leads to temporal couplings and race conditions.
Next, Martin looks at persistent data, and how working with data can mean having to "cheat" in functional terms and use variables. Chapter three adds in the concepts of recursion and iteration, which are used by functional programming to eliminate the need for assignment. Laziness is the topic of the next chapter, starting with the idea of lazy lists which Martin defines as iterators masquerading as lists. This opening part of the book ends with a chapter on statefulness, with sections explaining why mutation is essential, and how life is hard but software is harder. The second part of the book is titled Comparative Analysis, and it explorers and examines the differences between code written for a series of exercises in object-oriented and functional styles. Each chapter has a Java version and a Clojure version, and a comparative analysis. The chapters look at finding prime factors, a bowling game, and 'gossiping bus drivers' where given a number of bus drivers each with their own set of knowledge and circular route share gossip when their routes overlap. The question is, how many steps before all the gossip is shared. This section of the book ends with chapters looking at the payroll problem, and at adding type checking to the Clojure solution for payroll. The next part of the book consists of two chapters considering functional design, specifically how it relates to data flow and to SOLID, a set of object-oriented design concepts set out by Martin - Single Responsibility Principle, Open-Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle. The chapter on data flow contrasts object-oriented's data management with that of functional, arguing that functional code is like plumbing in the way it moves data through the process. A section titled Functional Pragmatics comes next, with chapters looking at how to incorporate testing into functional code; how to write code that seems outside the parameters of what functional code can achieve (using the example of a simple GUI interface); and how to deal with concurrency. Next comes a chapter on design patterns that look at tasks where functional programs might get bogged down, and ways round them. The book ends with a case study of creating a large functional program. Overall, I found this book explained the concepts of functional programming, its strengths and drawbacks very clearly. At the end I felt I had a much firmer grasp of functional programming. Recommended. To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
|