Skip to content

Conversation

Earlopain
Copy link
Contributor

The current good section is quite a mouthful.
That is partially the fault of Ruby, but it can be better:

  • Less indentation
  • Fewer lines with code on them
  • More concise

This style is quite common:
https://github.com/search?q=lang%3Aruby%20%2Freturn%20.*%20if%20defined%2F&type=code

Tried searching for the other one but every search I try just times out.

As for instance_variable_defined? vs defined?, it's just shorter but does the same thing.

I would like to change rubocop-rails autocorrect for this, so opening here first. rubocop/rubocop-rails#1513

cc @r7kamura if you have opinions here.

The current good section is quite a mouthful.
That is partially the fault of Ruby, but it can be better:
* Less indentation
* Fewer lines of code
* More concise

This style is quite common:
https://github.com/search?q=lang%3Aruby%20%2Freturn%20.*%20if%20defined%2F&type=code

Tried searching for the other one but every search I try just times out.

As for `instance_variable_defined?` vs `defined?`, it's just shorter.
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

@koic
Copy link
Member

koic commented Aug 12, 2025

It seems that the early return with a guard clause makes the memoization intention clearer.

@koic koic merged commit b14b217 into rubocop:master Aug 12, 2025
3 checks passed
koic added a commit to rubocop/rubocop-rails that referenced this pull request Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants