The term monkey patch seems to have come from an earlier term, guerrilla patch, which referred to changing code sneakily – and possibly incompatibly with other such patches – at runtime. The word guerrilla, nearly homophonous with gorilla, became monkey, possibly to make the patch sound less intimidating.1
An alternative etymology is that it refers to “monkeying about” with the code (messing with it).
Despite the name's suggestion, the "monkey patch" is sometimes the official method of extending a program. For example, web browsers such as Firefox and Internet Explorer used to encourage this, although modern browsers (including Firefox) now have an official extensions system.2
The definition of the term varies depending upon the community using it. In Ruby,3 Python,4 and many other dynamic programming languages, 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 desired. 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.
Monkey patching is used to:
Malicious, incompetently written, and/or poorly documented monkey patches can lead to problems:
The following Python example monkey-patches the value of Pi from the standard Python math library to make it compliant with the Indiana Pi Bill.
"Glossary — Definition of 'Monkey patch'". Plone Content Management System. Archived from the original on 2021-01-22. Retrieved 2021-07-02. when someone created a guerrilla patch very carefully and tried to avoid any battles, they tried to make it sound less forceful by calling it a monkey patch https://docs.plone.org/appendices/glossary.html#term-Monkey-patch ↩
Guha, Arjun; Fredrikson, Matthew; Livshits, Benjamin; Swamy, Nikhil (22–25 May 2011). "Verified Security for Browser Extensions". 2011 IEEE Symposium on Security and Privacy. pp. 115–130. doi:10.1109/SP.2011.36. ISBN 978-1-4577-0147-4. 978-1-4577-0147-4 ↩
Nutter, Charles Oliver. "Refining Ruby". Charles Oliver Nutter. http://blog.headius.com/2012/11/refining-ruby.html ↩
Biswal, Bimal. "Monkey Patching in Python". Software Technology Tips. Mindfire Solutions. Archived from the original on 22 August 2012. Retrieved 9 December 2013. https://web.archive.org/web/20120822051047/http://www.mindfiresolutions.com/Monkey-Patching-in-Python-1238.php ↩
Zakas, Nicholas C. (2 March 2010). "Maintainable JavaScript: Don't modify objects you don't own - Human Who Codes". Human Who Codes. https://humanwhocodes.com/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/ ↩
"New in Rails: Module#alias_method_chain". Ruby on Rails. https://rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain ↩
"Tainted kernels — The Linux Kernel documentation". www.kernel.org. Retrieved 2020-07-12. https://www.kernel.org/doc/html/v4.15/admin-guide/tainted-kernels.html ↩
Paul, Ryan (2009-05-04). "Mozilla ponders policy change after Firefox extension battle". Ars Technica. Retrieved 2020-07-12. https://arstechnica.com/information-technology/2009/05/mozilla-ponders-policy-change-after-firefox-extension-battle/ ↩