Becoming Functional |
Author: Joshua Backfield Do you want to be non-functional? Of course not!
Functional programming is of growing interest but it can be very difficult to break into this "other way of thinking". This book is an attempt to get you to think in a functional way without having to learn lots of theoretical things. The big problem is that the theoretical ideas are what motivate the way functional languages and functional programs are constructed and when you strip them down to their simplest cases they become fairly obvious. Once you have the theoretical ideas pinned down, applying them is equally obvious - but as error prone as applying any theoretical idea in computing. It takes practice to get it right. Joshua Backfield takes an interesting approach to explaining functional ideas. He explains things from the point of view of an experienced Java programmer. At this point you are probably thinking that Java 8 would play a big role in the explanations but no - most of it is in good old Java 7, Groovy and Scala. The problem is that the book doesn't explain the new languages, but slides their use in as modifications to Java. This more or less works as long as you are prepared to put in the effort to finding out a little more.
Chapter 1 outlines the basic ideas of functional programing in terms of its properties - first class functions, pure functions, recursion and so on rather than as a basic statement "to try and be as like mathematical functions as possible". Chapter 2 gets to grips with first class functions - a fairly simple idea. To explain it we have the start of an example that runs through the entire book. This takes the form of a simple database query problem and at first it takes about a page of code. This is the code we are expected to refactor into a more functional style. The idea is that we have repeated code that can be eliminated by using functions as objects, i.e. by passing functions. This is to be done using the facilities in Java 7, although this would be a really good opportunity to introduce the more appropriate facilities in Java 8. Next we move on to using Groovy to get the same results. I have to say that, while I am convinced that functional programming is a good thing, this demonstration didn't convince me. My first thought was that there were better ways to write the procedural code that more or less removed the advantages of the proposed, over complex, functional approach. However, in this case the functional approach is over complex because Java 7 isn't a good functional language rather than any inherent problem in the approach. Chapter 3 moves on to pure functions. Again we have a Java approach and then a move to Groovy. Chapter 4 introduces immutable variables and from this point on the book uses Groovy. The next chapter deals with how immutable variables can be used dynamically using recursion. Again, the whole discussion lacks a deeper explanation of what is going on. It gets into issues like tail recursion much too early as these things are about implementation and not the bigger idea - and the idea of trampolining has to be introduced because Groovy doesn't do tail recursion. Next we look at these things in Scala, which does do tail recursion. If you really are a beginner then things will be looking more complicated than they need to. Chapter 6 explains strict and non-strict evaluation - a very gentle topic that should cause the reader no problems. Chapter 7 is about the idea that a statement should always return a value, again a simple idea but now our example program is around two pages in length. Chapter 8 introduces pattern matching which is a fairly advanced idea in functional programming. The problem is that to my mind the initial example doesn't really demonstrate that it is a better way to work. I'd rather have the sequential Ifs and more to the point I'd prefer to convert to a function that was repeatedly called rather than write a big control structure that obviously needs attention. Chapter 9 is about functional OOP - a really difficult and contentious issue. The idea that objects are just data containers and methods should be static functions is introduced. Of course if you are deeply committed to OOP this will be a big shock and I don't think that the chapter does enough to explain or encourage this point of view.
The final chapter clears up some lose ends and gives an outline of how you might convert an existing system incrementally into a functional system. Personally I'd leave an existing system alone - converting to functional style would almost certainly introduce more problems than it removes. You can tell that I wasn't encouraged by this book. I am a functional programming enthusiast, but I found this book confusing rather than helpful. The use of Java 7 involved the use of some constructs that might seem strange just to implement missing functional programming facilities. Groovy is arguably not as attractive an alternative since Java 8 was introduced and Scala would have been a better, but slightly more radical, language change. If you like a very practical approach to explaining theoretical ideas you might like this book much more than I did. Showing how to convert Java 7 to a more functional style might well be just what you need to see the light. It would still be a much better book if it used Java 8 or later.
|
|||
Last Updated ( Saturday, 19 May 2018 ) |