Prototype Epiphanies


In between washing the salt spray from my glasses and fretting about whether the surf would succeed in taking over the small allotment of sand that I am paying through the nose to lounge atop in a rather finicky contraption of white plastic whose sole purpose in life is to (rather unsuccessfully) place some distance between my towel and the aforesaid surf, I had a sort of epiphany regarding Prototype.

I will freely admit that JavaScript and Ajax isn't the sort of thing the average person would mull over while stealing a glance over Hugh Laurie's The Gun Seller to see whether there would be time to turn a page before the next wave came in and drenched the towel, but in my defense I was trying steadfastly to ignore the hordes of rampaging beach goers that persist in being loud, British, or both, and have had to put aside a lot of interesting things during the past year, one of which is the Ajax/Web 2.0 frenzy.

Bugs Under The Microscope

Being somewhat of a pragmatist, my approach at catching up on Web 2.0 has so far largely consisted of puttering about the newest, trendiest sites and trying to understand why I did not feel like part of their target audience, which is apparently spilling out as much teenage angst as possible in the form of badly lit camera-phone videos, posting impossibly legal MP3 files of their favorite bands, letting everyone in the planet know what they are doing right now (again, via badly lit camera-phone imagery) or link to someone else's "self-generated content" and subsequently generate swarms of meaningless commentary that closely rival the average post-moderation score of a randomly picked Slashdot or digg comment.

All of which, of course, to the tune of gazillions of web advertising "impressions", which is where the real money is.

Regardless of the finantial/business/bubble aspects of that pursuit (which I'm keeping to myself, since I like to form my own, preferably unbiased opinion of the companies involved rather than add momentum to the Hype cycle) so far I have arrived at three separate and yet not totally disconnected conclusions:

  1. Web 2.0 is a usability nightmare (sites are designed to be "cool", have "exciting" interactive features or mimic desktop UIs, all of which are inherently wrong - and dumb - approaches).
  2. Content tends to be a lot harder to find or link to (how do you index or link to the third item on a collapsible section of a page that is completely assembled on the client side by the browser?).
  3. There are entirely too many JavaScript libraries in use, none of which are both widely encompassing in terms of functionality or truly cross-browser.

So, in between strenuous efforts to remove sand from between my toes and trotting down to the beach to ensure that I could have another go with more sand, I have tried to catch up with the state of the art in JavaScript coding, both because it is a welcome change from what I usually do and because it is a sort of character-forming exercise.

You see, I hate the language with a passion heretofore reserved to those British vacationers who make a point of sitting around popping their beer cans while their immense wives shout at the kids not to eat the sand, and "George why don't you keep an eye on them for chrissakes?" and who have, repeatedly and over the course of several years, managed to turn the Algarve into what is probably the southernmost English-speaking territory in Europe short of Gibraltar - this judging from the vast numbers of small restaurants with names starting with "Ye Olde" and advertising eggs, bacon and baked beans.

Ahem.

Barbarians At The Gate

Anyway, I've always considered JavaScript as a sort of bastard language that had no place in the civilized environs of the web, since old-school thought mandated that any decent sort of interactivity ought to be done server-side (which is where application state ought to reside), or, if absolutely necessary, parceled out between the twin evils of Java and Flash.

These, despite their foibles, shared the sole redeeming aspect of being somewhat outside Microsoft's peculiar way to integrating them in their browser, and were therefore assumed to be completely cross-platform, stable, and good targets for development, a notion that Sun and Macromedia soon helped dispel by churning out multiple updates over the past few years (my gripes about Sun's management of the evolution of Java into successively incompatible and fragmented sub-platforms are well-known and the stuff of legends told around the camp fire by disgruntled IT folk that shared - or rather, hogged - CPU time with me, but I won't repeat them here).

JavaScript, with its extremely poor debugging capabilities (yes, yes, I know about WebKit and Drosera, bear with me), the schisms between the Netscape and the Microsoft DOM camps (starring Opera in several cameo roles) and its almost minefield-like ability to fail to work as intended across several browsers (i.e., you know it's going to fail, but you don't know which click will trigger it), always reminded me somewhat of a gang of thieves that, having stolen into a residence by the front door and appropriated a C-like syntax and an object model consisting almost entirely of twisted cutlery and broken wire hangers, tried to leave through several different upper floor windows at once and land on the garden gnomes.

So it was with understandable amusement that I started spotting hints of Zealotry between web developers who aligned themselves with the rank and file spearheading this or that JavaScript library. How could a language so utterly broken (regardless of being an ECMA standard or not) have evolved to a point where it was actually feasible to do fancy DOM manipulation and expect it to work reasonably well across several browsers, and yet have so many different (and passionate) approaches popping up?

The Whys And Heretofores

And the answer is, by and large, because no two programmers code alike. Unless they're identical twins kidnapped by UFOs and telepathically linked to an extent where they can play Mozart at double-time synchronously in separate rooms, or something.

But worse than that, because all programmers would love to be virtuosos in some fashion, and therefore try to stamp their creations with some sort of defining theme. In Dojo, the emphasis is on keeping the namespace clean and on delivering complete, functional components, in moo.fx it's on size, and in Prototype... well, that's the reason for my little epiphany.

Prototype gratuitously mucks about with JavaScript innards in a way that some people thoroughly dislike, and I can't say I blame them. Nor can I, in all conscience, blame those who (like myself) find the core Prototype "documentation" (or lack thereof) appalling, and the script.aculo.us Wiki to be less than useful where it regards understanding all the fancy (and largely useless) effects it has spawned.

Truth be told, however, that Dojo's documentation is brilliant at detailing every little aspect of its API, but just as useless at explaining how to chain them together. In fact, until they started drafting the book online, it all reminded me of the MSDN library and my many quests to find meaning in its arid wasteland of function calls and punctiliously defined parameters.

One of Prototype's saving graces (or turn-offs, depending on your viewpoint) is its association with the rampaging hordes of Ruby on Rails, that have, like the proverbial million monkeys, made it almost trivial to use (or overuse), turning that off-yellow color so characteristic of Rails div updates into the new black.

But while being a right pain to use (in any decent scale) outside Rails, and even lacking the level of coherence afforded by something like the Yahoo UI Library (which bundles in some thoughtful snippets of UI design that appear slightly better thought out than the equivalents in Rico), Prototype does one thing right - it turns JavaScript into something you can use without trying to mask it or treat it like another language.

Bareback Riding

The gist of my little epiphany was, therefore, that Prototype, by way of its virtuoso hackish nature and the way it forced you to actually step through the code (see, I told you I knew about Drosera) and understand how it worked made it enjoyable to use JavaScript as JavaScript and not like something else, which is one of the things I, personally, never marshaled the nerve to attempt.

Despite JavaScript being a broken, hopeless language, Prototype's trickery makes it fun to create event handlers and separate presentation from content your way, and not someone else's way.

Sadly, that happy, all-encompassing mind-state (from which deeper truths would likely emerge in due time) did not withstand the horrified shriek that issued from the neighboring lounge chair as a bee alighted from an empty can of lager and tried, in its addled state, to make friends with what it perceived as a vast, rosy (and likely peaceful) mammal.

Fortunately for the little creature the surf finally reached our row of lounge chairs, causing a not unwelcome diversion (and, in time, our neighbors' most welcome retreat).