kiwiheretic — 2014-04-08T18:40:04-04:00 — #1
I like alot of the features of Ruby. Some I'm not so crazy about and its usually areas where they tried to copy Perl. One thing I've never been crazy about is monkey patching. It reminds me of the early days when self modifying code was frowned upon. Now it seems as though its being resurrected as the now acceptable monkey patching.
I took the following from the Discourse mailing list. (Full post here: https://meta.discourse.org/t/proposal-for-plugin-architecture-for-discourse/14501 )
Is it now expected that if you are using Ruby that at some point you will learn and embrace monkey patching? Is that now a ruby culture imperative? I've always found it to produced hard-to-maintain code.
jeff_mott — 2014-04-08T19:12:01-04:00 — #2
Monkey patching is both very handy and very dangerous. It's handy because, "We can let everyone ... change every piece of the code." And it's dangerous because, "We can let everyone ... change every piece of the code."
All the effort we put in with things like scopes and namespaces and DI to make sure our code plays nicely within its own little sandbox, and monkey patching just brazenly reaches into the global environment and changes things willy nilly.
kiwiheretic — 2014-04-08T22:04:23-04:00 — #3
The whole thread about let's do the Discourse plugin architecture as a monkey patch rather than as an official plugin API amazes me. Even more amazing is that it seems to have the support of the core development team. It reminds me of phpBB plugin architecture with its self modifying code.
pullo — 2014-04-09T02:59:50-04:00 — #4
Monkey patching in Ruby is not mandatory, but in my opinion is more prevalent than it should be.
However, as it is so easy to open up a class and change it at run-time, lots of people started using it as a viable way to add add random methods to the core classes.
This is bad in my opinion and its proliferation provoked some degree of backlash: Monkeypatching is Destroying Ruby.
Nonetheless, opinion remained divided and Ruby 2.0 even shipped with refinements, an experimental feature that aims to change the way monkey patching is performed. Refinements allow developers to change built-in methods without affecting other code that uses them.
You might also find this interesting: http://blog.codinghorror.com/monkeypatching-for-humans/
markbrown4 — 2014-04-09T10:32:12-04:00 — #5
You, as the author of your programs can write however you wish, you don't have to use every feature if you don't agree with it. That being said I've found learning Ruby has made me a much better programmer on the whole.
kiwiheretic — 2014-04-09T17:31:03-04:00 — #6
I can see people have different opinions about this, some say its useful in some cases, some say its something thats useful whenever we can fit it in to our project.
I guess for me is I am questioning whether it should be the entire plugin architecture. I understand that monkey patching offers maximum versatility but the same argument could be made for phpBB's source code modifying plugin architecture.
Yes, I understand that you can use Ruby without monkey patching but what I am wondering if the chief reason developers reach for ruby is there love for monkey patching.
jeff_mott — 2014-04-09T19:13:46-04:00 — #7
Monkey patching is useful like global variables are useful.
Code can be modified from anywhere... check.
Behavior in one part of a program varies wildly based on hard to identify operations in another part of the program... check.
Any part of the program may depend on it... check.
Two or more modules may cause a collision (that is, they may modify the same code in mutually incompatible ways)... check.
jeff_mott — 2014-04-09T19:35:11-04:00 — #8
To reinforce my last point, this article has a decent list of why global variables are bad. Such as...
Non-locality -- Source code is easiest to understand when the scope of its individual elements are limited.
No Access Control or Constraint Checking -- A global variable can be get or set by any part of the program.
Testing and Confinement - source that utilizes globals is somewhat more difficult to test.
Monkey patching also suffers from all of these.