The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Inherited code

Last post 11-20-2012 4:36 PM by TheRider. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 11-19-2012 7:57 PM

    Inherited code

    Found this in a project I have inherited at work:

                 if (pLine.Length > 4)
                    if (pLine.Substring(0, 4) == ("and "))
                        pLine = pLine.Substring(0, 4).Replace("and ", "").Trim() + pLine.Substring(4);

                if (pLine.Length > 2)
                {
                    while (pLine.IndexOf("&") == 0)
                    {
                        pLine = pLine.Substring(0, 1).Replace("&", "") + pLine.Substring(1);
                        pLine = pLine.Trim();
                    }
                }

    You can guess my password.
  • 11-19-2012 10:24 PM In reply to

    • Ben L.
    • Top 25 Contributor
    • Joined on 12-21-2010
    • HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
    • Posts 1,411

    Re: Inherited code

    I... words...
    Filed under:
  • 11-20-2012 12:39 AM In reply to

    Re: Inherited code

     So if I understand correctly, the thought process leading to this code went something like:

    "Hmmm, how can I make sure, that whatever 'and ' is in the beginning of this string gets moved to the end without being changed to another 'and '?"

     

    ...do I? Because... well... really??

    This is not the signature you are looking for
  • 11-20-2012 1:41 AM In reply to

    Re: Inherited code

    SEMI-HYBRID code:
    So if I understand correctly, [...]
    You don't.

     

  • 11-20-2012 1:45 AM In reply to

    Re: Inherited code

    You understood wrongly, the code removes the following strings from the beginning of a string: "and ", "& ". Oh, and it also removes white space at the ends afterwards. It's likely list formating cleanup code. I don't see any real issue with the code.
  • 11-20-2012 3:54 AM In reply to

    Re: Inherited code

    If only there were some kind of method that tells you whether one string StartsWith another string... oh, wait.

    Given the apparent "brillancy" of the original author of the code, even if the existence of the StartsWith method were pointed out to him, that would probably only have made it worse, since he seems unable to grasp even the most basic concepts of string manipulation (and possibly coding in general - people like that should be kept away from code by all means).

    Probably something like if (pLine.Substring(0, 4).StartsWith("and ")) ... and going downhill from there.

  • 11-20-2012 8:16 AM In reply to

    • TGV
    • Top 75 Contributor
    • Joined on 10-09-2005
    • Posts 552

    Re: Inherited code

    I think some people failed to grasp the beauty of this line:

     pLine = pLine.Substring(0, 1).Replace("&", "") + pLine.Substring(1);

    You know the line starts with "&", then you take the substring (0, 1), which gives "&", replace the ampersand by the empty string, and then concatenate the rest of the string. Which is of course equivalent to

    pLine = pLine.Substring(1);

    The same happens in the first replacement. Plus that the tests for length are unnecessary, and that he might also have just used a regexp to remove all & at the start of the line.

    It's like a little cluster of evidence that we're looking at the work from someone who doesn't quite understand how procedural programming works.

  • 11-20-2012 10:38 AM In reply to

    Re: Inherited code

    TGV:
    Plus that the tests for length are unnecessary, and that he might also have just used a regexp to remove all & at the start of the line.
    Not quite. If the original string is "and ", then it is kept intact. Then, after removing the ANDs, if it starts with a "&" but is no more than 2 characters, it is also kept as is. So the tests for length are not redundant. On the other hand, they are quite likely to be buggy.
  • 11-20-2012 11:37 AM In reply to

    • Zecc
    • Top 25 Contributor
    • Joined on 06-12-2007
    • and hasn't left since.
    • Posts 1,672

    Re: Inherited code

    henke37:
    I don't see any real issue with the code.
    !!

    If mixed metaphors were illegal, I'd be having an indigestion.
    typeof NaN == 'number'
    var ò_ó, ಠ⁔ಠ, ᄒᆺᄒ, ᅙᅳᅙ, ᖛᨓᖜ, ꖴᅩꖴ, ఠᨋఠ; // Naming your variables is serious business
  • 11-20-2012 4:36 PM In reply to

    • TheRider
    • Top 150 Contributor
    • Joined on 03-01-2005
    • Zurich, Switzerland
    • Posts 351

    Re: Inherited code

    TGV:

    I think some people failed to grasp the beauty of this line:

     pLine = pLine.Substring(0, 1).Replace("&", "") + pLine.Substring(1);

    You know the line starts with "&", then you take the substring (0, 1), which gives "&", replace the ampersand by the empty string, and then concatenate the rest of the string. Which is of course equivalent to

    pLine = pLine.Substring(1);

    The same happens in the first replacement. Plus that the tests for length are unnecessary, and that he might also have just used a regexp to remove all & at the start of the line.

    It's like a little cluster of evidence that we're looking at the work from someone who doesn't quite understand how procedural programming works.

    I wouldn't think that he understands better how object oriented programming works. Nor functional programming. Nor any kind of programming for that matter...
    CodeNinja

    I'm starting to think our management is really a bunch of cats and someone standing nearby has a freaking laser pointer.
Page 1 of 1 (10 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems