Functional Programming JavaScript – How to Write Manageable Code

code-qualityfunctional programmingjavascriptnode.js

I just started with functional programming (with JavaScript and Node.js) and from the look of things it looks as if the code I am writing would grow to be one hell of a code base to manage, when compared to programming languages that have a sort of object oriented paradigm.

With OOP I am familiar with practices that would ensure your code is easily managed and extensible. But am nore sure of similar convention with functional programming.

Best Answer

In practice, a large functional codebase will still need to be divvied up in some sort of structural system. If you are comfortable in OOP, classes remain the natural structuring element. You strive for "functional objects": conceptually related functions are placed together in a class. Of course, you avoid non-conversational state and reliance on instance variables.

Going beyond structure, there are certain techniques, such as partial function application, that are preferred in the functional world for extension, maintainability, and generalization. Learning to recognize these opportunities can be difficult when working in a hybrid functional/object mode, though. Personally, I think it is a good idea to work in a hybrid mode but to study and play in a more pure environment, such as that provided by Haskell.