Skip to content
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

Cop idea: avoid numbered variables unless needed #12392

Open
vlad-pisanov opened this issue Nov 15, 2023 · 2 comments
Open

Cop idea: avoid numbered variables unless needed #12392

vlad-pisanov opened this issue Nov 15, 2023 · 2 comments

Comments

@vlad-pisanov
Copy link

A cop idea for the Naming group: prefer var over var_1 unless there's also var_2 (etc.) in the same scope. Makes the code more readable:

# bad
def foo
   user_1 = create(:user)
   user_1.destroy
end

# good
def foo
   user = create(:user)
   user.destroy
end

# good
def foo
   user_1 = create(:user)
   user_2 = create(:user)
   user_1.destroy
   user_2.destroy
end
@koic
Copy link
Member

koic commented Nov 15, 2023

It's an interesting idea, but it cannot prevent false positives in cases with meaningful numbers. For example, in Naming/VariableNumber, certain method names are allowed by default, requiring a similar list of exceptions. However, managing such a list might be a hassle for users, leading to doubts about its value as a default cop.

@vlad-pisanov
Copy link
Author

Maybe this could be a non-default option of Naming/VariableNumber so it can piggyback off the exception list, like:

Naming/VariableNumber:
   Enabled: true
   OnlyWhenNeeded: true

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

No branches or pull requests

2 participants