Representative snippet


  • Considered Harmful

    A portion of our codebase was outsourced to an offshore company. Their classes tend to span several thousand SLOC, and here is a good example of the typical quality:

    char[] invalidcharacters = Configuration.Settings.InvalidItemNameChars;
    var processedName = name.Replace(",", "").Replace("'", "").Replace(".", " ").Replace("", "").Replace("(", "").Replace(")", "").Replace("®", "").Replace("&", "").Replace("!", "");
    processedName = Regex.Replace(processedName, "\\s", "spacespace");
    processedName = Regex.Replace(processedName, "\\W", "").Replace("spacespace", " ").Trim();
    processedName = string.Concat(processedName.Trim().Split(invalidcharacters)).Trim();
    

    I am refactoring as necessary (it's too much of a timesink to try to fix everything, I just fix what's actively causing problems). This algorithm I can't change because it would break compatibility with several other modules that use the same algorithm (copy-pasta'd).



  •  I'm halfway impressed they found 3 different methods to replace certain characters

     

    and it's interesting that they replace . with a space



  • @joe.edwards said:

    processedName = Regex.Replace(processedName, "\s", "spacespace");

    I found the WTF, it should be:

    processedName = Regex.Replace(processedName, "\\s", "spacespace",RegexOptions.IgnoreCase);


  • Considered Harmful

    "space invaders" -> "spacespacespaceinvaders" -> " spaceinvaders"



  • SPPAAAAAAAAAAAAAAACCCCCCCCCEEEEEE!!!!!!!


  • Considered Harmful

    @Buttembly Coder said:

    @joe.edwards said:
    processedName = Regex.Replace(processedName, "\s", "spacespace");

    I found the WTF, it should be:

    processedName = Regex.Replace(processedName, "\\s", "spacespace",RegexOptions.IgnoreCase);

    That should take care of those uppercase whitespace characters.


  • @joe.edwards said:

    processedName = Regex.Replace(processedName, "\\s", "spacespace"); ... Replace("spacespace", " ").Trim();
    I can't befuckinglieve it. How in the holy name of all that's based on Boolean algebra can someone think it's a good idea to replace a space by a word? And then replace that word by a space? Bloody hell.

     


  • Considered Harmful

    @TGV said:

    @joe.edwards said:

    processedName = Regex.Replace(processedName, "\s", "spacespace"); ... Replace("spacespace", " ").Trim();
    I can't befuckinglieve it. How in the holy name of all that's based on Boolean algebra can someone think it's a good idea to replace a space by a word? And then replace that word by a space? Bloody hell.

     


    Well, see, they wanted to replace all non-word characters (\W) and space was a non-word character, so the obvious solution was to use a character class pick a word no one was using and temporarily swap it in and out. Shame it doesn't round-trip when the content contains the word "space" (which is actually kind of likely given the context of where this is used).



  • @joe.edwards said:

    Well, see, they wanted to replace all non-word characters (\W) ...
    Sure, I got that. But the ineptitude! Not only multiple passes and trims and whatever, but using words to ... boggling.

     


Log in to reply