Yep, “MonkeyPatching” is the hot new word of the day. All the cool bloggers are dropping it left and right. I’d never heard the term til a few weeks ago. Now it seems I can’t go a day without reading it in at least a couple blog posts.
But until this morning, I didn’t really know what it meant. I had my own invented meaning – something vague about writing some hack to fix someone else’s broken code. “Dude, I totally had to monkey patch that data grid to get the cell renderer to scale correctly.”
In fact, it has a very specific, technical meaning. From WikiPedia:
A monkey patch (also spelled monkey-patch, MonkeyPatch) is a way to extend or modify the runtime code of dynamic languages (e.g. Smalltalk, Javascript, Objective-C, Ruby, Perl, and Python) without altering the original source code.
This all came about after reading this post at Coding Horror.
My initial thoughts on reading this was that the ActionScript community is running an almost directly opposite timeline from these guys. Back in ActionScript 1.0 days, MonkeyPatching was the epitome of hardcore programming. Remember prototype? 🙂 Remember layer51.com? (It’s still there.) Layer51 is a massive repository of AS1 MonkeyPatches. You want to know if two different arrays are equal? Grab the Array.equals() method (by senocular):
[as]Array.prototype.equals = function(a){
if (this == a) return true;
var i = this.length;
if (i != a.length) return false;
while(i–) if (this[i] != a[i]) return false;
return true;
}
/*\* EXAMPLE ****\
a = [1,2,3,4];
b = [1,2,3,4];
c = [1,2,3,5];
d = a;
trace(a.equals(b)); // true
trace(a.equals(c)); // false[/as]
Layer51 sports 370 MonkeyPatches to MovieClip. The thing I didn’t like about using such “prototypes” (as they were often misnamed) was that they get added to every instance of the class you are patching. For example, if you use the double click patch, every single movie clip in your movie now has double click functionality. And since there were no dynamic shapes or sprites back then, pretty much everything was built on movie clips. Chances are you only had one, or maybe a few movie clips you actually needed to have this functionality. So it falls into the “Excessively wide in scope” category as mentioned in the Coding Horror link above.
These days in the ActionScript community, any mention of adding functionality via prototype is usually mentioned as an inside joke. I know prototype still exists in AS3, but I honestly don’t know if it would still be possible to use it in the way we used to, because nobody even seriously talks about doing so. We seem to be very proud of the fact that ActionScript has gotten less and less dynamic as time has gone by. Heck, in AS3, private properties are really private even. Strict typing is enforced at run time. The MovieClip class is still marked as dynamic, for old time’s sake, but most other classes, including the more commonly used Sprite, are not.
Anyway, I just thought it was funny that while developers in these other languages, including c#, are discovering the joys of MonkeyPatching, we are leaving it behind more and more. Are we way ahead of the curve or way behind it?