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

Remove trailing named variables #2246

Merged
merged 4 commits into from
Oct 3, 2015

Conversation

rrosenblum
Copy link
Contributor

This turned into a larger commit than I originally thought it would be.

There are 3 modification to Style/TrailingUnderscorevariable.

  • Register an offense for trailing *_
    • this was never covered by the cop initially
  • Add a configuration to remove named trailing underscore variables.
    • I set this to be defaulted to true. Looking through RuboCop as an example, the vast majority of trailing named variables do not provide any useful information.
  • Do not register an offense for a trailing underscore variables that are preceeded by a splat variable
    • This has been a bug in the cop since it was first created.
    • a, *b, = foo() is a syntax error

@rrosenblum rrosenblum force-pushed the remove_trailing_named_variables branch from 31b6813 to cf71a68 Compare September 14, 2015 19:11
@rrosenblum
Copy link
Contributor Author

I forgot to add some of the performance notes about this PR. When running my normal memory profile on rubocop lib/rubocop/cop/performance lib/rubocop/cop/rails lib/rubocop/cop/lint, I saw a 1.5% memory drop going from 58893570 bytes to 58010356 bytes.

It is also faster to not assign underscore variables

ARRAY = *1..10

def with_trailing
  a, b, c, d, e, *_f = *ARRAY
  nil
end  

def without_trailing
  a, b, c, d, e, = *ARRAY
  nil
end  

Benchmark.ips do |x|
  x.report('with trailing') { with_trailing }
  x.report('without trailing') { without_trailing }
  x.compare!
end  
Calculating -------------------------------------
       with trailing    94.549k i/100ms
    without trailing   116.251k i/100ms
-------------------------------------------------
       with trailing      1.877M (± 5.5%) i/s -      9.360M
    without trailing      2.986M (± 4.5%) i/s -     14.996M

Comparison:
    without trailing:  2985706.0 i/s
       with trailing:  1876711.2 i/s - 1.59x slower

@rrosenblum rrosenblum force-pushed the remove_trailing_named_variables branch 5 times, most recently from 39a835d to fba0319 Compare September 18, 2015 17:21
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 21, 2015

Sorry that I'm responding to this PR just now. Was a bit short on time and wanted to push a bugfix only release before starting to merge new features, anyways.

My biggest doubt is what should be default now that the cop is configurable. I'd love to hear what other think about this.

@rrosenblum
Copy link
Contributor Author

It is fine that it took a while to comment. I realize that this is a rather large commit and goes against the original design of this cop. I debated as to whether the configuration should be defaulted to on, or defaulted to off and turned on for this project.

From time to time when I have to modify the code having the variables around already is helpful (saves looking into the structure of the node, as always forget it).

I agree with you that it is useful to know the structure of the node. A vast majority of the _ named variables in RuboCop are all named _args. I was not sure if _args was providing value to anyone or not.

@rrosenblum
Copy link
Contributor Author

I am fine with leaving the _ named variables for documentation purposes. What do you think about leaving the configuration option in and defaulting it to not remove named underscore variables?

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 22, 2015

What do you think about leaving the configuration option in and defaulting it to not remove named underscore variables?

That'd be great.

@rrosenblum rrosenblum force-pushed the remove_trailing_named_variables branch 2 times, most recently from 3fd9fa7 to c633cae Compare September 22, 2015 16:43
@rrosenblum
Copy link
Contributor Author

I changed the configuration name to AllowNamedUnderscoreVariables. It is defaulted it to true, the initial behavior. I also removed the commit that removes all of the trailing named underscore variables.

@rrosenblum rrosenblum force-pushed the remove_trailing_named_variables branch 4 times, most recently from 29da4a7 to b963907 Compare September 28, 2015 19:54
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 1, 2015

This has to be rebased.

@rrosenblum rrosenblum force-pushed the remove_trailing_named_variables branch from b963907 to a7706fa Compare October 1, 2015 13:16
@rrosenblum
Copy link
Contributor Author

The code has been rebased.

bbatsov added a commit that referenced this pull request Oct 3, 2015
@bbatsov bbatsov merged commit e1e5dfc into rubocop:master Oct 3, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 3, 2015

👍

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.

2 participants