-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add .descendants and .subclasses advice
#373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4c3d17f to
5ca37fe
Compare
|
Let me play the devil's advocate here. If this method is so unreliable, why not propose to remove it from Rails altogether? Related discussion rspec/rspec-mocks#1568 Why would descendants be GC'd ? Can this happen in runtime? Or just in tests/specs? Class has subclasses, descendants is a Railsism, right? |
Well, we have a lot of methods and classes that we shouldn't use in most cases of "regular" application development but then there's a rare occasion where there's no other choice. I'd also never suggest anyone iterating over If somebody is fully aware what they are doing and they still want to do it, they can still disable the warning.
Indeed! Initially I had the Cop suggesting For the detail questions, Ben is surely the one who can answer the details better than I can and I saw that you already mentioned him in the other issue. |
5ca37fe to
d3ec14f
Compare
descendants advice.descendants and .subclasses advice
|
There are many things one should probably not use, and that includes There are absolutely valid usecases for these (if not, they would probably not exist). You just have to be aware of why using them might not be such a good idea. BTW, your added example will always work as expected, there are no autoloads/GC present. |
A guide does not enforce anything anyway, no? 🤔 But I agree that Rails's documentation should be improved as well in this regards.
That's what Rubocop's cops are meant to, no? To raise awareness?
I agree. That's a very valid point. |
I guess it's not directly related to this PR, yeah.
Thing is, there is not much you can do better. You either use these methods, or maybe do it in a different way that exhibits the same problem. If you have to know subclasses/descendants then there is really no 100% reliable way to do so. The problem is that there is no advice to give on what to do except not to touch it.
Ruby docs have an example https://docs.ruby-lang.org/en/master/Class.html#method-i-subclasses |
There are so many cops that only a few get configured, the most offending ones get silenced, and it happens that only touched code gets linted. Whether the code is "corrected" when the project updates their RuboCop dependency and new cops are introduced, or not - depends on the project. With this in mind, I doubt that such a cop, or a guideline will be helpful. @Earlopain has a good point that the method's documentation needs to be improved. |
|
Yeah, it doesn't seem like a rule that should be stated in the Rails Style Guide. Please also refer to @rafaelfranca's comment posted in the RuboCop Rails. Thank you. |
A wise man, let's call him Ben, once said:
And I totally trust him on that because he's making a… GoodJob 🥁