2

I have some code like this:

StringBuilder rgba = new StringBuilder();  
for (...){
....
rgba.append(value + ", ");
}

IntelliJ shows me the following warning:

string concatenation as argument to 'stringbuilder.append()' call

From here, here and other sources I understand that concatenation is actually creates a new String object and that negates the whole point of using a StringBuilder.
So IntelliJsuggests me to use

rgba.append(value);
rgba.append(", ");  

instead of

rgba.append(value + ", ");  

But will this really be better?
Will this code be clearer?
Will this make the execution faster?

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1
    frankly, for this particular scenario I wouldn't give a second thought about "what is better" (how better? by which metric?). Concerning clearer, I would say "agree with Intellij" here, as you already have a SB instance anyway, would look more concise to just write one more `append` call, rather than throw concatenation in the mix – 62mkv Mar 08 '21 at 11:51
  • 1
    actually Intellij provides the reasoning concerning performance as well: Reports String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder allocation. This inspection ignores compile time evaluated String concatenations, which when converted to chained append calls would only worsen performance. – 62mkv Mar 08 '21 at 11:54
  • 1
    The append code will be clearer to the humans that have to read your code months or years later, including yourself. – Gilbert Le Blanc Mar 08 '21 at 12:37

3 Answers3

2

as per IntelliJ reasoning:

Reports String concatenation used as the argument to StringBuffer.append(), StringBuilder.append() or Appendable.append(). Such calls may profitably be turned into chained append calls on the existing StringBuffer/Builder/Appendable, saving the cost of an extra StringBuffer/Builder allocation. This inspection ignores compile time evaluated String concatenations, which when converted to chained append calls would only worsen performance.

so, I would say: go for it!

62mkv
  • 1,444
  • 1
  • 16
  • 28
1

What you've said is correct in the sense that a mixture of concatenation and StringBuilder goes against the point of using it in theory, but the extra performance we're talking about is incredibly minimal unless you're loop has a massive number of iterations.

In terms of code clarity I think the double append is actually a lot clearer considering you're using it anyway.

I would see the two statements as

Append ", " appended on "value"
Append "value"
Append ", "

of which the second actually seems more concise, even if the code is technically longer.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
0

In my opinion concatenation is much more readable for developers but StringBuilder.append is performant

mflorczak
  • 264
  • 3
  • 10
  • 1
    `rgba.append(value).append(", ")` can also be considered as clear as `rgba.append(value + ", ")` (or even clearer IMHO, when I want to *highlight* appending the comma) –  Mar 08 '21 at 12:51