- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I can't work out which is worse...the abuse of string concatenators, or the fact that there are still people out there who write SQL like that.
Still, I've seen worse.
Admin
I believe that, when executed, this code can make the universe explode.
Admin
He didn't write the SQL like that. It was the code that wrote it.
Admin
comments.append(first).append(post).append(post).append(wtf).append(post).append(troll).append(post).append(wtf).append(self);
Admin
That's what happens when one takes "abstract thinking required" in his job a bit too seriously.
Get a ream of Z-fold, run a daisy wheel printer on it and you've got yourself an art installation.
My first thought was "Looks like abstract art to me". And then I started wishing it was in fact abstract.
Admin
This has to be the uncontested winner of the "Most thorough misunderstanding of the point of StringBuffer()" award.
Admin
Surely that would go to something I saw earlier today.. (.net so StringBuilder instead of StringBuffer)
StringBuilder sb = new StringBuilder(); sb.Append(customer.FirstName + " " + customer.LastName + "\r\n");
return sb.ToString();
Admin
I don't get it. If the code generation "tool" is still around, why can't they add new columns to tables? I mean, the code sure is ugly, but it's generated anyway so it shouldn't be maintained. On the other hand, I don't think that refactoring this (only when needed to add a single column) should be that hard.
Captcha: cogo. Like thinking, but shorter.
Admin
Oh, he's using stringbuffer for improved speed! Just concatenating strings would take way too long... Not a wtf... no... not at all...
Admin
I also just saw something similar using Java's String.format():
return String.format ("%d", someInteger) + " blah";
Admin
Code writing code?! What are you trying to put us out of work!!!!!!
{I am quite capable of writing bad code on my own. I don't need a stinkin' machine to do it for me!}
Admin
No way can I accept that somebody actually wrote that code thinking it was right.
Are there really programmers out there who are that ignorant?
Admin
And to think people grumble about jQuery's ability to chain methods...
Admin
Key comments there in the exposition. "Formatted by yours truely" and "This is one of the smaller statements"
This is probably what this smaller statement looks like in the actual source:
Talk about something that would be a nightmare to maintain
Admin
Looks more like an abject fart. But I guess that's close enough for some.
Admin
Admin
I know the WTF here isn't specifically about the StringBuffers but it is a WTF to see how many people misuse this functionality. I have a little performance project set up in my dev environment that I pass on to my colleagues whenever they start using StringBuilders (.NET) for string concatenation. It basically shows that you need to be making at least 100 concatenations before the StringBuilder class offers any performance increase over regular string concatenation. This is invariably an order of magnitude greater than the number of concatenations they are actually making. StringBuilder is a widely misunderstood class that, due to its misuse, is probably responsible for hindering performance more often than it improves it.
Admin
I've seen code like that explode a server ... a couple thousand process forks later ...
Admin
Admin
And besides that, there is a second WTF: that piece of code is vulnerable to be sql injected :P
Admin
What I love about this is that after literally laughing out loud, developers will find it utterly impossible to explain to non-developers what they are laughing at.
Admin
Wow, that is a WTF.
Admin
While not -impossible- to explain, it's just not going to be funny to them. And while I didn't LOL, I did say 'Nooo!'
Admin
short answer: yes.
Admin
Admin
Well, good for you. You actually wrote code and tested something. Do you do this for every function call in every library you use? Or just the ones you're arguing about? I mean, empiricism rules and all, but generally depending on the tech documentation written by the people who actually, you know, designed and wrote the library, is a pretty reasonable thing to do.
Admin
The real WTF™ is that when writing this article, someone must have mixed up their Java and C#:
Admin
So do it! I see what the code does for once, I guess the problem is in the /r/n at the end?
I am a non-developer. But I took a class on Java once!
Admin
Do you mean StringBuffer? If so, StringBuffer is a faster way to do the operation you outlined.
Admin
Admin
But a key part is this:
Perhaps you are the one who doesn't know how to use StringBuilder correctly? If you make the StringBuilder big enough to start with, it shouldn't need to resize itself which is it's biggest drain on performance. Why don't you put your money were your mouth is and post your code? I do, however, agree that you should test your particular situation in your code before you draw any conclusions if you are really concerned about performance.
Admin
Wow. Its funny how one's sarcasm is measured by one's own ego.
In my own testing of StringBuilder, I have found the opposite of what you have, but I dont come here to trash someone and feel better about myself doing it.
Admin
I like the artist's use of negative space.
Admin
Well, not really!
'bol.Consignee' is protected from SQL Injection -- by doubling all apostraphe characters.
'bol.BolNum' and 'bol.State' are not so protected, which may or may not be a problem: If one relies on drop-down selection of values in HTML, then a hacker could do a SQL Injection attack. But if the values are validated on the server before calling insert, then SQL Injection would not be possible.
Admin
I pasted it into Eclipse to refactor it. Eclipse locked up!!! Yes, it really did!!!
(So while it didn't destroy the universe, it certainly destroyed the sun! (...or maybe just the moon. ;-)
Admin
The 'customer.getFirstName() + " " + customer.getLastName() + "\r\n"' in Java gets compiled in:
sb.append(customer.getFirstName()) sb.append(" ") sb.append(customer.getLastName()) sb.append("\r\n")
Admin
Well, on that point I would agree with the article. If you are using concatenation in a loop from 1 to 10, then the performance does not matter - so you should use StringBuffer so that it doesn't explode if for some reason later it somehow gets called 100000 times.
Admin
"The goggles, they do nothing!"
Admin
Someone actually tested this and posted results with 8+ strings StringBuilder is more efficient. So if there is a chance of using 8+ strings ever use StringBuilder.
Admin
OH I remember working with a programmer like steve. We were sending emails from VB.NET, I told him to use a string builder because there was a table in there.
And so he did, the entire auto generated email was a long chain of calls to the same string builder (at least it was the same one).
I changed that for a custom template text file.
Admin
Seems to support the StringBuilder being a full order of magnitude faster, even for relatively small numbers of concatenations and relatively small strings. For very large numbers of concatenations, like for example an in-memory log or writing out markup or something, I can see a difference of almost two full orders of magnitude. As you would expect, the StringBuilder time grows linearly while straight concatenation time grows exponentially.
The only edge case seems to be when the total length is around 50 characters, and even then the StringBuilder is still a little faster. I can imagine that there might be some very specific circumstances under which it would be slower, but certainly not by a wide margin. And a standard error message or database command is probably going to be at least a few hundred characters, so the case of 50 characters is essentially irrelevant anyway.
This took me all of 5 minutes. Just reading the MSDN documentation (assuming I didn't already know the answer, which I did) would have taken 10 seconds. I'm guessing that you did the majority of your "research" in your ass.
Admin
Acknowledging the last four items, Java has both StringBuffer and StringBuilder. Check the javadoc: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
Admin
[quote=ObiWayneKenobi] The real WTF™ is that when writing this article, someone must have mixed up their Java and C#:
Java also has StringBuilder as a non-thread-safe version of StringBuffer since version 1.5. That doesn't excuse the other issues, though.
Admin
OH FUCK WHY DOES THAT ALWAYS HAPPEN???????
Admin
Admin
OK, that first result looks a little off, I think it must be some jitting artifact. Changed it to run the first test twice and here's the result:
So it looks like for the most trivial possible concatenation, the StringBuilder takes 9% longer. If you're doing exactly 2 concatenations of very short strings in a tight loop, you might want to use regular concatenation (duh).
Admin
Admin
Admin
How convenient. That way you don't have to show that you're talking out your ass and don't know how to use StringBuilder. Put up or shut up.
Admin
It's great how you chastise somebody for looking it up on MSDN and not writing a real test app and then you chastise somebody else for writing a test app. TopCod3r is back!