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

Fix Violations of and Reenable Style/RescueModifier #57847

Merged
merged 4 commits into from Apr 16, 2024

Conversation

Hamms
Copy link
Contributor

@Hamms Hamms commented Apr 5, 2024

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:

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.

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 use rescue 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:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

[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
@Hamms Hamms added code-hygiene ruby Pull requests that update Ruby code labels Apr 5, 2024
@Hamms Hamms marked this pull request as ready for review April 5, 2024 23:30
@Hamms Hamms requested review from a team as code owners April 5, 2024 23:30
@Hamms Hamms requested a review from a team April 5, 2024 23:30
@davidsbailey
Copy link
Member

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
Copy link
Member

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

Copy link
Contributor Author

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
@Hamms Hamms merged commit bcf790b into staging Apr 16, 2024
2 checks passed
@Hamms Hamms deleted the fix-Style/RescueModifier branch April 16, 2024 00:21
Hamms added a commit that referenced this pull request Apr 16, 2024
Hamms added a commit that referenced this pull request Apr 16, 2024
Hamms added a commit that referenced this pull request Apr 16, 2024
Hamms added a commit that referenced this pull request Apr 17, 2024
…"" (#58030)

* Revert "Revert "Fix Violations of and Reenable `Style/RescueModifier` (#57847…"

This reverts commit 6b13227.

* manually fix several Style/RescueModifier violations in haml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-hygiene ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants