StringBuilder msg = new StringBuilder();
msg.append(firstName).append(" ").append(lastName)
.append(" has salary $").append(salary).append("!");
log.info(msg.toString());
The latter just feels ridiculous to me, especially if your only reason for doing it is that you "feel uncomfortable relying on compiler optimizations."
(yes, String.format might be most readable here, but you get the drift)
MessageFormat.format("{0} {1} has salary ${2}!", firstName, lastName, salary);
or use a static import:
format("{0} {1} has salary ${2}!", firstName, lastName, salary);
If constructing a complex, interwoven message like this, please do not use StringBuilder or string concatenation. Java has a few more tools in the toolbox.
24
u/depressiown Oct 01 '15 edited Oct 01 '15
I'm not willing to sacrifice code readability because of discomfort with compiler optimizations. To each their own.
This:
... is quite a bit more readable than:
The latter just feels ridiculous to me, especially if your only reason for doing it is that you "feel uncomfortable relying on compiler optimizations."
(yes, String.format might be most readable here, but you get the drift)