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 - 6 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 - 6 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 - 6 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 - 6 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 - 6 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 - 6 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 - 6 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 - 6 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 - 6 hrs
Leave a comment