Monkeypatching For Humans

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.

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

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.

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 ==

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!

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).

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…

One of the smartest monkeys I know is Reg Braithwaite (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) bears watching.

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.

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

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…

MORE php trolling ? This is getting old…

Other than that, interesting article :slight_smile:

Nevermind, I overreacted.

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

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

And remember, kids, ALWAYS MOUNT A SCRATCH MONKEY!

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

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

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

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 specific library and it is included in all Frameworks.

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!)