StringBuilder and my Biggest Pet Peeve

What You Should Know About Strings

In both Java and .NET (and other languages) String objects are immutable. They don’t change. Period. If you “change” a String, it creates a new String. That includes String concatenation using a +


// One string created "foo"
String foo = "foo";
// foo exists, "bar" is created and the combination of foo and "bar" is a third string
String bar = foo + " bar";

Ok, if you don’t know this, fine. But if you don’t know this, why would you EVER use a StringBuilder?

Why Does StringBuilder Exist?

We know that Strings are immutable. If you need to do a bunch of string modification, concatenation, replacement – you will create a bunch of strings. Ok, great…why do I care? We care because we are told that creating a lot of Objects (and then later having to Garbage Collect them) is inefficient. To start with, I will guarantee right now that concatenating strings WILL NOT be the thing that prevents your application from performing. Guaranteed. Period.

Ok fine, it’s not going to be a problem. But you want to be a responsible coder and not do things that are intentionally inefficient if you can help it.

So you use a StringBuilder. StringBuilder is implemented internally as an array of characters. The code manages the allocation and copying of data to new arrays if the buffer gets filled. It sometimes over allocates the new buffer so that it has to perform allocations less often. You sacrifice a bit of memory overhead to avoid some Object creation and Garbage Collection later.

My Biggest Pet Peeve

Your use of StringBuilder is a premature optimization but probably a forgivable one.

So, WHY, OH WHY do you do this:

// One string created "foo"
StringBuilder sb = new StringBuilder();
sb.append("Foo: " + fooValue + " \n");
sb.append("Bar: " + barValue + "\n");

It makes me have violent thoughts. Please stop.

Update:
For some of the reasons why Strings are immutable, see this post on immutability and its positive qualities.

5 thoughts on “StringBuilder and my Biggest Pet Peeve”

  1. Cmon Geoff, you have bigger pet peeves than this! Hey, how does string.Format or sb.Format(“Foo: {0} \n”, fooValue) work? Does that create a new string as well? You like that better right?

  2. Chris,
    Actually the String formatting is often implemented internally as a StringBuilder, so in the case of a Format method of an existing StringBuilder it passes itself to a helper to do the manipulation.

  3. I would be really interested in seeing you write an article for Java or .NET folks to help reveal to them not only the fact that strings are immutable, but why the language designers were led to this decision or design or approach. It was very intentional; and potentially more educational than relying on edicts that are only learned about in certifications or on web pages. It might give folks a reason not to perfomr string concatenation within a stringbuilder atleast :P hehe.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>