What is Monkey Patching?

himankbhalla

himank

Posted on February 20, 2023

What is Monkey Patching?

Monkey patching is a programming technique used in software development that allows developers to change or extend the behaviour of existing code without directly modifying the source code. This can be particularly useful when the code is closed-source or the developer doesn’t have access to the original code. In this post, we’ll explore the concept of monkey patching and provide several examples to illustrate how it works.

What is Monkey Patching?

Monkey patching is a technique that allows developers to change the behaviour of an existing piece of code at runtime. This is done by modifying or adding attributes or methods to an object or class, which can then be called as if they were part of the original code. The term “monkey patching” comes from the idea of a monkey modifying or “patching” a codebase, much like a monkey might tinker with a tool or piece of machinery.

Monkey patching is often used to fix bugs or add new features to third-party libraries, frameworks, or applications. In some cases, it can be used to add functionality to the language itself. However, monkey patching can be a double-edged sword. While it can be a powerful tool in the hands of experienced developers, it can also lead to unexpected behaviour and hard-to-find bugs.

Examples of Monkey Patching:

Here are a few examples that illustrate how monkey patching can be used in practice:

Modifying an Existing Function

Let’s say we’re working with a third-party library that contains a function that doesn’t quite do what we need it to do. Instead of modifying the library’s source code, you can use monkey patching to modify the function’s behaviour at runtime. Here’s an example in Python:

import third_party_library

def new_function(arg1, arg2):
    # new implementation
    return result

third_party_library.existing_function = new_function
Enter fullscreen mode Exit fullscreen mode

In this example, we’re importing a third-party library and then defining a new function that we want to use instead of the existing function provided by the library. We then use monkey patching to replace the existing function with our new function.

Adding a New Method to a Class

Another common use case for monkey patching is adding a new method to an existing class. Here’s an example in JavaScript:

class OriginalClass {
  originalMethod() {
    console.log('Original Method');
  }
}

OriginalClass.prototype.newMethod = function() {
  console.log('New Method');
};

let obj = new OriginalClass();
obj.newMethod(); 
Enter fullscreen mode Exit fullscreen mode

In this example, we’re creating a new method called “newMethod” and adding it to the prototype of the “OriginalClass” class. We can then create an instance of the “OriginalClass” and call the new method as if it were part of the original class.

Fixing a Bug in a Third-Party Library

Monkey patching can also be used to fix bugs in third-party libraries that you can’t modify directly. Here’s an example in Ruby:

require 'third_party_library'

# Define a new method to fix the bug
class ThirdPartyLibrary::OriginalClass
  def fixed_method
    # new implementation
    return result
  end
end
Enter fullscreen mode Exit fullscreen mode

In this example, we’re requiring a third-party library and then defining a new method called “fixed_method” that fixes a bug in the original library. We’re using the “::” operator to access the original class in the library and then adding our new method.

Conclusion:

Monkey patching is a powerful technique that can be used to modify or extend the behaviour of existing code at runtime. While it can be a useful tool for fixing bugs or adding new features to third-party libraries, it can also introduce unexpected behaviour, hard to find bugs.
Originally posted on medium.

💖 💪 🙅 🚩
himankbhalla
himank

Posted on February 20, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What is Monkey Patching?
monkeypatching What is Monkey Patching?

February 20, 2023