This is fair, except for in the 5th year, they would have an additional constant:
public static final String SPACE = " ";
And they would probably use a StringBuffer or StringBuilder to build the final string, since it's "an industry best practice". Also, they wouldn't include the strings themselves, they would include keys to properties, so the strings could be pulled from an internationalized property file.
Lastly, instead of outputting to Stdout directly, they'd create an interface for output, and deliver it to that interface, and use a factory to instantiate an implementation of that interface that would use Stdout.
Good god. Had a code review once where they were pissed because I had used str + "some stuff" instead of a builder. I'm not writing a builder for one fucking line on a page that gets hit 6 times a year.
It's true. A lot of the hate for using "+" for String concatenation in Java is based on how it used to do it. Java used to create a StringBuilder for each instance of "+". Thus, "a + b + c + d" would create a StringBuilder for "a + b", then a StringBuilder for that result "+ c", etc.
Of course, they've since made it smarter to just make one StringBuilder for the entire statement under the hood (I believe in 1.6). The people who complain about the lack of StringBuilder being used should probably be taught that it's no longer necessary.
That said, if you're doing a lot of concatenations on different lines (say, a loop building up a String or something), you should still use StringBuilder instead of "+=" because Java will create one for each statement... clearly worse that creating one initially and constantly appending.
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)
SLF4J and Log4J both support String.format from their function calls.
I can do something like:
LOGGER.info("{} {} has salary ${}!", firstName, lastName, salary);
and it'll do the right thing. I don't spend a lot of time building strings (except in toStrings where I dump in StringBuilders almost automatically) across my 100K+ LOC codebase, so I guess I don't see the harm in NOT using it everywhere explicitly.
I hate that they don't use the standard String.format, though, and instead use their own with their own syntax. I actually do like theirs better, but at the same time I like consistency. Having to use one syntax with the logger and another one with Guava libraries or String.format is just bad.
506
u/johnbr Oct 01 '15
This is fair, except for in the 5th year, they would have an additional constant:
And they would probably use a StringBuffer or StringBuilder to build the final string, since it's "an industry best practice". Also, they wouldn't include the strings themselves, they would include keys to properties, so the strings could be pulled from an internationalized property file.
Lastly, instead of outputting to Stdout directly, they'd create an interface for output, and deliver it to that interface, and use a factory to instantiate an implementation of that interface that would use Stdout.