Is Prototype evil?

Let me begin this critique by saying that I think Prototype JS is an inspired piece of work and that it inspired me to think about javascript in a whole different way. It’s also a stable framework that gets the job done and is used on a large number of websites. And it’s very comfortable programming when you know prototype is on the page, because it offers a lot of nice and handy feautures which are not natively available cross-browser.

So it must be good right?

Well… here’s where my story begins about what I don’t like about Prototype JS. But first, take a look at the source code for prototype 1.6.0. You don’t have to read it all, just glance at it and scroll through the file a bit. That’s a lot of code right?

This blog is about unobtrusive javascript… but I really can’t call prototype unobtrusive due to the sheer size of it. If unobtrusive code should not get in your way, how can a mammoth like prototype qualify?

Don’t get me wrong here. I don’t really care about the size of the file. I care about the size of the codebase. The number of objects and methods that it exposes and I thus should know about. Because unobtrusive javascript is about not getting in each other’s way. It’s about working together with other scripts on the page. By having a rather large API, with a fair amount of short identifiers in the global namespace, other scripts sort of have to ‘know’ that prototype is on the page and adjust for it. Just read about the dollar function clash between jQuery and Prototype JS.

But that’s not all of it. Just having a large API does not make prototype evil. But prototype is monolithic. It’s not a large set of modules in one file, though it may look like that. It may even be stored like that in CVS. But it’s not.

Some pieces of code may be worse than others, but the pieces of code that I have personally tried to pull out of prototype were very interwoven with the rest of prototype.

For example prototype tends to ‘enhance’ DOM objects and arrays using functions $() and $A(). Those functions then add some extra methods to otherwise native types. Those enhanced methods are happily called everywhere, but they are prototype specific.

Then there is the Prototype object that contains a load of useful properties and functions. Those are used throughout the code. Very convenient, but it makes all that code dependent on that object. A lot of times, the offending line of code could easily be replaced by an only marginally longer line of code that avoided the dependency.

The same goes for a lot of functions that prototype adds to native objects. Such as Object.isFunction(), which just returns typeof object == “function”. Ok so your checks become a teeny bit shorter and more readable. But it makes your code dependent on a function that is only available in prototype. It’s the spirit of coding that prototype was built with: no duplicate code. That’s good to some degree, but if it comes at the expense of modularity, I choose modularity. It’s the old story of tight cohesion, loose coupling. Prototype is tightly cohesive as a whole. I think they should have separated the code more into modules and introduce loose coupling between the modules. As it stands now, it’s monolithic.

And this monolithicness seems to be contageous. Code gets infected by it. Hey if you want to use function A from prototype, you need to include all of it, so you get function B for free as well. And since it’s there.. why not use it, right? And then, if your code is useful, why not include it right into prototype? This way, the framework never stops growing…

Prototype viral? Well that may be pushing it, but just read this article about pimping some code for inclusion into prototype that I think illustrates what I’m saying here, and draw your own conclusions.

I told you prototype inspired me. It was inspiring to see how much you can bend javascript to perform the way you want it. And when I realized it was monolithic, it inspired me to create a solution: Modular javascript with Packages JS.

I’ve given up on trying to make protoype modular. It’s just too interwoven for me to get any joy out of doing that. So instead I’m trying to build up my own personal framework of scripts I like using Packages JS. Give it a try!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s