Monkey Patching

MonkeyPatching is the act of changing code that is not in your code base. (by replacing preexisting methods, not by extending classes, at runtime.)

This practice is possible in languages that allow "reopening" class definitions or replacing existing methods, often at runtime.

Not related to CodeMonkey


Micro history

Monkey patching, while not described in those terms, was first used in Smalltalk (need ref).

Other languages using it: Ruby, Python, ???


Uses of MonkeyPatching

There are many reasons why people do that. Here are some on the top of my head :

Patching external projects:

Coding "DSL": Malicious intent:


Risks of monkey patching

The security risk apart, the biggest threat of monkey patching is that it can lead to hard to debugging code. The code describing an object is no longer only in one place. It can lead to long debugging sessions where the developer doesn't understant why the code and the runtime are behaving differently. Over the time, the code base also become increasingly difficult to maintain ; the monkey fixes are still in place while the library has fixed it's code, and no-one remembers why the fix is there in the first place ; the developer must also be aware of all the places where the code lies (keeping job by obscurity). Finally, money patches often use internal data structures, which is agains the common usage of abstraction.


EditText of this page (last edited May 18, 2009) or FindPage with title or text search