Skip to content

Conversation

chishui
Copy link

@chishui chishui commented Jul 24, 2025

Description

Found some classes don't have good encapsulation and doesn't offer accessors for its fields. It makes mocking MergeState in unit tests difficult.

We can't modify the access level for these fields, so I add getter functions for all these public fields.

Copy link
Contributor

This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.

Copy link
Contributor

This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.

@chishui chishui changed the title Add accessors for MergeState's public fields Add accessors for MergeState and SegmentCommitInfo's public fields Jul 25, 2025
Copy link
Member

@rmuir rmuir left a comment

Choose a reason for hiding this comment

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

Sorry, I can't agree with adding public getters for public fields.

/** Returns whether the index needs to be sorted. */
public boolean getNeedsIndexSort() {
return needsIndexSort;
}
Copy link
Member

Choose a reason for hiding this comment

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

Adding a getter to a public field is senseless java verbosity: it adds no value. The field is public: just access the field directly.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for reviewing. I fully understand that you can just use public fields but that's not the point. Like what I mentioned in the description, it provides better testability, we can easily mock a member function but we can't mock a field access.

Apart from that, it's not good practice to have public fields as it doesn't provide good encapsulation. It's just the fields are already public and for backward compatibility, we can't change them to private.

Copy link
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the [email protected] list. Thank you for your contribution!

@github-actions github-actions bot added the Stale label Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants