Monkey Patching

What is Monkey Patching?

The Monkey Patch named, monkey patch because it allows you to change the behavior of the functionality runtime. You can think of monkey patching as patching a road to fix the inconvenience without blocking the road completely :).

It’s basically you are messing up the code at runtime and that’s why I think it is named as Monkey Patch.

Monkey Patch is basically possible in all kind of dynamic languages (e.g. JavaScript, Python, Ruby, PHP etc.) and mostly used in case of molding, extending third party code’s functionality to meet with their application requirements.

You can do following things with Monkey patching.

  • Replace Methods/attributes/functions at runtime.
  • You can modify or extend the behavior of third party code at run time without maintaining a private copy of the product's source code.
  • You can apply a patch at runtime to the objects in memory not on the source code.

 

In JavaScript we can achieve Monkey-Patching by Overriding or extending the methods behavior runtime without changing it's original source code.

Let's understand this by example.

 

function TestSaveData(){
    
    this.saveData = function(text){
        console.log("Saved Data : "+text);
    }
}
/*
     This is normal javascript function calling saveData from TestSaveData class.
*/
function save(){
    var test =  new TestSaveData();
    test.saveData("Hello");   
}

save();

 

Now let's take a hypothetical example, I am just trying to explain the Monkey-Patching concept, you can solve this problem with easy way. Let's consider that above functionality is from third party library and after saving the data you also want to audit the data somewhere in file. We can achieve this with help of Monkey-Patching we can change the existing behavior of saveData, which will Save the data and also Audit the information.

Simplest way is to override the functionality (i.e. copy of the TestSaveData code and modify it) 

function save1(){
    var test =  new TestSaveData();
    test.saveData = function(text){
        // old code, which is like copy-paste of the existing code. 
        // Assume that if line of code is large it's going to look ugly
        console.log("Saved Data : "+text); 
        // New functionality.
        console.log("Log data : "+text);
    }
    test.saveData("With Extended functionality");   
}
save1();

 

To fix the above issue, you need to slightly modify the code. You can achieve this by saving the reference of original method and call it by using call or apply method.

By Saving the reference of Original method:

function save2(){
    var test =  new TestSaveData();
    var orginalRef = test.saveData;
    
    test.saveData = function(text){
        // one liner code to use existing functionality.
         orginalRef.apply(this, arguments);
        // New functionality.
        console.log("Log data : "+text);
    }
    test.saveData("By Storing Original reference");   
}

save2();

 

Hope this article was useful and you have got a understanding of Monkey-Patching concept.

 Summary: Monkey-Patching is useful in scenarios where you are working with third party code and you want to extend/modify the behavior as per your requirements. 

But it is always recommended that you should avoid using Monkey-Patching in your code, because it is very hard to understand/predict where will the extra behavior will execute, because it's monkey-patched somewhere in your code or not. Also it is very hard to debug in case of fixing/finding defects, because of monkey-patching changing the actual behavior many places in your application.

 

References: 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#Using_apply_in_monkey-patching