KEMBAR78
♻️ Remove redundant return statements by pboling · Pull Request #266 · ruby/rexml · GitHub
Skip to content

Conversation

@pboling
Copy link
Contributor

@pboling pboling commented Jul 8, 2025

Split from #261; expanded and improved.

  • Very slight behavior change here in REXML::Valdiation::Event#matches? , which is to align the predicate method's return value with the expected behavior of a predicate method (which is to return one of true or false). See comments for more details.

else
super
end
super
Copy link
Contributor Author

@pboling pboling Jul 8, 2025

Choose a reason for hiding this comment

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

Former construction was odd, since it could be mistakenly read as calling super twice. Keeping super in the else statement prevents this misreading.

No behavior change.

else
[]
end
return []
Copy link
Contributor Author

@pboling pboling Jul 8, 2025

Choose a reason for hiding this comment

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

This allows a lack of clarity around potential fall through of unhandled cases. Using the explicit else makes the code more declarative.

No behavior change.

true
when :start_element
return true if event[1] == @event_arg
event[1] == @event_arg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a slight behavior change, in that this would previously return nil when the condition was not met, since it would exit the case statement without returning early, and exit the method without any explicit return, thus defaulting to void / nil return value.

This seems like it was unintentional, and thankfully, since this method appears to be intended as a predicate method, which should always return true or false, callers are likely using the boolean-ish result, where nil is falsey.

Returning actual false here then should be backwards compatible with the explicit intent of the ? predicate method.

true
when :start_attribute
return true if event[1] == @event_arg
event[1] == @event_arg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above comment.

Returning actual false here, instead of nil after falling out of the case statement, should be backwards compatible with the explicit intent of the ? predicate method.

@kou kou merged commit 66232ea into ruby:master Jul 9, 2025
67 checks passed
@kou
Copy link
Member

kou commented Jul 9, 2025

Thanks.

@pboling pboling deleted the redundant-return branch July 9, 2025 01:15
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