Understanding Monkey Patching

python, rails, ruby, software engineering 3 Comments »

I’ve noticed one of the problems I have writing this blog is that I prefer to have finished thoughts when I write up something, or at least to have a good understanding of a problem I am working on before committing it to ‘paper’. Unfortunately, this doesn’t lead to many updates. I’ll try to break this habit a little. Recently I heard the term ‘Monkey Patching’ after one of the Seattle Patterns Group meetings, in relation to Ruby on Rails.

A Monkey-Patch (also called Monkey Patch, MonkeyPatch) is a way to extend or modify runtime code without altering the original source code for dynamic languages (e.g. Ruby and Python).

Today, reading a blog entry from Chad Fowler, the term came up again with a Python developer saying:

You can monkeypatch code in Python pretty easily, but we look down on it enough that we call it “monkeypatching”. In Ruby they call it “opening a class” and think it’s a cool feature. I will assert: we are right, they are wrong.

When I read that, I felt almost relieved, because I was thinking the same thing. I can see a limited use for it, but it seems like something you should only do in dire circumstances, that it would be detrimental to good software engineering practices. I can’t prove this, nor am I totally convinced, but Ruby and Ruby on Rails in particular seems to play a little bit fast and loose. I suppose this fits in with Agility, but there does seem to be a mental divide between Python and Ruby people (even though they are really quite close linguistically). To date, I’m much more in the Python camp, but I’ve been deploying Rails apps at work, and I’ll be delving more into Ruby as I go forward. I have heard rumors that Zope does Monkey Patching, and this convinces me even more. Zope has almost single handedly destroyed Python’s reputation at my place of work. Thanks Zope!

Waiting For Python Web Frameworks

django, python, rails, ruby, turbogears 4 Comments »

I’ve tried out Turbogears and Django, ultimately putting together a quick prototype in Django, because the built in admin interface was the only thing left for me to complete.  It was simple enough to port over my object from Turbogears, add in some meta data and push the whole thing to “production”.

Unfortunately, I started coding in Django right before the “magic” removal branch went public.  The documentation didn’t mention anything about a massive API change that seems to require lots of changes to any code you write.  Not that it is too important to me, it will be faster for me to start over and move the methods into the new branch.  Still frustrating.

Of the two, I think Turbogears has the long term advantage.  I love the fact that someone took the time to integrate a bunch of seperate programs into a whole (it’s not quite coherent yet).  The desire to re-invent the wheel is strong with all programmers and I’ve done it myself a couple times.  Turbogears also is not quite ready, they have some nice widgets that will be a joy to use, but when I tried, I couldn’t find any documentation beyond the wiki.  The example in the wiki didn’t work with my build, so I decided I would come back to it after I had tried  Django.  Maybe after they come out with 1.0.

Which leaves me to ponder trying out Ruby on Rails.  Frankly, I’ve been avoiding it because I want to stick with Python.  I’ve been using Python for all my Unix Scripting and I love the way it is put together.  I’ve heard good things about Ruby, but I also want to deepen my Python experience, not dilute the languages I know with yet another one.  But considering Rails is past 1.0 and is gaining momentum, it would be a mistake not to look into it further.

There is no doubt in my mind that I ever want to go back to writing straight PHP if I don’t have to though.  These new frameworks make writing web applications a joy.  The thought of thousands of lines of poorly written PHP make me shudder.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in