Esbjörn Blomquist
2 min readJul 1, 2021

--

Hello Derek! Thanks for feedback and the question!

Well, yes and no. It is definitely different, but you are right that it is gluing operations and data together again. In that sense it is a middle ground of sorts, but I wouldn’t put it that way.

It is instead a completely different pattern, coming as an extension of pure functions when persistent state is needed and you choose to glue some logic and a state together in a thin layer that references operations, getting something that is both much simpler and much more flexible than a class.

The only logic that the Functional Style Factory is adding is the handling of the state. The actual meat of all the operations are in pure functions, easily testable without the factory. You can do this with classes too (rarely seen), but you will probably be stuck with other bad patterns like inheritance instead of composition.

But again, as I also write in that chapter, the logic for some of the state management might not be well placed, so there might be a better pattern.

The benefits of keeping state as a completely separate data structure is so great that Redux became the most used state manager in JavaScript. The concept of the reducer is extremely powerful and yet so very simple. The reducer is just another pure function.

I would however like to end with warmly recommending XState. It is growing in popularity very fast and is getting much tooling around it. It is internally separating data and logic and reducer-like functions are used to update the state. It is also event-based, which lowers coupling greatly compared to calling functions directly.

It is an interesting note that the creator of OOP has pointed out that the whole class structures with inheritance and tight coupling was not at all what he meant with OOP. His central pattern was messaging. One could argue that the actor model that XState can leverage is true OOP (in a good way).

--

--

Esbjörn Blomquist
Esbjörn Blomquist

Written by Esbjörn Blomquist

Software architect and developer with web and functional JavaScript close to his heart. Worked many years with web solutions mainly within the IoT field.

No responses yet