-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Replace use of String.format(…)
with formatted Strings
#2751
Comments
jxblum
changed the title
Replace
Replace Oct 19, 2023
String.format(..)
with "".formatted(..)
String.format(..)
with "..".formatted(..)
jxblum
changed the title
Replace
Replace Oct 19, 2023
String.format(..)
with "..".formatted(..)
String.format("message", args)
with "..".formatted(args)
jxblum
changed the title
Replace
Replace Oct 19, 2023
String.format("message", args)
with "..".formatted(args)
String.format("..", args)
with "..".formatted(args)
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Oct 19, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Oct 19, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Oct 19, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Oct 19, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Oct 20, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Nov 21, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Nov 21, 2023
jxblum
added a commit
to jxblum/spring-data-redis
that referenced
this issue
Nov 21, 2023
mp911de
changed the title
Replace
Replace use of Sep 10, 2024
String.format("..", args)
with "..".formatted(args)
String.format(…)
with formatted Strings
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This task replaces all uses of
String.format("..", args)
with"..".formatted(args)
(formattedStrings
).From my perspective, the code has:
Much improved focus and visibility on the "message" being crafted for
Exceptions
or LOG messages FIRST rather than boilerplate code used to format theString
message, and...Reduced, unnatural line breaks in the source code, such as, but not limited to:
From my perspective, the above is not as readable as:
This refactoring also reduced the use of the following technique to avoid weird, unusual and unnatural source code formatting by the code formatter:
Thereby keeping the "message" with the Exception.
Although, I must WARN that, in certain cases, the
Exception
"messages" was unusually long with placeholders spread throughout, such as:In which case, when using
"..".formatted(args)
you need to wrap the concatenated message with parentheses, like so:("multiline, concatenated message, with placeholders").formatted(args)
This is a minor trade-off, and I do not think it impairs the readability in any way.
Additionally, and fortunately, the IDE is smart enough to pick up on this and wrap the formatted String appropriately.
I considered the use of text block (or multi-line Strings) in Java, but you must be careful since the text block will preserve tabs and other (potentially) hidden characters (e.g. newlines) contained in the String.
All in all, even despite the necessary use of
()
in only a few places, I think the use of"..".formatted(args)
is much cleaner, clearer to read, and more succinct thanString.format(..)
.The text was updated successfully, but these errors were encountered: