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

Avoid noise when code runs with Ruby warnings #1526

Merged
merged 1 commit into from
Dec 1, 2016

Conversation

cpetschnig
Copy link
Contributor

@cpetschnig cpetschnig commented Nov 25, 2016

New versions of RSpec have warnings turned on by default:

# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true

I believe these warnings are helpful in general to write better code. However, Grape and Grape Entity are very noisy. I get about 20 lines of warnings for one single test where Grape is involved.

I hope you agree. Many of my changes could also be solved differently for sure. I am happy to discuss and change those cases. I also changed the specs for less warnings and turned them on in the test suite.

@cpetschnig cpetschnig force-pushed the avoid_ruby_warnings branch 2 times, most recently from 4e234cc to 746cd59 Compare November 25, 2016 12:04
@dblock
Copy link
Member

dblock commented Nov 25, 2016

I think initializers such as @status ||= nil should be @status = nil in def initialize. Otherwise this looks sensible.

@cpetschnig
Copy link
Contributor Author

I have done so in all the classes. The cases where I didn't are all modules in the DSL namespace.

@dblock
Copy link
Member

dblock commented Nov 28, 2016

Because those are included? I am not a huge fan of side-effects of assigning something on return to nil, warning or convention or not. Anyone else has an opinion, maybe @namusyaka or @LeFnord?

@cpetschnig
Copy link
Contributor Author

I agree that @body ||= nil is not a very good pattern, rather hard to read/understand. I removed the two occurrences from the PR.

@dm1try
Copy link
Member

dm1try commented Nov 29, 2016

Sorry to bother you guys, I'm personally 👎 for this.
I prefer to move "instance variable @var not initialized" warning to the other verbosity level than add more code to deal with it :)
bundle exec ruby -Ispec -W2 spec/grape/validations/validators/default_spec.rb - I'll happy to see it only here.

There are some good points in the sequel repo that can help with the decision.

@cpetschnig
Copy link
Contributor Author

cpetschnig commented Nov 29, 2016

The discussion in the sequel repo is very interesting, also the references to the Ruby development. The warning gem in Ruby 2.4 will definitely help. The sequel gem should be a good reference.

I see that some of my changes may affect the performance of every API call, while others are only executed when the app is started. What do you think of changing only the occurrences in start-up code?

@dblock
Copy link
Member

dblock commented Nov 30, 2016

I would be down with what you're proposing @cpetschnig. Initialization in def initialize is ok with me, but side effects in properties aren't.

@cpetschnig
Copy link
Contributor Author

I updated the PR:
Instance variables are now only initialized in def initialize. Also removed the config.warning = true from spec_helper.rb.

@@ -5,6 +5,11 @@ module Middleware
class Formatter < Base
CHUNKED = 'chunked'.freeze

def initialize(*_)
super
@app_response = nil
Copy link
Member

@dm1try dm1try Nov 30, 2016

Choose a reason for hiding this comment

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

maybe Base class is a better place for this

New versions of RSpec have warnings turned on by default:

    # This setting enables warnings. It's recommended, but in some cases may
    # be too noisy due to issues in dependencies.
    config.warnings = true

I believe these warnings are helpful in general to write better code. However, Grape and Grape Entity are very noisy. I get about 20 lines of warnings for one single test where Grape is involved.

I hope you agree. Many of my changes could also be solved differently for sure. I am happy to discuss and change those cases. I also changed the specs for less warnings and turned them on in the test suite.
@cpetschnig
Copy link
Contributor Author

Very true, I've changed it.

@dm1try dm1try merged commit dc5e4b0 into ruby-grape:master Dec 1, 2016
@dm1try
Copy link
Member

dm1try commented Dec 1, 2016

It's very good, thanks @cpetschnig!

@LeFnord
Copy link
Member

LeFnord commented Dec 1, 2016

🎉

@dblock
Copy link
Member

dblock commented Dec 4, 2016

Perfect, thanks!

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.

4 participants