# Monkeypatching For Humans

**URL:** <https://discourse.codinghorror.com/t/monkeypatching-for-humans/291>\
**Category:** blog\
**Created:** [July 12, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291 "2008-07-12T00:00:00Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![TomC4](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/t/848f3c/32.png) [@TomC4](https://discourse.codinghorror.com/u/TomC4)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/41 "2008-07-13T00:00:00Z")

</div>

It’s not just the addition only nature of extension methods that makes them safe, it’s the fact that they are simply a regular static method once compiled. You can certainly make your code very confusing by overusing them (which I have done) but you won’t cause problems for anyone else.

Sometimes being able to modify a standard method easily would be helpful, but with all the trouble that can cause it’s probably best that the solution is something that is obviously an ugly hack. Some things shouldn’t be easy.

---

<div class="post-metadata">

**Author:** ![buggyfunbunny](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/b/b9bd4f/32.png) [@buggyfunbunny](https://discourse.codinghorror.com/u/buggyfunbunny)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/42 "2008-07-13T00:00:00Z")

</div>

For those with short memories; what M$ tried to do to java (and caused Sun to sue) amounted to monkeypatching. In the java/M$ case, it was done to destroy Sun’s control of java. Caveat Emptor

---

<div class="post-metadata">

**Author:** ![sweetperception](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/96bed5/32.png) [@sweetperception](https://discourse.codinghorror.com/u/sweetperception)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/43 "2008-07-13T00:00:00Z")

</div>

most monkeypatching done is made out of sheer demonstration that they know metaprogramming (especially for ruby). i find it less smart to instantly resolve to monkeypatching the language itself rather than extending it within the application or its library.

most importantly, monkeypatching without documenting it is pure evil. it makes it harder to debug the application imho.

---

<div class="post-metadata">

**Author:** ![kevin4](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/k/a3d4f5/32.png) [@kevin4](https://discourse.codinghorror.com/u/kevin4)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/44 "2008-07-13T00:00:00Z")

</div>

you can check for a null string and an empty string with String.IsNullOrEmpty. Saw this in some code somewhere and wondered how much time i had wasted typing s == null || s ==

---

<div class="post-metadata">

**Author:** ![MaS1](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/848f3c/32.png) [@MaS1](https://discourse.codinghorror.com/u/MaS1)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/45 "2008-07-13T00:00:00Z")

</div>

Well, that’s exactly how it is in other, more dynamic languages such as Javascript, Python, Perl, and Ruby.

Unfortunetely, Python doesn’t allow you to extend built-in classes. Such a shame!

---

<div class="post-metadata">

**Author:** ![Toothy](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/t/f17d59/32.png) [@Toothy](https://discourse.codinghorror.com/u/Toothy)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/46 "2008-07-13T00:00:00Z")

</div>

You really like to comment on things you don’t fully understand … now don’t you? The quality of the blog is going downhill.

Check this out (python)

a = Hello  
a. **len** = lambda x: x  
Traceback (most recent call last):  
File stdin, line 1, in module  
AttributeError: ‘str’ object attribute ‘ **len** ’ is read-only

I guess it is not that simple to change the behavior of a builtin type. By the way this is not monkeypatching (even if it worked).

---

<div class="post-metadata">

**Author:** ![NicolsM](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/n/7ab992/32.png) [@NicolsM](https://discourse.codinghorror.com/u/NicolsM)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/47 "2008-07-13T00:00:00Z")

</div>

The fact that the langague gives you the tools to do this monkeypatching thingy doesn’t mean that you should do it.

It’s just there… as lots of other stuff.

There should be some kind of specification where you allow or disallow things to be done…

---

<div class="post-metadata">

**Author:** ![Avdi](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/a/c6cbf5/32.png) [@Avdi](https://discourse.codinghorror.com/u/Avdi)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/48 "2008-07-13T00:00:00Z")

</div>

One of the smartest monkeys I know is Reg Braithwaite ([http://weblog.raganwald.com/).](http://weblog.raganwald.com/).) Rather than argue one side or the other, Reg is working on making it possible to have your cake and eat it too, through localized language alterations which don’t contaminate the entire codebase. His Ruby-rewriting project (see e.g. [http://weblog.raganwald.com/2008/07/separating-concerns-of-what-to-do-from.html)](http://weblog.raganwald.com/2008/07/separating-concerns-of-what-to-do-from.html)) bears watching.

---

<div class="post-metadata">

**Author:** ![Davidf4](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/d/85f322/32.png) [@Davidf4](https://discourse.codinghorror.com/u/Davidf4)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/49 "2008-07-13T00:00:00Z")

</div>

Good post. To me, Monkey Patching seems like, if you’re having to do it, there’s some other issue which you’re having to work around which needs attention. Say there’s a function in the language which is actually broken: maybe a Monkey Patch would work but it would also warrant investigating if there’s a bug fix on the way and putting a reminder to remove the patch when it comes through. The problem I’ve always found is, though it seems easy to remove any sort of patching like this now, given time, anything becomes harder to roll back.

---

<div class="post-metadata">

**Author:** ![DanielJ](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/d/e56c9b/32.png) [@DanielJ](https://discourse.codinghorror.com/u/DanielJ)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/50 "2008-07-13T00:00:00Z")

</div>

Since you’re extending the methods of a class to make it more usable, may I suggest choosing more usable method names?

Maybe it’s just because I’ve been spared this convention, but Left means nothing to me. I had to read your code just to figure out what it was expected to do.

In the Objective C Cocoa framework, we have a couple methods to accomplish what your Left and Right would. They’re called substringFromIndex and substringToIndex. Judging from your use of a built-in Substring() … it sounds like these better-named enhancements would also be at home in your C# project.

Daniel

---

<div class="post-metadata">

**Author:** ![steve24](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/a5b964/32.png) [@steve24](https://discourse.codinghorror.com/u/steve24)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/51 "2008-07-13T00:00:00Z")

</div>

If you are the only person in the world, then many this is an OK idea.

The other exception would be to build a centralized library of monkey code, which had been laboriously commented.

Other than that, it’s just asking for trouble…

---

<div class="post-metadata">

**Author:** ![manu2](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/dbc845/32.png) [@manu2](https://discourse.codinghorror.com/u/manu2)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/52 "2008-07-13T00:00:00Z")

</div>

MORE php trolling ? This is getting old…

Other than that, interesting article :)

---

<div class="post-metadata">

**Author:** ![manu3](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/76d3ee/32.png) [@manu3](https://discourse.codinghorror.com/u/manu3)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/53 "2008-07-13T00:00:00Z")

</div>

Nevermind, I overreacted.

---

<div class="post-metadata">

**Author:** ![Paul\_Betts](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/p/bb73d2/32.png) [@Paul\_Betts](https://discourse.codinghorror.com/u/Paul_Betts)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/54 "2008-07-13T00:00:00Z")

</div>

Here’s something that will blow your mind if you think monkeypatching classes is weird enough - in Ruby, I can monkey patch _individual instances_ of a class:

class Foo  
def bar  
puts Baz  
end  
end

irb(main):012:0 a = Foo.new; b = Foo.new  
= #Foo:0x5a18ec

irb(main):013:0 a.bar  
Baz  
= nil

irb(main):014:0 def a.bar  
irb(main):015:1 puts Bamf  
irb(main):016:1 end  
= nil

irb(main):017:0 a.bar  
Bamf  
= nil

irb(main):018:0 b.bar  
Baz  
= nil

---

<div class="post-metadata">

**Author:** ![lmbo](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/l/b3f665/32.png) [@lmbo](https://discourse.codinghorror.com/u/lmbo)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/55 "2008-07-13T00:00:00Z")

</div>

Yeah, there is nothing like good old write-only code.

---

<div class="post-metadata">

**Author:** ![DaveA](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/d/8e8cbc/32.png) [@DaveA](https://discourse.codinghorror.com/u/DaveA)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/56 "2008-07-13T00:00:00Z")

</div>

And remember, kids, ALWAYS MOUNT A SCRATCH MONKEY!

(If you’re too young to CTR, Google is your friend.)

---

<div class="post-metadata">

**Author:** ![Steve](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/f19dbf/32.png) [@Steve](https://discourse.codinghorror.com/u/Steve)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/57 "2008-07-13T00:00:00Z")

</div>

There are certainly pros and cons to monkeypatching, but you outlined them perfectly. Another great post!

---

<div class="post-metadata">

**Author:** ![pallu](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/p/a88e4f/32.png) [@pallu](https://discourse.codinghorror.com/u/pallu)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/58 "2008-07-13T00:00:00Z")

</div>

What about Java language? There’s the override which makes it possible to do monkey patch as well, correct?

---

<div class="post-metadata">

**Author:** ![Jim](https://discourse.codinghorror.com/user_avatar/discourse.codinghorror.com/jim/32/75530_2.png) [@Jim](https://discourse.codinghorror.com/u/Jim)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/59 "2008-07-13T00:00:00Z")

</div>

Why not just add a reference to Microsoft.VisualBasic (which is included in the .NET Framework) and then use Left() function?

You can use Left, Right, Mid, etc. in C# by just adding a reference to that library that is already included in the Framework. It’s a not [VB.NET](http://VB.NET) specific library and it is included in all Frameworks.

---

<div class="post-metadata">

**Author:** ![Syd](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/3d9bf3/32.png) [@Syd](https://discourse.codinghorror.com/u/Syd)\
**Post date:** [July 13, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291/60 "2008-07-13T00:00:00Z")

</div>

The real problem with Left() (ie. the lack thereof) is that the substring function COMPLETELY UNNECESSARILY throws an exception if you pass a second argument longer than the string itself. WHY? (I guess the answer is arrogance amongst the original language devs, who came from a C background, and therefore did the right thing rather than the useful thing, when VB would always have done the later!)

[Previous page](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291.md?page=2)

[Next page](https://discourse.codinghorror.com/t/monkeypatching-for-humans/291.md?page=4)
