This is an AMP version of the article, its original content can be found here.
Avoid String Concatenation
This is "string concatenation", and it is a bad practice:
Why? Some may say that it is slow, mostly because parts of
the resulting string are copied multiple times. Indeed, on every
String class allocates a new block in memory and copies everything
it has into it; plus a suffix being concatenated. This is true,
but this is not the point here.
Actually, I don't think performance in this case is a big issue. Moreover, there were multiple experiments showing that concatenation is not that slow when compared to other string building methods and sometimes is even faster.
Some say that concatenated strings are not localizable because in different languages text blocks in a phrase may be positioned in a different order. The example above can't be translated to, say, Russian, where we would want to put a name in front of "привет". We will need to localize the entire block of code, instead of just translating a phrase.
However, my point here is different. I strongly recommend avoiding string concatenation because it is less readable than other methods of joining texts together.
Let's see these alternative methods. I'd recommend three of them (in order of preference):
There is also a
but I don't find it as attractive as
StringUtils. It is a useful
builder of strings, but not a proper replacer or string
concatenation tool when readability is important.
is my favorite option. It makes text phrases easy to understand
and modify. It is a static utility method that mirrors
sprintf() from C.
It allows you to build a string using a pattern and substitutors:
When the text is longer, the advantages of the formatter become much more obvious. Look at this ugly code:
This one looks much more beautiful doesn't it:
Please note that I'm using argument indexes in order to make the pattern even more localizable. Let's say, I want to translate it to Greek. This is how will it look:
I'm changing the order of substitutions in the pattern, but not in the actual list of methods arguments.
When the text is rather long (longer than your screen width),
I would recommend that you use the utility class
from Apache commons-lang3:
The need to include an additional JAR dependency to your classpath may be considered a downside with this method (get its latest versions in Maven Central):
Similar functionality is provided by
from Google Guava:
It is a bit less convenient than
StringUtils since you always
have to provide a joiner (character or a string placed between text blocks).
Again, a dependency is required in this case:
Yes, in most cases, all of these methods work slower than a plain simple concatenation. However, I strongly believe that computers are cheaper than people. What I mean is that the time spent by programmers understanding and modifying ugly code is much more expensive than a cost of an additional server that will make beautifully written code work faster.
If you know any other methods of avoiding string concatenation, please comment below.