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.

convertSingleQuoteAndDoubleQuoteAsciiToCharacters is supposed to convert strings that have ' and " in them into ordinary strings containing single and double quotes. For instance, if you pass it "O'Malley", it will return "O'Malley". If you pass ")'Malley''s Irish Pub" it will … actually, never return.

 

function convertSingleQuoteAndDoubleQuoteAsciiToCharacters(str){
        var tmpName = "";
        var tmpIndex = -1;
        tmpIndex = str.indexOf("'");  // double quote

        while(tmpIndex > -1){
                tmpName = str.substring(0,tmpIndex);   
                tmpName += "'";
                tmpName += str.substring(tmpIndex+5);  
                tmpIndex = tmpName.indexOf("'");
        }
       
        if(tmpName.length < 1){
                tmpName = str;
        }

        var tmpName2 = "";
        tmpIndex = tmpName.indexOf("&#34;"); // single quote

        while(tmpIndex > -1){
                tmpName2 = tmpName.substring(0, tmpIndex);
                tmpName2 += "\"";
                tmpName2 += tmpName.substring(tmpIndex+5);     
                tmpIndex = tmpName2.indexOf("&#34;");
        }
       
        if(tmpName2.length < 1){
                tmpName2 = tmpName;
        }

        return tmpName2;
}

I’ve found that this snippet leaves me asking a lot of questions about the system. Why does the coder insist reproducing Javascript’s String.Replace() function? How did their data get “&#39;” in the first place? Why is this data cleansing being done at the UI-level? And what’s with the fondness of really long function names?

Actually, thinking about that further, it’s probably best we don’t know ...