My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
String fullName = new StringBuilder().Append(firstName + " " + middleName + " " + lastName).ToString();

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!

Leave a comment

Is the "+" operator the correct one to be using? What is it doing? Throwing an error? Or giving an unexpected result?

Posted by Ben Nadel on Nov 19, 2007 at 04:47 PM UTC - 5 hrs

The code works (well, let's assume for the sake of argument it does - I didn't actually test it).

There is something wrong conceptually, rather. (Also assume + operator is the correct one for concatenating strings).

I don't want to give too much away - just wondering if anyone will catch the concept-error (or what I think of as an error). =)

Posted by Sammy Larbi on Nov 19, 2007 at 05:56 PM UTC - 5 hrs

Not sure what language this is (if it's not psuedo code that is) but here are a few thoughts:

Surely StringBuilder is responsible for building strings (in a more efficient way than concatenation), maybe from multiple arguments (or multiple calls to the append() method), so using string concatenation on the argument is pointless.

Also one would assume StringBuilder returns a string, so calling toString() seems superfluous too.

Posted by Dave Spurr on Nov 19, 2007 at 06:23 PM UTC - 5 hrs

Well, StringBuilder would be a StringBuilder, and the toString method does get the string that got built - so that's not superfluous that I know of.

But you are right about the meat of the matter - it is pointless (to my knowledge) to be using a StringBuilder and building it yourself with concatenation.

I just saw something similar in code this morning and had to put it out there!

Posted by Sammy Larbi on Nov 19, 2007 at 09:07 PM UTC - 5 hrs

Good point Sammy. Something else that is useful to know - the cfsavecontent tag uses a java StringBuffer under the hood to concatenate it's values. So if you are building up a large string manually, it's better (if possible) to use cfsavecontent rather than lots of mystring = mystring + nextbit statements, unless of course you directly use a java StringBuffer.

Posted by Kevan Stannard on Nov 20, 2007 at 01:30 AM UTC - 5 hrs

As pointed in earlier comments - the problem is string concatenation. The difference between concatenating string with + vs with StringBuilder, at least in java and .net is the following:
String a = "string_a";
a = a + "-string_b";

This will allocate memory for "string_a", then allocate memory for "string_a-stringb", but won't free the already allocated memory for "stirng_a" until GC runs, where StringBuilder will just append it to the stack

Posted by Emil Ivanov on Nov 20, 2007 at 02:06 AM UTC - 5 hrs

Hey all :)

(In response to Emil's comment)

in your code,
string a = "string_a";
a = a + "-string_b";

both "string_a" and "-string_b" are string literals and are stored in the assembly's meta data. So neither are actually new string objects, and don't require allocation nor garbage collection. This can be seen by looking at the IL produced by the code. The instruction generated is ldstr, which sets 'a' to be a reference to the string in the metadata.

Usually when I need to do something like the original code segment, I make use of String::Format.

string fullName = String.Format("{0} {1} {2}", firstName, middleName, lastName);

It makes use of StringBuilder internally, and is a tad easier on the eyes, IMHO. :)

I've seen some dewsies with regards to string manipulation.. for instance,

string blah = "c:\\" + "Program Files\\" + "someFolder";

type stuff. That was pretty hilarious. Luckily they're all literals and the compiler recognizes them as such. But my coworker and I had a laugh.

Posted by Glitch on Nov 26, 2007 at 07:17 PM UTC - 5 hrs

@Kevan - I was not aware CF used string builders/buffers in the underlying implementation. That said, I'm wondering where or how? When I concatenate 2 cfsavecontent tags under the same variable? Otherwise, it just seems the same to me as:

<cfset someBigString = "alot of stuff here with #vars# and another #var# and perhaps a

\newline or two.">

Something else I'm not thinking of at the moment, probably?

@Emil - I think you sound as if you're focusing too much on the memory (I know you said the allocation of, but to someone not in the know it sounds like you're more worried about space complexity than time complexity). The point is it's generally faster using StringBuilder (.NET) or StringBuffer (Java) to append one string to another than it is doing string1+string2 (which appends string 1 to string 2). (I know you know that)

Of course, you mention that, but I only try to clarify for the unknowing.

@Glitch - Awesome - didn't know you blogged. I've subscribed to it (and to Kevan's) - I'm liking what I see so far!. For those that don't know, I'm in class with Glitch making a game he's blogged about, and he blogs about game making in XNA more than I likely ever will, so if you enjoy that stuff like I do, I'd check him out.

Glitch also makes a point (especially for "statically typed" languages) that I'd like not to forget:

"String.Format("{0} {1} {2}", firstName, middleName, lastName) ... makes use of StringBuilder internally" and it "is a tad easier on the eyes."

First, the eyes are the most important part of software development, in almost all cases.

Second, don't prematurely optimize. As you can tell from his statement about internal use, a good optimizing compiler will do it for you - just write clear code so it can know what to look for!

Posted by Sammy Larbi on Nov 26, 2007 at 11:23 PM UTC - 5 hrs

The worst way to create a string is to + them. What it will do is make new strings every time an + is added. So, therefore you will use the stringbuilder. With the append you can nicely append extra string parts. But at this moment first the complete string is builded very inefficient and then added to the empty string object…?!?

Posted by Patrick on Mar 23, 2010 at 07:49 AM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner