-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Extend safe navigation docs about long &.
chains
#947
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love how you put it.
Considering the law of Demeter, long chains are inherently bad. But this principle has downsides, and they are due to forcing to write wrappers (like presenters), or propagate increasing the interface of the top-level receiver class.
It really depends. Can it be that an address doesn’t have a zip? It’s business logic, can vary.
This change seems for the best. Can you try finding confirmation that the suggested style is widespread?
Is there a cop already to facilitate finding this ?
At least in my company we are striving to adhere to this. I will try to investigate other codebases.
I am currently implementing this cop - rubocop/rubocop#13171 |
1c17c18
to
4f178e0
Compare
Found only 1 offense in rails - https://github.com/rails/rails/blob/53ce1964f0bd6491fdc08aebc45f61275100f818/actionpack/lib/action_controller/metal/exceptions.rb#L43, so people definitely stick to it |
README.adoc
Outdated
[source,ruby] | ||
---- | ||
# bad | ||
user&.address&.zip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure if this example is appropriate. According to rubocop/rubocop#13228, up to two consecutive &.
are allowed by default. It would be better to explicitly clarify, with an explanation and example code, how many consecutive &.
are considered "Long safe navigation chain".
Rails code might not be representative, I believe they are strict in code quality. How about other repos from real-world-rails-apps and real-world-ruby in general? |
We already have above the following rule:
|
4f178e0
to
3f97718
Compare
Updated existing section for safe navigation. I am bad at writing docs - concrete suggestions welcome. |
&.
chains&.
chains
Probably we shouldn't mention a specific number here and just expand a bit the rationale - namely that the longer the chain is, the harder it becomes to track what on it could be returning a |
3f97718
to
b845209
Compare
Updated with a suggested rationale. |
Closes #946.