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

Remove concatenation in String.format() calls #13038

Merged
merged 1 commit into from
Jan 29, 2024

Conversation

sabi0
Copy link
Contributor

@sabi0 sabi0 commented Jan 28, 2024

Using concatenation in the format string like this:

              String.format(
                  Locale.ROOT,
                  "top: warning: still warming merge "
                      + info
                      + " to "
                      + preCopy.connections.size()
                      + " replicas for %.1f sec...",
  • is inconsistent
  • is error prone (the info.toString() result above might happen to contain some 100%s)
  • results in longer code
  • that is more difficult to modify

Replacing the concatenation with the format placeholders results in a cleaner more compact code:

              String.format(
                  Locale.ROOT,
                  "top: warning: still warming merge %s to %d replicas for %.1f sec...",
                  info,
                  preCopy.connections.size(),

@uschindler
Copy link
Contributor

uschindler commented Jan 29, 2024

Thanks for this. The code also had safety problems: If the concatted text contains percent signs it would break the format string. So format strings should never ever be constructed with variables containing unchecked strings.

@uschindler uschindler self-assigned this Jan 29, 2024
@uschindler uschindler merged commit e3a6a4b into apache:main Jan 29, 2024
4 checks passed
@sabi0 sabi0 deleted the string_format_concatenation branch January 30, 2024 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants