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

Cleanup String.format() usage #14404

Open
shounakmk219 opened this issue Nov 7, 2024 · 4 comments
Open

Cleanup String.format() usage #14404

shounakmk219 opened this issue Nov 7, 2024 · 4 comments

Comments

@shounakmk219
Copy link
Collaborator

The poor performance of String.format() is brought up multiple times in different PR reviews (recently)

We should either move to to using + or StringBuilder instead of String.format() and cleanup the existing usage of String.format() with the decided option as well.

ref - https://www.baeldung.com/java-string-concatenation-methods

@gortiz
Copy link
Contributor

gortiz commented Nov 7, 2024

I think we should use string concatenation (+) by default and only using the other alternatives when there is an actual reason. For example, in loggers or guava preconditions using their own {} or %s will be better because it won't allocate the string in cases where the log is not generated or the precondition doesn't fail.

@ashishjayamohan
Copy link
Contributor

ashishjayamohan commented Nov 11, 2024

@gortiz @shounakmk219 I see that there are a lot of usages of format in ControllerRequestURLBuilder. Would it be advisable to replace these with concatenation? I see a lot of them could potentially make sense as format uses (as it is slightly more readable)

@gortiz
Copy link
Contributor

gortiz commented Nov 11, 2024

as it is slightly more readable

I think that is very subjective. People coming from C background may find it more readable, but people coming from more modern languages where strings can be concatenated with + (Java, JavaScript, Go, Python, etc).

IMHO the only place use case format is acceptable is when we need to print in a formatted way. For example, when we need to print an in using exactly 8 characters (filling with spaces if necessary). In all other cases it is not easier to read.

In terms of performance it is always the worse alternative, given they are not optimized at compile time (like a macro in Rust would be).

@ashishjayamohan
Copy link
Contributor

That makes sense to me as well, just figured I'd check in with you before making that change. I've refactored a lot of usages in my PR. Let me know if anything looks off!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants