The one objection I _haven’t_ heard yet to my monkey patching rant is: you’re a “hypocrite”:[http://utilitybelt.rubyforge.org/svn/lib/utility_belt/convertable_to_file.rb]!
*Gasp*, yes, I have written code that exploits Ruby’s open classes. It even extends @Object@, the core-est of the core! And then shamelessly contributed it to a publicly available gem!
As anyone who read past the title of my article should know, I don’t think monkey patches and open classes are unmitigated evil. I even use them myself. In this case, I happen to think it was justified. UtilityBelt is essentially a collection of MonkeyPatches. It’s entire usefulness lies in providing convenience methods that require as few keystrokes as possible, since they will be used from the command line. And it is intended for a very special environment – IRB. Many Ruby hackers have hacks in their .irbrc that they would never include in a production application.
So does this mean I think that I should have power of approval over when monkey patching is justified?
No. Ruby is a powerful language. That’s why I use it. That’s one of the reasons that when I set out to find a new job, I looked for one where I could use Ruby every day. And with power comes “danger”:http://www.jamesbritt.com/2008/2/25/monkey-poaching-is-distracting-ruby.
But because I work with Ruby every day, on large, real-world projects that use a lot of third-party libraries, I also come in daily contact with the annoyances and hangups that are caused by the misuse of that power. And lately I’ve been noticing that the majority of those irritations stem from misuse of open classes, and that the majority of those misuses are *completely avoidable*.
I’m not going to tell people what features they can and can’t use. But I’m also not going to hold back from commenting when I see a clear trend emerging, a trend with a completely predictable outcome. Pile all of your extensions from a myriad sources into a few pitifully overburdened classes, and there *will* be collisions, which *will* be a pain to track down when they start causing inexplicable bugs. And all of this will have been, once again… completely avoidable.
I’m not out to stifle anyone’s “creativity”:http://weblog.raganwald.com/2008/02/1100inject.html. I don’t believe, however, that there’s necessarily any tension between promoting creativity and promoting wise practices.
All I’m saying is this: before re-opening a class, _did you go through the rest of your toolbox first_?