-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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: numbered variable names (user_1 vs user1) #3167
Comments
Would it also be useful to have a configuration option to disallow numbered variables? I can see how some teams might want this to encourage better design (i.e. using a data structure like |
I can't say that it would be something that we would use. We do a lot of testing around things like one-off scripts and database scopes to ensure that only specific records are returned or modified. So something like: user_1 = create(:user, ...)
user_2 = create(:user, ...)
expect do
SomeThing.call
end.to change { user_1.reload.some_attribute }
.and not_change { user_2.reload.some_attribute } |
I agree with both of you. I think this new cop would have some value and disallowing numbered variables might be the best default config. |
There is, of course, a small chance for false positives. Consider, for example, a method call |
What if the cop specifically looked for more than one local variable with incrementing suffix numbers? Perhaps starting at 0 or 1? I think that would catch the problem and not miss any violations. |
It almost sounds like there are multiple rules being discussed here:
|
@mockdeep: These would almost certainly go in the same cop (with configuration options), which is why we're discussing them all in the same thread. 😀 Checking adjacent variables is a bit more involved, but might be possible if the rules are well defined. |
We've got a lot of places in our code where we use a couple of records, something like:
Sometimes people number them instead,
user1
anduser2
. Would be nice to have a rule to enforce consistent numbering of variable names, so always with_
or always without.The text was updated successfully, but these errors were encountered: