In my most recent Coding Blog Post I briefed on what I learned while reading the Book “DOM Enlightment“. The book improved my knowledge of DOM transversing, specific and list selection, style manipulation etc. This time I have decided on reading the book “Functional JavaScript” by Michael Fogus. I hope to prevent redundancy in my code and shorten the duration of my development process after reading this book.
The main plan is to blog every 2 – 3 days on what I could comprehend from each chapter and at the end of the book, create a project with which I could put the knowledge to test.
Hope you travel with me on my journey to self improvement
I encourage you to purchase the book for better depth into the subject. Especially since I will not blog every detail provided in the book.
Introduction : Chapter 1
What is functional programming ?
Book – “It is the use of functions that transform values into units of abstraction”
Me: “huh?”
Apparently there are several approaches to programming, two of which are object oriented and functional approach. In OO, you break problems into nouns, while in functional into verbs / functions /actions. What I have taken from this chapter after a few pages in is in Functional JavaScript we focus on abstracting behaviour into a unit. For example:
Instead of
var function(person){ if(person.name){console.log("name does not exist");} else if(person.name && person.name.length <= 5){console.log("name exists but too short");}e else{console.log("thanks for the input");} }
Do this
if(person.name){error("name does not exist")} else if(person.name && person.name.length <=5){warn("name exists but too short");} else{success("thanks for the input"); }
where
var warn = function(str){console.log("WARNING:", str)} var error = function(str){console.error("ERROR:", str)} var success = function(str){console.log(str);}
This seems to me like extra lines of code, but we will probably find out more into later pages of the book.
There are two conditional statements, from experience, that are repeatedly used:
if(x != undefined){doSomething(); } else {return undefined; }
and
if(y != undefined && y == true){ doSomething(); } else{return false}
They may not look this way in your own code, but the goal behind it should be familiar. The chapter addressed these conditional statements and advised abstraction of these statements. For instance:
See the Pen Functional JavaScript: Chapter 1 by Ajala Comfort (@AJALACOMFORT) on CodePen.
Personally speaking, I am still not convinced yet as to why I would need function Js. There are more functions to be written (just from what I have read in chapter 1) due to this “Abstraction”. However let us not overlook the fact that after establishing such abstractions, you can reuse these functions while being sure of their behaviour. I guess it is worth it in the long run.
Underscore library, one used as the foundation of the book, provides several functional programming helpers. Before using such library one is forced to choose between speed/performance and beautiful code. I like a balance of both worlds, but if I had to choose one as a developer, beautiful code. Because if my code implements abstraction and reusability it will be easier to debug than it would have had I applied the generic approach. Note that cross browser incompatibilty issues was mentioned in this chapter as a reason to use a library over methods like map of the Array. Apparently not all environment have the same map implementation.
There is more to learn in the book. We are finished with chapter 1 and I look forward to chapter 2.
Current Status: Confused but understood 20% of the concept of abstraction