Skip to content

Use Collections#isEmpty or String#isEmpty #35243

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
wants to merge 2 commits into from

Conversation

iddeepak
Copy link

@iddeepak iddeepak commented Jul 25, 2025

Summary
This PR improves readability and consistency across the codebase by replacing manual size/length checks with more idiomatic methods:

Replaces collection.size() == 0 or collection.size() != 0 with collection.isEmpty() / !collection.isEmpty()

Replaces string.length() == 0 with string.isEmpty()

Motivation
These changes align with modern Java best practices:

isEmpty() is more expressive and self-documenting than comparing .size() or .length() to 0.

This is similar to the MNG-8060 change made in Apache Maven and #10945, which improved code clarity.

Please check :
[Experimental] Add rewrite support for errorprone.refasterrules.StringRulesRecipes
Convert value.trim().length() < 1 to value.isBlank() < 1
https://error-prone.picnic.tech/refasterrules/StringRules

These patterns are common and can be enforced or auto-corrected via CI using static analysis and refactoring tools.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 25, 2025
Signed-off-by: Deepak <sdeepaksharma15@gmail.com>
@Pankraz76
Copy link

well done, like always!

If not done by some plugin this is kind of waste as it will occure any time again.

If spring really want to solve this issue, by having solution like in check:

Kindly let me know @bclozel

@iddeepak
Copy link
Author

This is best if rewrite not accepted can we still add a regex in checkstyle to fail the build. Your thoughts @Pankraz76

@Pankraz76
Copy link

nice idea, yes give it a try in dedicated pr.

@mdeinum
Copy link
Contributor

mdeinum commented Jul 28, 2025

IIRC the checks collection.size() == 0 and collection.size() > 0 are deliberate in there. Although it is tempting to use collection.isEmpty() and !collection.isEmpty() they are actually hard to distinguish from one another. The ! is quite easily missed or added by accident, hence usage of .size() instead for readability and to reduce mistakes.

If you want to replace them you could introduce an isNotEmpty method (next to the isEmpty) on the CollectionUtils class and delegate to that. The same could apply for the String checks.

@bclozel
Copy link
Member

bclozel commented Jul 28, 2025

All classes under the asm.* and cglib.* packages are repackaged classes from the original projects. We track the original projects, update and maintain local patches. Changing the code in this namespace for stylistic reasons is just likely to make our maintenance process harder and error-prone. Readability is not really an issue here, because we only maintain the patches, not the rest of the code. In my opinion those are the only good instances of changes where we have .size() == 0 and .length() == 0. I would suggest sending PRs to the original projects here if you believe this improves readability and maintenance.

As for all the other instances, we're really turning something.size() > 0 into !something.isEmpty(), which I don't think improves readability.

As a result, I'm declining this PR because I don't think there are other cases than the two types listed above.
Thanks!

@bclozel bclozel closed this Jul 28, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 28, 2025
@iddeepak
Copy link
Author

Thank you for detailed feedback, @bclozel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants