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

Refactor the interesting_ancestors_of method in the Mixins listener #967

Merged
merged 1 commit into from
Jun 6, 2022

Conversation

egiurleo
Copy link
Contributor

@egiurleo egiurleo commented Jun 3, 2022

Motivation

Use Set#compare_by_identity to make the logic of the interesting_ancestors_of method easier to understand.

Implementation

The old implementation worked as follows:

  • Create inherited_ancestors as a new Set, and add all the object_ids of the modules in inherited_ancestors_of(constant)
  • Iterate through the modules in constant.ancestors and reject any whose object_id is already in inherited_ancestors_ids

The new implementation is much more straightforward:

  • Create inherited_ancestors as a new Set with compare_by_identity
  • Add the results of inherited_ancestors_of(constant) to this set
  • Create ancestors as a new Set with compare_by_identity
  • Add the results of ancestors_of(constant) to this set
  • Subtract inherited_ancestors from ancestors and return the result as an array

Tests

Behavior should not change. Tests are still green.

@egiurleo egiurleo requested a review from a team June 3, 2022 20:29
Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Nice refactor

Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

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

Nice. I just have a suggestion for further shortening.

Comment on lines 74 to 75
inherited_ancestors = Set.new.compare_by_identity
inherited_ancestors.merge(inherited_ancestors_of(constant))
Copy link
Member

Choose a reason for hiding this comment

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

I think we can do all of this on a single line:

Suggested change
inherited_ancestors = Set.new.compare_by_identity
inherited_ancestors.merge(inherited_ancestors_of(constant))
inherited_ancestors = Set.new.compare_by_identity.merge(inherited_ancestors_of(constant))

Comment on lines 94 to 95
ancestors = Set.new.compare_by_identity
ancestors.merge(ancestors_of(constant))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above:

Suggested change
ancestors = Set.new.compare_by_identity
ancestors.merge(ancestors_of(constant))
ancestors = Set.new.compare_by_identity.merge(ancestors_of(constant))

Co-authored-by: Ufuk Kayserilioglu <ufuk.kayserilioglu@shopify.com>
@egiurleo egiurleo force-pushed the interesting-ancestors-refactor branch from 5a26c12 to 8414af4 Compare June 6, 2022 19:27
@egiurleo
Copy link
Contributor Author

egiurleo commented Jun 6, 2022

@paracycle beautiful! Implemented your one-liners.

@egiurleo egiurleo merged commit b0990a9 into main Jun 6, 2022
@egiurleo egiurleo deleted the interesting-ancestors-refactor branch June 6, 2022 20:12
@paracycle paracycle changed the title Refactor the interesting_ancestors_of method in the Mixins tracker Refactor the interesting_ancestors_of method in the Mixins listener Jun 6, 2022
@shopify-shipit shopify-shipit bot temporarily deployed to production July 7, 2022 17:53 Inactive
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.

3 participants