News for hackers. Stuff that matters.
Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @09:43AM (#28327710)
What's wrong with 2.weeks.from_now ? I get that it hurts your sensibilities (SOLID etc), truth is it's fine. I've seen a good share of Rails projects and never had a problem with this yet. We tend to have a knee-jerk reaction to certain things, and sometimes for good reason, but ActiveSupport is used in millions of Rails projects and nothing horrible happens.
Re:Responsible Monkeypatching in Ruby by Fire-Dragon-DoL on Friday August 27, 2021 @10:09AM (#28327983)
There are things that go wrong when using ActiveSupport, but it's so ingrained in people's workflow that they won't notice.

The one I consider the most annoying: it substantially increases load time. ActiveSupport is huge and just requiring it increases the load time by 1.x seconds. Given what it provides, that's substantial.

The inability to extract a library (or a service) without depending on ActiveSupport is a cost that doesn't exist with the other approaches.

Not to underestimate, your _number_ now responds to `weeks`, and duck typing is the only way you have "interfaces" in Ruby. I can easily picture having a "weeks" or days attribute on some entity and a service object expecting that, now there are subtle bugs when passing accidentally the entity's _value_ instead of the entity itself.

Yeah, they are "minor" things, but the point is dying of thousands cuts.

The last damage is more subtle: devs, even less experienced devs, will feel authorized to monkey patch. In the private codebases I saw (multiple) this is common. It's not a last resort anymore and it ends up becoming a hard problem to debug and resolve.

The ability to monkeypatche pushes devs to NOT design their objects in an extensible fashion. This effectively is the highest damaging consequence, but it's incredibly hard to assess.

Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @10:26AM (#28328209)
> The inability to extract a library (or a service) without depending on ActiveSupport is a cost that doesn't exist with the other approaches.

I'm not sure what you mean; Rails depends on ActiveSupport so if you're a Rails codebase you don't really care, you already depend on it. If you're a gem author it's your own decision if you want to depend on ActiveSupport or not, I'm sure many gems don't depend on it, and others do.

> Not to underestimate, your _number_ now responds to `weeks`, and duck typing is the only way you have "interfaces" in Ruby. I can easily picture having a "weeks" or days attribute on some entity and a service object expecting that, now there are subtle bugs when passing accidentally the entity's _value_ instead of the entity itself.

Never seen this. Not saying it can't happen (can u give an example though? not 100% sure what you mean) but I've never seen it.

> Yeah, they are "minor" things, but the point is dying of thousands cuts.

This is a bit of a hyperbole statement wouldn't you agree?

> The last damage is more subtle: devs, even less experienced devs, will feel authorized to monkey patch.

I think it happens way less than you think; usually in teams of inexperienced people who discover Ruby and metaprogramming for the first time. But the worst monkeypatching bug I've ever seen was actually done in javascript; a guy monkeypatched some jquery method and broke 20000 websites at once (we were a widget startup). Monkeypatching isn't a Ruby thing it's a dynamic languages thing. I don't see Ruby/Rails culture just encouraging people to monkey patch String or Object. The fact dhh and a very experienced team of devs did it, and tested it and perfected it for years, doesn't mean you should do it. This is pretty common knowledge now and I think 99% of Rubyists would agree.

Re:Responsible Monkeypatching in Ruby by Fire-Dragon-DoL on Friday August 27, 2021 @10:45AM (#28328482)
I wish it was true, still, when I look at authentication done with devise, there is extensive monkeypatching done (probably caused by poor design in Devise itself, but still).

> This is a bit of a hyperbole statement wouldn't you agree?

I'm not sure, I see many small problems that slowly leads to what the Ruby community is now. That being said, if you look at how many things ActiveSupport monkeypatches, "thousands cuts" it's not an understatement: https://github.com/rails/rails/tree/main/activesupport/lib/a...

Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @12:33PM (#28329822)
Here is a simple example. Ruby's start_with? method which is confusing because its bad grammar. So AS added an alias and called it starts_with? https://github.com/rails/rails/blob/main/activesupport/lib/a...

Why is this bad? Makes total sense to me. Now both forms work and hopefully it eventually makes it into the language like often happens. But until it does, why not monkey patch obvious mistakes?

Re:Responsible Monkeypatching in Ruby by Fire-Dragon-DoL on Friday August 27, 2021 @01:02PM (#28330269)
Because Ruby has been consistent: there is no method starting with S.

Array has `include?`, Regexp has `match?` and String has `match?` and `start_with?`.

This is consistent: there is no S.

Now when I'm in a codebase, depeneding on ActiveSupport and which parts of it are included, the consistency is now missing. Is it with S or without S?

And what methods DO have the S version and which one do not?

Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @01:27PM (#28330592)
It's a fair point about consistency but its still bad naming. AS accepts both so I don't see a problem. I do hope Ruby will eventually deprecate and rename it to includes? , starts_with? etc.
Re:Responsible Monkeypatching in Ruby by eloisius on Friday August 27, 2021 @11:30AM (#28329098)
I don’t think you’re helping your case by holding out Devise as a problem example. Devise is extremely popular, works very well, can easily be extended to support different authentication modes like OAuth, and has saved countless hours for many people. In fact, I haven’t worked in the Ruby world for several years now, but devise is one piece of software that I frequently miss when using other languages.
Re:Responsible Monkeypatching in Ruby by Fire-Dragon-DoL on Friday August 27, 2021 @11:54AM (#28329387)
Devise is well known to have many problems. It works out of the box, but when deviating from strictly the use case it was designed for, there is no documentation and the shortcoming of the design become apparent.

That's where the monkey patching comes in, usually.

But to be clear, I'm stating the facts that many software developer see in the Ruby world when you join an organization that's growing tremendously. As usual, if you fit perfectly in the Rails target and the Devise target, you won't encounter problems.

Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @12:37PM (#28329874)
I don't see it sorry. I don't think Shopify crashes every monday because thousands of devs monkeypatch Object all the time or because AS adds 20 miliseconds to load time. Neither does Github or countless othet huge companies. I agree that after a certain size types make a lot of sense and Ruby or other dynamic languages becomes less attractive but ActiveSupport? Works well.
Re:Responsible Monkeypatching in Ruby by Fire-Dragon-DoL on Friday August 27, 2021 @12:58PM (#28330208)
Shopify has very strict rules to prevent that and they have been working to address that problem for YEARS. They have actually worked on Packwerk (which they use) which enforces the usage of constants when crossing boundaries, rather than any form of interface or duck typing.

The load time of a single test is a recurring problem in the Rails world though, so ActiveSupport adds up.

Monekypatching doesn't lead to crash (it can, but that's not the point), it leads to: - Less extendable code - Less learning - Less maintainable code

Even if the damage is small, given how competitive is the world, why taking the chance for no benefit?

Re:Responsible Monkeypatching in Ruby by joelbluminator on Friday August 27, 2021 @01:29PM (#28330618)
> Even if the damage is small, given how competitive is the world, why taking the chance for no benefit?

Well that's the crux of it here, you see it as damage/no benefit while others in the Rails world disagree. It's a difference of opinions that won't be resolved tonight by arguing on Hacker News. I did like your comment about naming consistency in Ruby though, learned something new.