From: Bob Hutchison Date: 2008-02-25T02:07:30+09:00 Subject: Re: Monkeypatching is Destroying Ruby On 24-Feb-08, at 11:24 AM, Avdi Grimm wrote: > On Sun, Feb 24, 2008 at 11:17 AM, Bob Hutchison > wrote: >> Aside from that, there isn't a lot to argue with in your blog >> posting. >> Your points individually are fine and quibbling over insignificant >> details on a mailing list is a waste of time (though it might be >> ideal >> in a pub :-) In other words, I don't disagree with anything you say >> until you start drawing conclusions. I *like* monkey patching and I >> approve of its proper use. Furthermore, I don't special case the >> abuse >> of MP, it is just like any other abuse of a feature: bad. > > Thanks. I think the reason I'm making a special case of monkey > patching is that, at least in some subsets of the community, I'm > seeing it morph into almost a standard extension technique. How do > you extend ActiveRecord::Base? Why, you re-open it, of course! > That's how everyone does it! An analogy maybe... it can be remarkably difficult to get new programmers to use composition rather than inheritance. This is dealt with through education (occasionally a brutal education :-) Monkey patching *is* a standard extension technique. It isn't just a hack. At least, that's what I say :-) If you design a class that is to be used in different contexts then it is reasonable to assume that the class's responsibilities may change with context. This change in responsibility may not be reflected precisely by either inheritance or composition (or forwarding) -- inheritance and composition may be something of a contortion. You can make a pretty strong argument that you shouldn't be abusing inheritance or composition to avoid something neatly expressed by MP. Of course someone can use this interpretation of MP to justify a lot of dubious stuff. Ruby has other techniques that are not so much talked about that can deal with some of the suspicious uses of MP. These come to mind immediately: dynamically including modules into a class or specific *object* (there are some handy hooks defined in Ruby that allow some really nice stuff to be done when this happens), and methods defined on specific *objects*. These are open to abuse too, and can be spectacularly difficult to debug if you are thinking that an object is defined by its class. > > > This could be more of a Rails thing at the moment, but as I commented > earlier, Rails culture will increasingly become Ruby culture, because > that's where the new Ruby programmers are coming from. I agree I think. I believe that it is very important to distinguish between a language's capability to do something and its use/abuse. The trouble with Rails, and its tremendous success, is that less experienced programmers can get a *long* way before they really need to know anything about programming. Somewhere in that interval is room for some pretty ugly code -- it isn't just MP. Cheers, Bob > > > -- > Avdi > ---- Bob Hutchison -- tumblelog at http://www.recursive.ca/so/ Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/