Comment On convertSingleQuoteAndDoubleQuoteAsciiToCharacters

Today's code snippet was discovered by Dave Conrad inside of a large production web application in its fourth year of life. As you might imagine, projects of such size are rather difficult to sum up in a brief article, but I believe that today’s Javascript function is a wonderful representation of said system. [expand full text]
« PrevPage 1 | Page 2Next »

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:17 • by Mickey (unregistered)
Now that's just terrible.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:20 • by Dirk (unregistered)
oneGottaLoveReallyLongFunctionNamesBecauseTheyAreSoDescriptive()

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:22 • by TheRider
Shouldn't that method be named like this:

convertSingleQuoteAndDoubleQuoteUnicodeToAscii

?!?

(gotta do it right if at all)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:26 • by Joshua (unregistered)
You gotta love how the only two comments in the function are wrong too. &#39 != double quote.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:32 • by H|B
Form and function are completely mistaken in this one.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:33 • by freelancer (unregistered)
ThisIsTheNewFunctionToConvertSingleQuoteAsciiToCharactersActuallyNowItAlsoConvertsDoubleQuoteAsciiToCharacters

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:37 • by Looce (unregistered)
129278 in reply to 129273
Dirk:
oneGottaLoveReallyLongFunctionNamesBecauseTheyAreSoDescriptive()
Actually, the teachers I had in computer science all liked long function names; in at least one assignment, I made a really convoluted function name, like createLabelToPlaceAtTheTopOfTheUserInterface(), just to get them to tell me that I needed to remove some words in that.

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... ;)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:39 • by Will (unregistered)
This method should really be called convertFirstSingleQuoteAndDoubleQuoteAsciiToCharactersAndGoIntoAnInfiniteLoopIfThereAreMore.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:41 • by Kuli (unregistered)
129280 in reply to 129278
Looce:
convertSingleQuoteAndDoubleQuoteHTMLEntityToASCIICharacters() would be a more fitting name for the function... ;)

Or findMoreThanOneOfTheSameQuoteCharactersByNotReturningAnyMore().

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:41 • by C Gomez (unregistered)
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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:42 • by Cable (unregistered)
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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:53 • by dkf (unregistered)
I especially like:
tmpIndex = str.indexOf("'");  // double quote
and
tmpIndex = tmpName.indexOf("""); // single quote
There's nothing like a good old misleading comment to set you up for the rest of the code...

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 09:53 • by ok (unregistered)
I must admit it took me a while to notice said loop. :-\.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:02 • by kanna (unregistered)
129286 in reply to 129284
ok:
I must admit it took me a while to notice said loop. :-\.


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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:02 • by bstorer
129287 in reply to 129280
Kuli:
Looce:
convertSingleQuoteAndDoubleQuoteHTMLEntityToASCIICharacters() would be a more fitting name for the function... ;)

Or findMoreThanOneOfTheSameQuoteCharactersByNotReturningAnyMore().

Let's just cut to the chase and call it thisIsAStupidFunctionWhichShouldNeverBeUsedForAnyPurposeUnderAnyCircumstances(youHaveBeenWarned)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:07 • by Smitty (unregistered)
129289 in reply to 129281
C Gomez:
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.


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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:09 • by martin (unregistered)
129290 in reply to 129272
Mickey:
Now that's just terrible.


no kidding.

To the author of the original code:
Keep your hands in your pockets next time you see a computer, trust me.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:22 • by Michael Houghton (unregistered)
'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.


Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:23 • by anne (unregistered)
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?

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:27 • by morry
Yes, but it's Self Documenting©

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:29 • by H|B
129296 in reply to 129292
anne:
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?


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...

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:50 • by Zylon
All things considered, it's probably best that the guy who wrote this doesn't know about regexes.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:51 • by Eam (unregistered)
129298 in reply to 129281
C Gomez:
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.

For real, but you also gotta make sure you're documenting what the method accomplishes, not how it works. In this case, a better name might be "UnescapeQuotes"

Although a WTF by any other name...

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:54 • by Anonymous (unregistered)
It should be named

replaceCharacterEntities(string)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:55 • by Marcin (unregistered)
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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 10:55 • by Mystified (unregistered)
129301 in reply to 129274
TheRider:
Shouldn't that method be named like this:

convertSingleQuoteAndDoubleQuoteUnicodeToAscii


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?

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:00 • by DigitalGnome (unregistered)
129302 in reply to 129300
Marcin:
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.


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 &#39; strings converting one to a ' and having the other one reverted back to a &#39;

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:02 • by H|B
129303 in reply to 129300
Marcin:
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.


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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:03 • by dolo54 (unregistered)
function names are apologies?

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:05 • by Marcin (unregistered)
129305 in reply to 129303
H|B:
Marcin:
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.


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.


Oh good point. I guess the real WTF is that they didn't use functional programming.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:07 • by herminator
129307 in reply to 129300
Marcin:
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.


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 &#39; into tmpName, then on the third pass reintroduces the second one, on the fourth pass the first one again, etc ad infinitum

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:08 • by MichaelWojcik
129309 in reply to 129300
Marcin:
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.


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

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:18 • by that guy (unregistered)
InPvpAShadowPriestTheFacesWillMelt()

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:25 • by bob the dingo
Why does the coder insist reproducing Javascript’s String.Replace() function?

Duh, you don't get LOC bonuses by using libraries! =)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:31 • by benk (unregistered)
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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:32 • by benk (unregistered)
129314 in reply to 129313
Arg, I'm an idiot. Or really tired. Or both (probably this one).

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:38 • by JF (unregistered)
129315 in reply to 129313
benk:
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.


eh, tmpIndex is assigned a value IF it find the character in the string...

tmpIndex = str.indexOf("&#39;");

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:38 • by Been there done that don't care any more (unregistered)
129316 in reply to 129312
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.


String wtf = someString.replace("A","B").replace("C","D").replace("B","A").replace("D","C");

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 11:38 • by JF (unregistered)
129317 in reply to 129315
JF:
benk:
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.


eh, tmpIndex is assigned a value IF it find the character in the string...

tmpIndex = str.indexOf("&#39;");


Sorry didn't see your 2nd post :) guess I'm tired as well.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 12:12 • by foxyshadis
Just let those punks at Apache do all the hard work for you.

StringEscapeUtils

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 12:18 • by StrawberryFrog
129328 in reply to 129292
anne:
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?


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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 12:19 • by me (unregistered)
129329 in reply to 129326
foxyshadis:
Just let those punks at Apache do all the hard work for you.

StringEscapeUtils


And how do you call a java class from JavaScript? Ah, sure, just add a web service! That's so enterprisey...

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 12:25 • by Elvis (unregistered)
Another WTF:
tmpIndex will be the offset of &#39 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 &#39 are would either cause an infinite loop or return with some funky results.

Consider the string: &#39;Hi&#39;
Expected result: 'Hi'
Actual result: &#3'#39;

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 12:42 • by Anonymous Tart (unregistered)
129335 in reply to 129316
Been there done that don't care any more:
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.


String wtf = someString.replace("A","B").replace("C","D").replace("B","A").replace("D","C");


Yeah ... dont maintain any of my code thx. Complete noop innit....


>>> var a = "BBAA"; var b = "AABB";
>>> a.replace("A","B").replace("C","D").replace("B","A").replace("D","C")
"ABBA"
>>> b.replace("A","B").replace("C","D").replace("B","A").replace("D","C")
"AABB"


Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 13:01 • by AdT (unregistered)
Alex Papadimoulis:
If you pass ")&#39;Malley&#39;'s Irish Pub" it will … actually, never return.


Hey! Who are you to decide the halting problem? :-)

Marcin:
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.


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 &#39;foo&#39;, tmpName will alternate between 'foo&#39; and &#39;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.
- Pointless use of temporary variables tmpName and tmpName2
- Variables named tmpName and tmpName2 that do not necessarily hold a name
- The ifs don't do anything. Ever. The only reason why tmpName.length would be less than 1 is that str is empty. But then tmpName, which is empty, is overwritten with str, which is empty.
- str.replace would have worked fine
- Why write a function that can only replace &#34; and &#39; instead of all HTML rsp. XML entities?

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 13:12 • by Tom (unregistered)
129342 in reply to 129286
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.

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 13:21 • by AdT (unregistered)
129344 in reply to 129301
Mystified:
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.


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?

Mystified:
Why is Unicode so hard?


Good question indeed.

MichaelWojcik:
Since it scans the unmodified string, it will always find it.


No, it doesn't.

me:
And how do you call a java class from JavaScript? Ah, sure, just add a web service! That's so enterprisey...


Just don't forget to XML escape your stuff before sending it to the web service. :-)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 13:38 • by The Vicar
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.)

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 14:04 • by Todd (unregistered)
129363 in reply to 129273
drStranglove()||doubleQuoteHowICameToStopWorringCommaAndLoveTheBombPeriodEndQuote()

Re: convertSingleQuoteAndDoubleQuoteAsciiToCharacters

2007-03-29 14:08 • by h@x0r (unregistered)
129368 in reply to 129292
since this is javascript, you'd only be DoS'ing yourself...
« PrevPage 1 | Page 2Next »

Add Comment