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 for avoidable multiple assignment #1136

Closed
agrimm opened this issue Jun 4, 2014 · 5 comments · Fixed by #1831
Closed

Cop for avoidable multiple assignment #1136

agrimm opened this issue Jun 4, 2014 · 5 comments · Fixed by #1831
Assignees

Comments

@agrimm
Copy link
Contributor

agrimm commented Jun 4, 2014

The following code does not generate any offenses:

# Does fooing
class Foo
  def initialize(a, b, c, d, e)
    @a, @b, @c, @d, @e = a, b, c, d, e
  end
end

but I'd like RuboCop to be able to tell me to put each of those assignments on a separate line.

I realise that some people may prefer to have multiple assignments in a single line. I used to be one of those people. Nowadays, I'm of the philosophy that each line of code should do only one thing. It makes it easier for me to read, and makes the git diff of deleting a variable easier to read. But differences in coding style is what disabling a cop is for.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 4, 2014

I get your point. The only case in which multiple assignment should be allowed would be mismatching number of variables on the left side and the right side of the assignment, right?

@bbatsov bbatsov self-assigned this Jun 4, 2014
@agrimm
Copy link
Contributor Author

agrimm commented Jun 4, 2014

More or less. The only other scenario I can think of is

a = 1
b = 2
a, b = b, a

@mikegee
Copy link
Contributor

mikegee commented Jun 10, 2014

The only case in which multiple assignment should be allowed would be mismatching number of variables on the left side and the right side of the assignment, right?

That seems problematic. I wouldn't want the offense to go way if purposefully break the code like this:

@a, @b, @c, @d, @e = a, b, c, e

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 18, 2014

I'll think about this. Perhaps it only makes sense to do multiple assignment when destructuring a single argument (or you swap values as mentioned earlier).

@mikegee
Copy link
Contributor

mikegee commented Jun 18, 2014

I agree that destructuring one object into many, and swapping two values are valid use cases. I can't think of any others.

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 25, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 25, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 27, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 27, 2015
rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Apr 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants