Monkey patch

Monkey patch

A monkey patch is a way to extend or modify the run-time code of dynamic languages without altering the original source code. This process has also been described as "duck punching".[1]

Contents

Etymology

The term monkey patch seems to have come from an earlier term, guerrilla patch, which referred to changing code sneakily at runtime. The word guerrilla, homophonous with gorilla, became monkey, possibly to make the patch sound less intimidating.[2]

The definition of the term varies depending upon the community using it.

In Python, the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as you desire.[citation needed] Other forms of modifying classes at runtime have different names, based on their different intents. For example, in Zope and Plone, security patches are often delivered using dynamic class modification, but they are called hot fixes.[citation needed]

In Ruby, the term monkey patch means any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime.[citation needed]

Some members in the Ruby world started adopting the term duck punching in lieu of monkey patching, originating from the extensive use of duck typing in Ruby and Python.

Applications

Monkey patching is used to:

  • Replace methods/attributes/functions at runtime, e.g. to stub out a function during testing;
  • Modify/extend behaviour of a third-party product without maintaining a private copy of the source code;
  • Apply a patch at runtime to the objects in memory, instead of the source code on disk;
  • Distribute security or behavioural fixes that live alongside the original source code (an example of this would be distributing the fix as a plugin for the Ruby on Rails platform).

Pitfalls

Carelessly written or poorly documented monkey patches can lead to problems:

  • They can lead to upgrade problems when the patch makes assumptions about the patched object that are no longer true; if the product you have changed changes with a new release it may very well break your patch. For this reason monkey patches are often made conditional, and only applied if appropriate[3].
  • If two modules attempt to monkey-patch the same method, one of them (whichever one runs last) "wins" and the other patch has no effect, unless monkeypatches are written with a pattern like alias_method_chain[4].
  • They create a discrepancy between the original source code on disk and the observed behaviour that can be very confusing to anyone unaware of the patches' existence.

Even if monkey patching isn't used, some see a problem with the availability of the feature, since the ability to use monkey patching in a programming language is incompatible with enforcing strong encapsulation, as required by the object-capability model, between objects.

See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Monkey-patch — Une Monkey Patch (aussi nommé Monkey Patch, MonkeyPatch) traduit par modification singe est une façon de modifier ou d étendre du code qui sera exécuté sans modifier le code source original pour des langages de programmations dynamiques. Il s… …   Wikipédia en Français

  • Monkey Patch — Une Monkey Patch (aussi nommé Monkey Patch, MonkeyPatch) traduit par modification singe est une façon de modifier ou d étendre du code qui sera exécuté sans modifier le code source original pour des langages de programmations dynamiques. Il s… …   Wikipédia en Français

  • Monkey patch — Une Monkey Patch (aussi nommé Monkey Patch, MonkeyPatch) traduit par modification singe est une façon de modifier ou d étendre du code qui sera exécuté sans modifier le code source original pour des langages de programmations dynamiques. Il s… …   Wikipédia en Français

  • Monkey-Patch — Une Monkey Patch (aussi nommé Monkey Patch, MonkeyPatch) traduit par modification singe est une façon de modifier ou d étendre du code qui sera exécuté sans modifier le code source original pour des langages de programmations dynamiques. Il s… …   Wikipédia en Français

  • monkey patch — 1. noun A patch which alters the behaviour of a program by forcefully overriding aspects of the original code at runtime. 2. verb To alter the behaviour of a program by forcefully overriding aspects of the original code at runtime …   Wiktionary

  • Monkey Thieves — Logo Monkey Thieves, also known as Rebel Monkeys, is a documentary series produced by National Geographic, examining the habits and social interactions of Rhesus Macaques in the city of Jaipur, in northwestern India. Monkey Thieves follows a f …   Wikipedia

  • Monkey tree phenomenon — Monkey tree redirects here. For the tree popularly known as the Monkey puzzle Tree, see Araucaria araucana. The monkey tree phenomenon is a social phenomenon in Singapore, which began in September 2007. It arose from the discovery of a callus on… …   Wikipedia

  • Monkey Island — Titellogo Monkey Island [ˈmʌŋki ˈaɪlənd] ist eine fünfteilige Grafik Adventurereihe nach einer Idee von Ron Gilbert, der auch maßgeblich an der Entwicklung der ersten beiden Teile beteiligt war, bevor er in den frühen 1990er Jahren die Firma… …   Deutsch Wikipedia

  • Common squirrel monkey — Conservation status Least Concern …   Wikipedia

  • Colombian spider monkey — Colombian spider monkey[1] Conservation status …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”