JavaScript Allongé thoughts

An interesting, and slightly overwhelming, look into functional programming with JavaScript

M. Lee Pee
Aug 22, 2017 · 4 min read

So the I have been a bit quiet for the last few weeks! After finishing the Twitch App, I took a small break to review front-end via Colt Steele’s Web Development Bootcamp on Udemy. I finished the front-end portion of that course and enjoyed it, learned a few new things about bootstrap that I had missed during the FCC curriculum, and made a few neat little projects. I also started on my calculator app for the front end, more about that some other time…


I have been on+off reading this 400+ page tome, JavaScript Allongé, by Reg Braithwaite in between. From its cover and table of contents, I thought it would be a nice simple overview of how to write functional JS. I should have known better than to think P1XT would assign anything “simple”. It is a bit more complicated than that. The book uses ES6 syntax, which can be intimidating at first, but after typing a lot of code into the console and to a transpiler, it made sense. The book starts off slowly, defining what are functions, expressions, statements, values. It then takes off into hyperspace with function combinations, getting into some more advanced topics of logic and math theory.

The great point of the book is to show how functions can be passed around within JavaScript, and how this can tidy up your code, making it easier to debug, read, refactor, and reuse. These are called “higher-order” functions. ES6 syntax helps to write functional code, as it is similar to “lambda syntax” present in other languages. We can create small micro-functions that are used to wrap other functions, like allowing only 1 instance of a function once, checking for null values, maybe, or combining two functions together, compose. He goes through many many many topics, and I can’t cover them all so I will list my favorites:

  • Tail Call Optimization — something I never really considered when solving algorithms recursively, but is super interesting. We can also use default arguments to simplify the function’s design.
  • Lots of fun with the . . . (ellipses) for array gathering, reminiscent of YDKJS ES6 + Beyond. I’m not the most comfortable using this, so I will need to practice with it more.
  • Each function you write should do only one tiny thing. If you want to sum all the items in an array, you should have one function do the traversing and one function do the summing. This is harder than I expected!

A lot of time is spent with linked-lists and their manipulation, how to optimize performance and avoid issues using a copy-on-write. If you haven’t worked with linked lists before (if you’ve seen them in CS50 it is similar), this will be a bit confusing. It certainly has its applications, I just haven’t found any yet. Traversing linked-lists efficiently is another big topic he goes over. A lot of topics here shadow what I read in YDKJS, but in a completely new way, such as iterators.

The section on creating data using only functions blew my mind. It is way advanced compare to what I learned in freeCodeCamp and is definitely something you’d learn in a college CS course. I am so glad to have read it and understood a tiny portion of it. The author does mention that it’s not entirely …erm… relevant, but interesting nonetheless:

Knowing how to make a list out of just functions is a little like knowing that photons are the Gauge Bosons of the electromagnetic force. It’s the QED of physics that underpins the Maxwell’s Equations of programming. Deeply important, but not practical when you’re building a bridge.


His discussion of objects and classes also touches on stuff learned first in YDKJS, but with different opinions and depth. There are many ways to construct and organize objects, and you need to carefully think about which one you’re going to use (if you’ll use class or not, to use the new keyword or not, etc), especially regarding this binding (remember the same discussion in YDKJS?). Another thing he really cleared up for me, is the reasoning behind the introduction of Symbol to keep things private to certain objects.

His discussion of “colored” JS towards the end of the book opened my eyes to the issue of writing code among a large team, especially if the code is very abstract. You need to either make sure all team members write in the same style, or you need to make sure the high level functions can work across “colors”. If one person write in functional, and another in OO, how can you make sure the two codebases play nicely?

One caveat, I guess, is all his syntax uses ES6 functionality. This made it a bit confusing at first, and you need to double check with a transpiler like Babel that all code you write in this way can be polyfilled for use in older browsers. For example, I’ve started using ES6 Array Destructuring more often, but this is not backwards-compatible by default.


In summary, this book, more so than YDKJS, left me many times saying WTF (What’s That Function?). I re-read sections over and over again. This is a valuable resource in JavaScript and also in programming paradigms / computer science. If I can apply just a few things I learned, I am sure my code will be more performant, more professional, and easier to maintain.

)

M. Lee Pee

Written by

Teaching myself webdev for fun and glory!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade