ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Extend safe navigation docs about long `&.` chains

Open fatkodima opened this issue 1 year ago • 7 comments

Closes #946.

fatkodima avatar Sep 11 '24 16:09 fatkodima

Can you try finding confirmation that the suggested style is widespread?

At least in my company we are striving to adhere to this. I will try to investigate other codebases.

Is there a cop already to facilitate finding this ?

I am currently implementing this cop - https://github.com/rubocop/rubocop/issues/13171

fatkodima avatar Sep 11 '24 17:09 fatkodima

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

fatkodima avatar Sep 11 '24 18:09 fatkodima

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?

pirj avatar Sep 12 '24 06:09 pirj

We already have above the following rule:

Avoid chaining of &.. Replace with . and an explicit check. Perhaps it just needs to be expanded and clarified a bit instead of adding a new guideline?

bbatsov avatar Sep 12 '24 06:09 bbatsov

Perhaps it just needs to be expanded and clarified a bit instead of adding a new guideline?

Updated existing section for safe navigation. I am bad at writing docs - concrete suggestions welcome.

fatkodima avatar Sep 12 '24 15:09 fatkodima

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 nil. That's always been my fundamental problem with any safe navigation chains.

bbatsov avatar Oct 20 '24 17:10 bbatsov

Updated with a suggested rationale.

fatkodima avatar Oct 20 '24 18:10 fatkodima