-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
Conversation
Signed-off-by: Deepak <sdeepaksharma15@gmail.com>
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 |
This is best if rewrite not accepted can we still add a regex in checkstyle to fail the build. Your thoughts @Pankraz76 |
nice idea, yes give it a try in dedicated pr. |
IIRC the checks If you want to replace them you could introduce an |
All classes under the As for all the other instances, we're really turning As a result, I'm declining this PR because I don't think there are other cases than the two types listed above. |
Thank you for detailed feedback, @bclozel |
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.