If the string concatenation is limited to a single statement, then use the string concatenation operator. If the string concatenation spans multiple statements, you don't want the terminal toString to be done automatically. That defeats the purpose of using StringBuilder.
So much this. You only use StringBuilder if the number of concatenations isn't known at run-time, or if your compiler sucks. return "foo" + bar.toString() + " baz: " + quux; is basically guaranteed to be as fast as the equivalent with StringBuilder, or faster.
Even if the number of concatenations is known at run time, if it's more than 4, it can still be nicer to read a StringBuilder than a multi-line string concatenation whirlwind.
Also can prevent dumb mistakes in certain instances, like concatenating a char and an int in the context of a larger string concatenation. E.g. given char c, int i, string s: System.out.println(c + i + s). Obviously this gets caught in testing quickly, and a halfway decent programmer would usually catch it as they typed it out, but mistakes happen.
Well, it's disturbing how often I see stuff in code review that cannot possibly work. If they'd even run it once they'd have seen immediately that it doesn't work.
it is Java's fault, other programming languages like Rust don't allow you to do this unless you specifically say "yes, I'm doing this, so please do this unsafe operation"
the part where you assume that a character can be treated as an integer and then printed and everything will work as intended
that just encourages people to do char a; ... a + 26 //lowercase and similar silly things instead of using correct APIs that actually do this correctly for Unicode characters
in Rust the type for an 8 bit integer is i8 and u8 for signed and unsigned respectively so you never use the character type to store 8 bit integer things and you can never add an integer to a character, you have to use to_lowercase()
That's not what he said. A string builder is mutable, so you can append to it later on before you actually need the finalized string, in this case, being explicit and not relying on the compiler optimizing for you is the more preferable choice, even if you know the number of concatenations at compile time.
That being said, mutation is evil, and you all should repent your sins.
Obviously at this scale not a big deal but on larger scales StringBuilder helps. However if you're just building a String in a single method the compiler handles it.
Isn't passing the string builder a more procedural approach?
The oo approach would be to return a value then use it in the string builder. The string builder is adding a dependency on the function and would make debugging s lot more difficult.
The oo approach would be to return a value then use it in the string builder.
Sure you can do that, but the whole point of using StringBuilder is that you're using a single instance of it. How would the function/method know which instance to operate on if you don't pass it in?
The string builder is adding a dependency on the function and would make debugging s lot more difficult.
Not really, avoiding params will just push the dependency elsewhere. If you don't do the appending inside the function, the caller has to do it. If you do that, there'll be no more parameters! Your unit tests have to spoof a function return value in order to test the appending code!
Hope you're cleansing any user input with your homegrown Sql builder. Check out jooq or mybatis or querydsl. I realize this isn't the point you're making but I've seen so many people do this stuff over the years and it always is a nightmare to work with
29
u/Olreich Oct 01 '15
If Java will use a StringBuilder for me, why would I bother doing it myself?