Monkey Patching
ELI5 — The Vibe Check
Monkey patching is changing how existing code works at runtime — reaching into a library or framework and modifying its behavior without touching the source code. It's like sneaking into a restaurant kitchen and changing the recipe. Incredibly powerful, incredibly dangerous. When it works, you're a genius. When it breaks, nobody can figure out why the library is 'broken.'
Real Talk
Monkey patching is the dynamic modification of a module or class at runtime, typically to fix bugs, add features, or modify behavior without altering source code. Common in Ruby and Python, it's powerful but creates maintenance risks: patches can break with library updates, create hard-to-debug behavior, and conflict with other patches. TypeScript's module augmentation is a type-safe alternative.
Show Me The Code
# Python monkey patching
import requests
original_get = requests.get
def patched_get(*args, **kwargs):
print(f"Requesting: {args[0]}")
return original_get(*args, **kwargs)
requests.get = patched_get # Now all requests.get() calls are logged
When You'll Hear This
"We monkey-patched the ORM to add query logging." / "Don't monkey-patch in production — it's a ticking time bomb."
Related Terms
Anti-Pattern
Anti-Pattern is the opposite of a design pattern — it's a commonly used approach that looks like it solves a problem but actually makes things worse.
Decorator Pattern
You have a Coffee object. You want Coffee with Milk. Coffee with Sugar. Coffee with Milk and Sugar. With inheritance you'd need four classes.
Middleware
Middleware is like a security checkpoint at an airport.
Plugin Architecture
Plugin architecture is building your app so features can be added, removed, or swapped without changing the core.