JavaScript Reusability Is A Quagmire
Recently my involvement with a project with Chris Wanstrath and general association with Toby Sterrett and Ian Collins has led me to start getting serious about JavaScript skills. And in reading a few books, I’ve been exposed to a lot of opinions on the subject of “good” js coding.
And my reaction thus far has been one of shock. “JavaScript: The Good Parts” details four distinct ways to do OO, all of which can be found in the wild. Literally half the book is an apology for the other half, “This is awful, but hey first-class functions are swell!”
But, lots of languages have badness “on paper”, I didn’t assume it was going to be a quagmire as I entered into my project with Chris. But right off the bat, we’re running into problems. I’d sure love to use OMeta-js, but that’s not allowed because it uses Prototype, which poops on things that Chris’s own library needs. I’d love to use some other code I found, but it uses a different kind of OO instantiation and it creates some weird code ugliness extending those objects within our own framework.
So not even a week in and we’re already hitting compatibility issues and reinventing the wheel numerous times. What the fuck is this? Even using Scheme didn’t hit me in the face with so many incredibly obnoxious compatibility details. Before you can use anyone else’s code you need to do what amounts of a code audit just to see what kind of core hacks they rely on, what libraries they use, and how they modify the core primitives. And I say this without qualification because nearly everyone modifies the core primitives (they’re almost unusable, otherwise!).
JavaScript is particularly vulnerable to this phenomenon because, as far as the literature indicates, it ships in a basically unusable state. The OO model is totally schizophrenic, the data structures are hard to use, the for-in loop is basically worthless but dangled out like candy. The only thing that isn’t a total fsckup is the DOM model stuff, and the event handling. And even then, you’d need to form some of your own event mechanisms out of the primordial JavaScript clay.
My real question is, “Why do JavaScript developers accept this?” I’m already reading through upcoming JavaScript specs praying I’ll find some inkling of things like block scoping, a revision to the object model (let’s just make it like Self’s, please… we’re already halfway there). And even if that’s 6-10 months out, why don’t more people start to standardize on one consistent strategy? It seems like js base libraries are a dime a dozen, and even in the equivalent of the “Local Farm Grown Organic” bin of the JavaScript base framework section we still see a ton of variety, JQuery and Mootools and Prototype, each doing things significantly differently and only sorta playing nice with each other.
The funny thing is, people use JS every day to do real and interesting work, and I’d almost be willing to say that things like MochaUI are getting close to being viable alternatives to GUI toolkits. But fuck, can we please get JS’s act together and get rid of its no-man’s-land of compatibility issues (and this isn’t even browser issues, this is right down in the spec) soon?