- 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
Now that's just terrible.
Admin
oneGottaLoveReallyLongFunctionNamesBecauseTheyAreSoDescriptive()
Admin
Shouldn't that method be named like this:
convertSingleQuoteAndDoubleQuoteUnicodeToAscii
?!?
(gotta do it right if at all)
Admin
You gotta love how the only two comments in the function are wrong too. ' != double quote.
Admin
Form and function are completely mistaken in this one.
Admin
ThisIsTheNewFunctionToConvertSingleQuoteAsciiToCharactersActuallyNowItAlsoConvertsDoubleQuoteAsciiToCharacters
Admin
Back on topic, though, isn't that an HTML Entity to ASCII converter, and not ASCII to character?
convertSingleQuoteAndDoubleQuoteHTMLEntityToASCIICharacters() would be a more fitting name for the function... ;)
Admin
This method should really be called convertFirstSingleQuoteAndDoubleQuoteAsciiToCharactersAndGoIntoAnInfiniteLoopIfThereAreMore.
Admin
Admin
As a very broad and general rule, once I've descriptively named a method something like that, it tells me I should rethink the method. After all, if it takes that long to describe it, it's possible that it needs to be refactored.
Not a hard and fast rule but it at least gets me thinking.
Admin
Sometimes I think some programmers try to rewrite some useful code at locations where it would be extremely useless, just to show they are capable at creating garbage that looks nice.
I think I will have to get some more dried frogpills.
Admin
I especially like:
and There's nothing like a good old misleading comment to set you up for the rest of the code...Admin
I must admit it took me a while to notice said loop. :-.
Admin
You're not the only one :( I'd like to blame my lack of experience with Javascript, but I don't think that would really do anything to mitigate my shame.
Admin
Admin
How true, how true, particularly when the thinking get's you to look in the class library and notice String.Replace(). Be careful, this usually coincides with possibly unexpected performance gains as well.
Admin
no kidding.
To the author of the original code: Keep your hands in your pockets next time you see a computer, trust me.
Admin
'How did their data get “'” in the first place?'
TinyMCE does this in certain circumstances. At least, I find myself having to clean them up, for reasons I am not clear on. It would certainly explain why there was a need to process them in javascript.
Admin
Has anyone noticed how many of these involve string processing? I suppose string processing is kind of annoying sometimes, but why does it produce so many glorious WTFs?
The infinite loop is teh awesome. DoS, anybody?
Admin
Yes, but it's Self Documenting©
Admin
I've seen countless coders really infatuated with strings. Doing things like <number>.toString().trim(), which would be toString()'ed AGAIN some lines later, then converted back to numeric. String being like a panacea: not working? .toString() it!
Being so lovely and overused, it's no wonder why they end up WTF'ed...
Admin
All things considered, it's probably best that the guy who wrote this doesn't know about regexes.
Admin
Although a WTF by any other name...
Admin
It should be named
replaceCharacterEntities(string)
Admin
Perhaps I'm being stupid, but what's wrong with this code, other than being redundant? I don't see why it would infinite loop, possibly because I've never tried to manipulate strings in ecmascript.
Admin
No, it shouldn't.
Let's all, please, divorce ourselves of the idea that Unicode is a character set, or character representation in any way, shape or form. It is not. Unicode is an attempt at a canonical set of all written glyphs, no matter what language or languages they occur in, with a unique value assigned to each. A character set is a specification of binary representations of a set of glyphs. UTF-X are also NOT UNICODE. They are character sets which provide specifications for the binary representations of glyphs contained in the Unicode canon.
What this guy was trying to do was convert the HTML representation of the Unicode value of a given glyph to an ASCII character set representation of the same glyph.
Why is Unicode so hard?
Admin
It took me a shamefully long time to figure it out as well. It's because in his first loop if a 2nd entity is found it will use the original string to replace it. So basically it will flip-flop between the two ' strings converting one to a ' and having the other one reverted back to a '
Admin
Well... take a look at the first loop, and check what happens when a characted index from ONE (mutable) string is used to extract text from ANOTHER (constant) string... That is the problem with tmpIndex, tmpName and str.
Admin
function names are apologies?
Admin
Oh good point. I guess the real WTF is that they didn't use functional programming.
Admin
Inside the while loop it keeps working with the str variable instead of the tmpName variable, so on the second pass it reintroduces the first ' into tmpName, then on the third pass reintroduces the second one, on the fourth pass the first one again, etc ad infinitum
Admin
It's easy to overlook, because we tend to see what we want to see. I had to look a couple of times to see it, too, and I only did that because I knew (thanks to comments) that there was an infinite loop.
Inside the loop (so on all iterations other than the first), the code keeps scanning the original, unmodified string to see if it contains the target sequence. Since it scans the unmodified string, it will always find it.
When we see what appears to be an iterative scan-and-replace pattern, we assume that the author avoided this bug, either by starting each scan from after the previous hit, or by recursively rescanning the modified input. (The latter is obviously less efficient - O(n**2) in the worst case rather than O(n) - but elegant when implemented in functional style, which may make it preferable in some situations if performance isn't an issue.)
-- Michael Wojcik
Admin
InPvpAShadowPriestTheFacesWillMelt()
Admin
Why does the coder insist reproducing Javascript’s String.Replace() function?
Duh, you don't get LOC bonuses by using libraries! =)
Admin
As the code is written, it'll always return the input string. Since tmpIndex = -1 at the top, while(tmpIndex > -1) will never run. Of course, that's probably just a transcription error.
Admin
Arg, I'm an idiot. Or really tired. Or both (probably this one).
Admin
eh, tmpIndex is assigned a value IF it find the character in the string...
tmpIndex = str.indexOf("'");
Admin
I found something like this once (but with variables - that were set far in advance of the call - instead of literals). The only thing I can figure is that it used to do the replacements, and at some point, they became unnecessary. The subsequent coder didn't realize they were just undoing a preious replace, and the whole thing could have been eliminated, so they just tacked on additional calls to replace to "fix" the broken string.
Admin
Sorry didn't see your 2nd post :) guess I'm tired as well.
Admin
Just let those punks at Apache do all the hard work for you.
StringEscapeUtils
Admin
No idea, but you're right, it crops up a lot; or else we see my own personal favourite technique - date processing by means of toString() and string slicing. Yuk.
Oddly, shortly after I discovered automated unit testing, I tried it on this kind of isolated string processing function, and found that it is is ideally suited to rapidly and completely testing this kind of function. I guess you need to avoid unit testing and refactoring altogether in order to to make this kind of infinite-loop wtf.
Admin
And how do you call a java class from JavaScript? Ah, sure, just add a web service! That's so enterprisey...
Admin
Another WTF: tmpIndex will be the offset of ' in tmpName which will replace the wrong characters in str the second time through the loop since tmpName is 4 characters shorter than str. Depending upon where the two occurrences of ' are would either cause an infinite loop or return with some funky results.
Consider the string: 'Hi' Expected result: 'Hi' Actual result: '#39;
Admin
Yeah ... dont maintain any of my code thx. Complete noop innit....
Admin
Hey! Who are you to decide the halting problem? :-)
The second and all later indices are obtained from tmpName, but the substrings used to reconstruct tmpName are taken from str, which is never modified.
Thus, if str is 'foo', tmpName will alternate between 'foo' and 'foo', ad infinitum.
Even if this was fixed, the function would have quadratic worst-time performance, but hey, at least that means runtime is finite.
There are more gems in this function.
Admin
I just didn't expect it to find the replaced "'" character. Too much discipline from Perl regular expressions in my skull to realize how plain silly Javascript is.
Admin
Nonsense! What he was trying to do is converting the HTML entities for single and double quote to ordinary characters. What do ' and " in JavaScript have to do with ASCII?
Good question indeed.
No, it doesn't.
Just don't forget to XML escape your stuff before sending it to the web service. :-)
Admin
Wow, that's a weird piece of code.
I've done some stuff like this myself, but never to this extent. (And, in case anyone is wondering: the legitimate reason to do stuff like this is to make your Javascript run in really old browsers. In this case, it's pointless, because String.replace dates to Netscape 4 and IE 4; nobody's going to expect anything older than that to work anyway these days. If that was the goal, and this was not mere cluelessness, they still made an error: there is some browser version -- I forget which, but it might have been a version of IE for Mac -- you can't assume arguments are proper String objects to begin with and have to explicitly use "str = new String( str )" at the top of your code to make sure the browser knows what you mean.)
Admin
drStranglove()||doubleQuoteHowICameToStopWorringCommaAndLoveTheBombPeriodEndQuote()
Admin
since this is javascript, you'd only be DoS'ing yourself...