map() – JavaScript Functional Programming

What is functional programming?

Functional programming is a programming paradigm, the process of building components using pure functions and avoiding shared state, mutable data and side-effects.

In simple, functional programming is doing our tasks with functions, avoiding side effects by using pure functions.

For example, Without iterating the list using user defined loops; we can iterate the list using the functions like map(), reduce(), and filter()

Let’s see about map() function in this post.

I have started exploring about functional programming in recent times. The topic looks very interesting and helping me to optimize my own codes. After using functional programming, my code looks pretty good and maintainable.

For example, If I need to iterate a list and extract some data previously I use some for loops to do the task like following:

var players = [
 { name: 'Dhoni', nickname: 'Super Cool Captain' },
 { name: 'Raina', nickname: 'Furious fielder' },
 { name: 'Ashwin', nickname: 'Spin Master' }
];

var namelistusingloop = [];

for (var i = 0; i < players.length; i++) {
 namelistusingloop.push(players[i].name);
}

Later I found that using map function, we can simplify our function even more.

map() will take the callback function as follows.

var namelistusingmap = players.map(function (player) {
 return player.name;
});

In the above part, the map() will iterate the list based on its length. We don’t have to initialize & iterate list as we did in the for loop.

Now the code looks simple and effective. All the short code doesn’t mean effective unless it reduces the development time & maintenance period.

We can even simplify the code with map() using arrow functions.

var namelistwitharrow = players.map((player) => player.name);

In short, arrow function is used to simplify our callback function. To know more about arrow functions click here to visit the earlier post in this space.

Now the code looks even cooler and smart

var namelistusingmap = players.map(function (player) {
 return player.name;
});

var namelistwitharrow = players.map((player) => player.name);

Lets put all code together to compare to take quick look,

var players = [
 { name: 'Dhoni', nickname: 'Super Cool Captain' },
 { name: 'Raina', nickname: 'Furious fielder' },
 { name: 'Ashwin', nickname: 'Spin Master' }
];

// using loops
var namelistusingloop = [];

for (var i = 0; i < players.length; i++) {  namelistusingloop.push(players[i].name); } // using map function var namelistusingmap = players.map(function (player) {  return player.name; }); // using map function and arrow function var namelistwitharrow = players.map((player) => player.name);

console.log(namelistusingloop);
console.log(namelistusingmap);
console.log(namelistwitharrow);


Output

["Dhoni", "Raina", "Ashwin"]
["Dhoni", "Raina", "Ashwin"]
["Dhoni", "Raina", "Ashwin"]

Now it’s up to us to decide which module we have to use in our code – the code using the pure function or using the loops.

Let’s explore more about functional programming in upcoming posts.

Happy coding!

The art of readable code – review

I’m not sure whether we can call this blog post as review or summary of a book. After reading please decide yourself which name suits very well.

Book Name : The Art of Readable Code
Authors : Dustin Boswell and Trevor Foucher

cat

 

The book has been divided into 4 parts

  • Surface-level improvements
  • Simplifying loops and logic
  • Reorganizing your code
  • Selected topics

And then each parts discussing various important topics in details in the entire book.

Every chapter starts with a cartoon image which is fun and loads with deep meaning related to the topic. The cartoon image gives a quick view on what information the entire chapter had in it.

The author is discussing the topics with code examples which he himself and his team coded in the past. And also examples are very clear on how we can improve our coding standards and styles. The examples includes various programming languages like Java, JavaScript, Python, and C++.

The ultimate aim of this book is to discuss about “how we can make code more readable”. So that whenever we revisit our own code, we don’t have give a alien look to our code editor.

The author mentioned in the book as the goal is “to minimize the time
it takes someone else to understand your code.”

Part – I: Surface Level improvements

The topics under this part will help us make a small level changes in the code line by line which will results in a big form. The topics like choosing better name for variable & function names, writing more understandable comments, using common formatting for code across team are covered under this section.

My favorite topic under this part is loading information with names – like adding units in the arguments of a function, using concrete names.

Adding proper comment to the proper place in a proper way is a winning policy for every successful coder. (+ commit logs :)) There is no specific length for a good comment or log message, the message should precise and compact.

Part – II: Simplifying loops and logic

I have started my coding career by learning basic loops and logic. The various topic under this part is like revisiting our own memory and brush up some basic understanding of loops & logic.

The large number of variables, complicated loops, and giant expression in our code will create great confuse in our mind while going through the code for understanding. This part discuss various topics on how we can make our loops more readable. If loops looks so complicated, the bugs may likely go unnoticed. Sometimes our complicated loops will play Three-card monte trick with us.

My favorite topic under this part is shrink the scope of the variables. Too much global variables will create pretty good confusion at its best.

Part – III: Reorganizing your code

Topics under this part discuss about the functional level changes in our code – Extracting “unrelated sub problems”, Rearranging the code so that one task happening at a time, turning thoughts into code.

The ultimate aim of this part of code is to separate the generic code from the project specific code, and also to improve reusable of the code.

“Each new line of code needs to be tested, documented, and maintained. Further, the more code in your codebase, the “heavier” it gets and the harder it is to develop in.”

Part IV: Selected topics

In this part, author discussed about testing – how to write tests that are effective and readable at the same time.

Too long to read

Its really a nice book, I would like to recommend to all the people in Software field (especially young developers). Some of the Experienced people may already have more insight on above topics by gathering knowledge from their years of experience. We won’t accept some of the suggestions in the book, if we discuss these topics with our team we can get into more interesting discussions.

Happy reading 🙂