Functional Programming in Javascript

In this post I will attempt to articulate the reasons and goals I have for using Functional style programming in javascript.

I’m building a distributed event sourcing system in node.  The part I’m focusing on now, is the event dispatcher.  It receives events from the eventstore, determines if they are relevant and hands them to the appropriate eventhandler.

While I have already written the piece, I wrote it in an OO manner with inheritance, fancy es6 features, and lots of imperative code.  I found that there were so many small pieces of fairly simple logic, that it was very easy to make mistakes or miss possible error scenarios.  I wrote shit tons of logging code and ran complex unit and integration tests and watched the logging output.

My pain points were lots of if-then logic, lots of null check logic, lots of stream parsing logic, lots of objects with state and functionality, and trouble with inheritance using es6 classes.

Functional programming, as I started reading up on it, addressed each of these issues.

Using Maybe I avoided ( deferred and centralized really ) if-then logic and null check logic, and error handling .  My functions were more numerous, but smaller, stateless and easy to test.  I found that I could build up a workflow reusing the composite pieces then simply call the function rather than invoke the behavior from the various pieces that contained the state.  As I was not mutating state this also reduced the mental overhead of the app.

In the next post I will start to introduce the pieces that made this possible.  If this is a topic that you are interested in, I invite you to visit Functional Programming in Javascript a google group for discussing this style of programming.

Functional Programming in Javascript

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s