Why go functional in programming?

Functional programming is hot now. I used to wonder what difference does it really make in going functional while coding…after all, if we can make the logic and code run, it should execute the program in the same way. But, in the last few weeks, I really changed my mind and get why functional programming is so much more effective. Hence, I’d like to share my experience.

Recently, I was assigned a very simple interview question to write an implementation for. Basically, the program was supposed to write a function that would count words given a paragraph of strings.

In haste, during the interview, I wrote the program in a very non-functional way. Below is the code in javascript using node.js. This code is long and a bit involved in my opinion.

Then quickly, after the interview, I started to think how I could have done this in a functional way and saved time and space. At that moment, it dawned on me how functional programming can really help. Below is the gist of how I did the same thing in functional style. The code is half the size and easy to read.

So, the benefits of functional programming that I found were:

  1. Succinct Code
  2. Easy to read
  3. Reusable functions
  4. Easy to manipulate data structures

So, if you haven’t jumped on the functional side of programming yet, I highly recommend to take a peek at it. It indeed takes you to a more functional world!