Skip to content

Change .size() == 0 to .isEmpty() for Collections #15904

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

Closed
kwonyonghyun opened this issue Oct 12, 2024 · 1 comment
Closed

Change .size() == 0 to .isEmpty() for Collections #15904

kwonyonghyun opened this issue Oct 12, 2024 · 1 comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement

Comments

@kwonyonghyun
Copy link
Contributor

Expected Behavior

  • All checks for empty Collections (Lists, Maps, etc.) should use the .isEmpty() method instead of .size() == 0.

Current Behavior

  • The current codebase uses .size() == 0 to check if Lists and Maps are empty.

Context

  • This issue affects code readability and adherence to Java Collections Framework conventions.
  • We are trying to improve code clarity and follow best practices in Java programming.
  • Alternatives considered: Keeping the current .size() == 0 checks, but this doesn't align with standard Java conventions.
  • No workarounds are necessary as both methods are functionally equivalent for checking emptiness.

Proposed Changes

  1. Replace all occurrences of .size() == 0 with .isEmpty() for Collections (Lists, Maps, etc.).
  2. Update any related documentation or coding guidelines to reflect this change.

Reasons for the proposed change:

  1. Improved readability: .isEmpty() more clearly expresses the intent of checking whether a collection is empty.
  2. Adherence to Java Collections Framework conventions: isEmpty() is the standard method for checking if a collection is empty.

Questions

  • Is there a specific reason for using size() == 0 instead of isEmpty() in our current codebase?
@kwonyonghyun kwonyonghyun added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Oct 12, 2024
@kwonyonghyun
Copy link
Contributor Author

I've submitted PR #15898 to address this issue. I would greatly appreciate if you could review the changes and provide your feedback.
Please take a look at the implementation and let me know if you have any concerns or if there are any reasons why this change might not be feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant