Table of contents
Definition
Monkey patching involves overriding the default functionality of code with another during execution ie: (runtime [→]).
Use Cases and Examples
01: //output before override
02: console.log("hello"); // hello
03:
04: //overriding default action
05: console.log = function(input) {
06: return "Output is: "+input;
07: }
08:
09: //output after override
10: console.log("hello") // Output is: hello
The above Javascript code shows the output of the console.log
function before and after its monkey patched, with an extra string (Output is:
) attached to the output after its patched.
Summary
There are a few good use cases for monkey patching in programming, for example, we can mute all console.log
outputs when in production by just patching it up. There are however many instances where monkey patching can lead to code that is hard to work with.
Monkey patching could mean overriding well-known functionalities which could lead to confusion among co-developers and potential bugs [→] in your code.
If you ever think of monkey patching there is a chance something wasn't done right in your code, to begin with.
Here is another article you might like 😊 What Is Open Source?