Lets Get Functional

The time of Object Oriented Programming (OOP) ruling the landscape seems to be coming to an end and although a lot of the alternatives are not exactly new they do offer some paradigms which were missing and at the very least encourage a little more flexibility within the development space.

OOP in general is pretty good and it is a very easy concept to follow as it takes advantage of the real world and how we construct complex systems in the physical world. I think in many ways this is the beauty of the pattern you are able to say everything around us is made up of discrete parts, where many overlap and can be used time and time again some are the parts that make that particle item unique. An obvious example for this is vehicles, there are many different types and they all have an engine but will differ in many areas and create countless subclasses some being entirely different others just with superficial differences, pretty much how development is.

OOP is easy to understand and it generally works fairly well, but at the same time people have started to grow tired of inheritance. People have been complaining about OOP for a good few years and really it is past time that we try different things for different situations. Now of course JavaScript isn't really Object orientated at all, even the class system is just syntactic-sugar  for its prototypical model, as a result JavaScript has a different approach, however JavaScript is also a little too flexible but this is where being more functional just might help.

JavaScript is not fully functional, really, it isn't fully anything it is a somewhat mismatch of different ideas with not much restriction and this is where the functional nature of JavaScript can help in making more readable and understandable functions that are easy to test.

One of JavaScripts biggest boons is how it treats functions as first class citizens, allowing you to pass them as parameters, allowing you to return them and as a result you are able to chain functions together to create very reusable functions that are split logically. A simple example of this is for a logging function. There are a lot of different log types with different outputs and styles, however the majority of the code is the same. The base function may look something like this:

const myLogger = logger => type => message => 
logger(`${type} - ${message}`)

The beauty about functional style is that the above function is very general and easy to reuse in many different situations as you could easily create basic console logging functionality as such:

const consoleLogger = myLogger(console.log);
const debugger = consoleLogger('Debug');

debugger('This is a debug message');

This naturally allows you to break up the functionality of the function into increasingly specific parts that allow reuse at different parts of the functionality along with making for small very easy to test functions.

Maybe you want to add a new logging platform, that is easy with this model as you can just write in a new function replacing the console.log and it will work just as expected. Maybe you also have more types that is easy to add in to make for more specific sections easily. As everything is being passed into the functions it is also very easy to test and mock any step along the way which is great as every step of this is easy to test and work with.

JavaScript can't really be fully functional as you involve DOM manipulate, however it is possible to write more and more functions that are more functional and as a result more predictable. Although functional programming is nothing new it helps provide more readable code and naturally splits up functions along with making them very easy to test due to the fact everything is being passed along.