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
Fix Violations of and Reenable Style/RescueModifier
#57847
Conversation
[The official Ruby Style guide](https://rubystyle.guide/#no-rescue-modifiers) recommends avoiding the use of `rescue` in its modifier form. It does somewhat beg the question of why include a modifier form in the first place, but that's just by the by. Quoting from Rubocop documentation, this rule: > Checks for uses of `rescue` in its modifier form is added for following reasons: > > - The syntax of modifier form `rescue` can be misleading because it might lead us to > believe that `rescue` handles the given exception but it actually rescue all exceptions > to return the given rescue block. In this case, value returned by handle_error or > SomeException. > > - Modifier form`‘rescue` would rescue all the exceptions. It would silently skip all > exception or errors and handle the error. Example: If `NoMethodError` is raised, > modifier form rescue would handle the exception. Initial fix applied automatically with `bundle exec rubocop --autocorrect-all --only Style/RescueModifier`; I then manually ran a few more autocorrect commands to fix up some of the indentation and layout errors produced by the initial autocorrection. - https://rubystyle.guide/#no-rescue-modifiers - https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/RescueModifier - https://docs.rubocop.org/rubocop/cops_style.html#stylerescuemodifier
While I do like the original syntax because it allows you to write more concisely, the somewhat unexpected behavior of the syntax does make it a bit of a land mine if you are new to it and do not take the time to look up what it does (see inline comment). since the obscurity of the syntax makes it harder to read and somewhat error prone, I'm inclined to vote yes on this PR. however I am also interested to hear what others think :-) |
@@ -549,7 +549,9 @@ def parse_table_map_from_query_string(query_string) | |||
def parse_ids_from_query_string(query_string) | |||
[].tap do |ids| | |||
CGI.parse(query_string)['id[]'].each do |id| | |||
ids << Integer(id, 10) rescue ArgumentError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to say, this looks like a potential misuse of the "modifier" syntax -- it looks like the caller wanted to rescue this type of error, rather than insert this error class into the list of returned ids. in fact it looks like this error was introduced specifically when switching to the modifier syntax: d04d412
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, great catch! Definitely agree with that analysis; I'm tempted to manually update this fix to reflect that presumed intent rather than the current functionality.
To match what we believe the original intent was, rather than to preverse existing (and likely broken) original functionality. See the commit that introduced this behavior for more context: d04d412
The official Ruby Style guide recommends avoiding the use of
rescue
in its modifier form. It does somewhat beg the question of why include a modifier form in the first place, but that's just by the by.Quoting from Rubocop documentation, this rule:
Initial fix applied automatically with
bundle exec rubocop --autocorrect-all --only Style/RescueModifier
; I then manually ran a few more autocorrect commands to fix up some of the indentation and layout errors produced by the initial autocorrection.In general, I think
Style/
Cops are always optional, so I'm quite open to pushback on enabling this rule if folks want to continue to be able to userescue
in its modifier form. I lean towards being in favor of enabling it, myself; although I appreciate the aesthetics of inlining a rescue clause, I put a lot of weight on the official Ruby style guide's recommendations, and the reasons laid out in Rubocop's documentation seem compelling.What do y'all think?
Warning!!
The AP CSP Create Performance Task is in progress. The most critical dates are from April 3 - April 30, 2024. Please consider any risk introduced by this PR that could affect our students taking AP CSP. Code.org students taking AP CSP primarily use App Lab for their Create Task, however a small percent use Game Lab. Carefully consider whether your change has any risk of alterering, changing, or breaking anything in these two labs. Even small changes, such as a different button color, are considered significant during this time period. Reach out to the Student Learning team or Curriculum team for more details.
Links
Testing story
Relying on existing tests to verify that this does not result in any change to functionality.
PR Checklist: