Functional JavaScript

Author: Michael Fogus
Publisher: O'Reilly
Pages: 260
ISBN: 978-1449360726
Audience: Advanced JavaScript developers
Rating: 3
Reviewer: Ian Elliot

And you probably thought JavaScript was functional out of the box

This book isn't about using functional programming techniques in raw JavaScript. It makes use of the Underscore library to add additional functional features.  

Even if you do want to use Underscore this might not be the book for you because it doesn't really explain anything about the library. In fact it doesn't even make clear when a function is being used from the Underscore library. You are reading along and all of a sudden you meet:

_.toArray(arguments);

Yes this is a use of an Underscore method, but you aren't warned and what it does isn't discussed. The book continues in this way - simply presenting examples and not explaining them. You are supposed to work out what is going on by knowing what the code does. This isn't quite the same thing as understanding the features and facilities that a library offers and then seeing how they might be used. 

 

Banner

 

Chapter 1 starts with a look at JavaScript and how functional programming provides desirable features like encapsulation. What it doesn't do is really explain what the characteristic of functional programming are and why it is different. If you don't know why functional is good, my guess is that you probably still won't know after Chapter 1. 

Chapter 2 is about standard JavaScript functions and the idea of applicative programming. Here we meet ways of using JavaScript - imperative, prototype and meta programming. I'm not sure I agree that these are three paradigms - they just seem to be three aspects of JavaScript and you could add a few more if you wanted to - classed based object oriented, non-class based object oriented and so on. When we get to applicative programming, the explanation of what it is doens't really help. Next we are given a lot of examples of Underscore functions that apply another function to a collection. Again the big problem is that the functions used are hardly explained. For example the _.reduce function is described as:

collects a composite value from the incremental results of a function supplied with an accumulation value and each value in a collection. 

This is fine if you know what a reduction is all about and have met a function like reduce before. If you don't then you are going to have to work out what is going on from the examples. This could be made so much easier to understand. 

 

functionaljavascript

 

Chapter 3 is an in depth look at scope and closure. Again the problem is that there are no helpful explanations of what something is. For example, dynamic scope is said to be useful and then we have some code that implements it - without the reader really having much idea of what is being implemented. It's like a puzzle you have to solve to understand what something is. Later the chapter goes into detail about closure but fails to notice the key relationship between scope, closure and the Function objects lifetime. Again it seems to make everything more difficult than it needs be. 

Chapter 4 introduces the idea of higher order functions. It takes you through using functions as parameters and return values. There is a lot of discussion of how the ability to use functions in this way makes it possible to create functions that let you work in a different way. The issue of state and mutability is also introduced but you may not follow whey immutability is a good idea in functional programming.

The theme of higher order function continues in Chapter 5 about function building functions. This is where composition is introduced along with another look at mutability and using imperative styles in framework functions. To quote:

"This book is not about spewing dogma regarding the virtues of functions programming. I think that there are many functional techniques that offer ways to rein in the complexities of software development, but I realize that at times, there are better ways to implement any given individual part."

OK - but this is a book about function programming, right? And as such I think I for one would like to be exposed to some of the ideas that are being ignored. The problem is that the key ideas are hardly ever made explicit and when they are you could easily miss them.

The chapter then moves on to currying, which is a much more practical and directly useful technique than worrying about immutability. The problem is that once again you have to figure out what currying is from examples and difficult-to-follow explanations.

"A curried function is one that returns a new function for every logical argument that it takes."

I have no idea what this means and I already know what currying is. Later the distinction between currying and partial application becomes even harder to fathom. 

Chapter 6 is about recursion and provides fairly standard material that you probably already know. It does also point out that JavaScript doesn't optimize recursion and explains how to use a trampoline to implement tail recursion. 

Chapter 7 tackles the issue of purity and immutability, which given how important these ideas are probably should have been introduced earlier in the book. It makes the point that mutation is OK as long as it is used internally within a framework function that returns an immutable value. It also explains the connection between immutability and the way recursion can cover up the changes by making them on the stack. It is the core premise of functional programming that functions should behave like mathematical functions and this implies immutability.

Chapter 8 explains flow-based programming, i.e. how to create fluent interfaces like those jQuery uses and how this can be the basis for building functional programs. This flows into Chapter 9 where the idea of building systems without the use of class hierarchies is introduced. These final two chapters are perhaps the most interesting in the book, but as before it is difficult to pull out the pearls of wisdom because of the way the examples keep them hidden.

This is not a book for the beginner in JavaScript. It isn't a book for the functional programming beginner because it does nothing to motivate the functional approach and it makes it hard to extract the principles being described. There are a lot of interesting things in this book if you are prepared to mine to extract its meaning and it isn't enough to just understand the examples - you have to think about why things might be better done in this particular way.

 

Banner


Object-Oriented Python

Author: Irv Kalb
Publisher: No Starch Press
Date: January 2022
Pages: 416
ISBN: 978-1718502062
Print: 1718502060
Kindle: ‎ B0957SHYQL
Audience: Python developers
Rating: 3
Reviewer: Mike James
Python, Object-Oriented? Not a lot of programmers know that!



Racket Programming the Fun Way

Author: James W. Stelly
Publisher: No Starch Press
Date: January 2021
Pages: 360
ISBN: 978-1718500822
Print: 1718500823
Kindle: B085BW4J16
Audience: Developers interested in Racket
Rating: 4
Reviewer: Mike James
If you have ever wanted to Lisp then try Racket.


More Reviews

Last Updated ( Saturday, 12 July 2014 )