On Functional vs Object Oriented Programming

Starting out programming, I always found Object Oriented Programming to be awesome. It totally made sense, you could model your real life in code and data so easily. 

You have your class object that has class methods that did operations on the object, you have your class attributes that you can update. Anything and everything is fair game. But I recently read a good quote about how Object Oriented programming is great for large corporation because it allows you to monkey patch existing bugs and issues to objects.

This view of OO programming got me to thinking and digging more into functional reactive programming. Stuff such as immutable data, reactive declarative programming, monads, etc, etc kept calling my name. Though the concepts of FP are definitely foreign and note as easily relatable as OO, there are some awesome concepts and guidelines around it that make things feel so simple.

Rule: Data is immutable, it cannot and should not be overwritten.

If you want to update a value on a data object, you must create a copy and trigger a change related to the object in memory/storage.

Rule: Code should be reactive and easy to debug.

This means convoluted, hidden functions on specific objects were a no-no.

RuleCode should be readable and easily maintainable.

Code should be clearly expressive and obvious as to what it does.

ES6 which is Javascript's new version is supporting even more features that support a more functional, reactive approach to programming. Thing such as monads, generators all defer to a more declarative approach to programming.


Here is a great example of using Javascript Streams with functional programming using highland.js.


_([1,2,3])
 .append(4)
 .append(5);  
// returns [1,2,3,4,5]


_(dataObject)
  .map(JSON.parse)
  .filter(underThree)
  .map(util.format)
  .pipe(toConsole);