Friday, October 15, 2010

Fear of State

I've been fooling around with the Node.js native MongoDB driver for a month or so now. I love the concept but the interface lets me down.

When I ask about it everyone wants to show me object oriented wrappers, and every one of those wrappers has timing flaws requiring discipline to call things and access things at the right time. Two made it possible to try to use a database connection object to access a collection before the database connection had been made. Most left data in an object but required you not to try to access it until the asynchronous fetches complete.

Deeply unsatisfying, deeply risky, particularly when the sample code doesn't have those problems. It has a different problem of egregious redundant code...

I think the sample code is on the right track and the object wrapping style is a red herring. Passing functions to functions, and for reuse use higher order functions which take your unique code as an input functions and return a function which wraps them up with the required boiler plate.

Functions all the way down; the functional style appeals to me. The functional model presents the state to the appropriate handler when it's available and not before. But the interface makes it hard to write this style and led me to hacking the driver to make it easier. Why is it hard and what was I doing to make it easier?

Essentially through all these apparently nested but actually asynchronously activated functions there is a need to pass forward certain objects: the database connection, the collection handle, cursors, and similar things.

I wanted to create my unique code first and pass it in, but it needed to pick up state along the way and use that state several levels of asynchronous calls further in. So I made the functions pass on parameters, passing in a bit of context and getting it back a parameter to the callbacks.

I'm not going to maintain the hack so I'll abandon that project and try again some other time.

Don't ask the users of your library to look after objects that have special rules about when and how to use them. Make it easy for them to get what they need at the right time, make it hard for them to get things when it would be the wrong time to access them.

It seems Uncle Bob agrees with me it seems.