Saturday, April 24, 2010

The importance of bikeshedding

I really like what's going on with CommonJS. I love how an ad-hoc standards group has formed around a real integration problem. But I'm embarrassed to say that I haven't been following it. Until now, I haven't been worried about interoperability. But now I really want to port msjs to node.js, and I'm paying attention.

I'm similarly embarrassed to say that before reading this thread, I wasn't familiar with the use of "bikeshedding" as a gloss on Parkinson's Law of Triviality. But while I really like the formulation, I'm concerned about the way it can be used as rhetorical device to dismiss valid concerns about the semantics of an API.

I suppose I'm in the Sarah Allen camp on this: language shapes thought. I think the name "exports", which begat "module" was a wrong turn. To me, it makes more sense for the other side of the "require" API to be a corresponding function.


I like "publish" for this. I propose this kind of implementation (minus locking)


var runModule = ...//implementation dependent
var bindings = {};
var require = function(moduleName){
    //circular dependency check
    if (bindings[moduleName] == UNPUBLISHED) throw ...;

    //Run script if no binding for module
    if (bindings[moduleName] === void 0){
        bindings[moduleName] = UNPUBLISHED;
        try {
            var moduleScope = {
                require : require,
                publish: function(value){
                    //error to publish twice in same module 
                    if (bindings[moduleName] != UNPUBLISHED) throw ...; 
                    bindings[moduleName] = value; 
                }
            }
            runModule(moduleName, moduleScope);
        } finally{
            if (bindings[moduleName] == UNPUBLISHED) {
                //OK, module didn't publish anything.
                //It will run again.
                delete bindings[moduleName];
            }
        }
    }

    return bindings[moduleName];
}

I guess I'm also psyched about the new bike shed, but I do care what color it is.

No comments:

Post a Comment