• SomebodyElse (unregistered)

    You mean like this

    result = parts[parts.length -1];
    

    ~Tim

    Captcha: creative -- Not Very!

  • [email protected] (unregistered)

    string strPoster = "a/b/c/d/e/f/g/h/fist!"; string retval = ""; foreach( string s in strPoster.Split( "/" ) ) retval = s; Console.WriteLine( retval ); // CAPTCHA: craptastic

  • dave_v (unregistered) in reply to [email protected]
    string strPoster = "a/b/c/d/e/f/g/h/fist!"; string retval = ""; foreach( string s in strPoster.Split( "/" ) ) retval = s; Console.WriteLine( retval ); // CAPTCHA: craptastic

    You need to write shorter code to achieve that goal.

  • lorna (unregistered)

    Looks like the predecessor was bored at work (we've all been there at least once) and was entertaining him/herself by finding creative ways to do the mundane.

    captcha: java (Hey! I resent that, Alex!)

  • (cs)
    The .NET framework has a handy getFileName method
    Where? All I know is
    new System.IO.FileInfo(fullpathname).Name;
  • meh (unregistered)

    too bad that also / can be used as a path separator...

  • (cs) in reply to striker
    striker:
    The .NET framework has a handy getFileName method
    Where? All I know is
    new System.IO.FileInfo(fullpathname).Name;
    System.IO.Path.GetFilename(fullpathname)
  • Michael (unregistered)

    I've never used .Net, so can someone tell me what the @ in front of the string means?

  • wonko (unregistered) in reply to meh

    well, with .NET it is quite okay to trust on backslashes as path separator I guess...

  • D (unregistered)

    I don't get the WTF.

  • derobins (unregistered) in reply to Michael
    Michael:
    I've never used .Net, so can someone tell me what the @ in front of the string means?

    It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '' and 'n' and not a string containing a single newline.

  • Fabian (unregistered) in reply to wonko
    wonko:
    well, with .NET it is quite okay to trust on backslashes as path separator I guess...

    Not if you want to use mono in a linux environment.

  • leeg (unregistered) in reply to Michael
    Michael:
    I've never used .Net, so can someone tell me what the @ in front of the string means?

    It means someone was thinking of Cocoa when they wrote the framework,

  • (cs) in reply to D
    D:
    I don't get the WTF.
    To name two: - Reimplementing a system function - Iterating through an array and repeatedly overwriting a variable, instead of just assigning it once (see the first reply)
  • (cs) in reply to IceFreak2000
    IceFreak2000:
    System.IO.Path.GetFilename(fullpathname)
    Thank you.
  • joe (unregistered)

    Yeah the loop is a real WTF, but due to the mass of lib functions in .net, I am sure most of us reimplementet at least one of them.

    J.

  • (cs)

    Not to mention the use of Regex instead of just fullPathName.Split(Path.DirectorySeperatorChar), the use of ToString() on probably a string and ofcourse the overwriting of the same variable to keep the last one.

  • (cs) in reply to Michael
    Michael:
    I've never used .Net, so can someone tell me what the @ in front of the string means?
    To clarify, that's nothing to do with .NET. It's a feature of C# string literals.
  • Anonymous (unregistered) in reply to derobins
    derobins:
    Michael:
    I've never used .Net, so can someone tell me what the @ in front of the string means?

    It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '' and 'n' and not a string containing a single newline.

    Why does he use @"\" then, shouldn't that be either @"" or "\" to separate paths? Could be just a typo in the article though?

  • Jon Skeet (unregistered) in reply to Anonymous

    Nope, he wanted two backslashes - this is a regex, don't forget.

  • audiedog (unregistered)

    I found this in my first weeks at a new job:

    While (endCut <> True)
       endCut = True
       If InStr(fileName, "\") <> 0 Then
           endCut = False
           fileName= fileName.Substring(InStr(fileName, "\"))
       ElseIf InStr(fileName, "/") <> 0 Then
           endCut = False
           fileName=  fileName.Substring(InStr(fileName, "/"))
       End If
    End While
    

    Which, of course, can be replaced by:

    filename = IO.Path.GetFileName(filename)

    String processing version of this array wtf...

  • Inkstain (unregistered) in reply to Anonymous
    Anonymous:
    derobins:
    Michael:
    I've never used .Net, so can someone tell me what the @ in front of the string means?

    It means you don't interpret \ as escaping another character. @"\n" is interpreted as the string containing two characters '' and 'n' and not a string containing a single newline.

    Why does he use @"\" then, shouldn't that be either @"" or "\" to separate paths? Could be just a typo in the article though?

    Because the "" is an escape character in regular expressions, he was escaping the slash.

  • Old Geezer (unregistered)

    This isn't a WTF.

    In terms of Big-O, his solution is probably equivalent to the array construction routine.

    Modern API sets are big, so it may have been faster for him to regex and iterate in thirty seconds rather than research for ten minutes for an equivalent call, especially if it's in another package.

    Furthermore, you can readily tell what he's doing in the code. No obfuscation by stupidity.

  • (cs)

    It could be worse, they could have offshore developers actively creating and indexing into arrays for no reason.

    static final char charArray[] = new char[94];
    static {
    for (int i = 0; i < 94; i++) {
    charArray[i] = (char) (0x21 + i);
    }
    }

    // ...

    return charArray[random.nextInt(94)];

    versus the shorter but somehow inferior
    return (char) ( random.nextInt( 94 ) + 0x21 );
    I wish I was kidding.

  • snqow (unregistered)

    While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?

    captcha says it's bedtime. But I'm wide awake!

  • SpiritOfGrandeur (unregistered) in reply to snqow
    snqow:
    While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?

    captcha says it's bedtime. But I'm wide awake!

    Single quotes are for character literals, while double quote are for strings.

    CAPTCHA: initech (huh?)

  • lynn (unregistered)

    Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

    I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.

  • (cs) in reply to dave_v
    dave_v:
    string strPoster = "a/b/c/d/e/f/g/h/fist!"; string retval = ""; foreach( string s in strPoster.Split( "/" ) ) retval = s; Console.WriteLine( retval ); // CAPTCHA: craptastic

    You need to write shorter code to achieve that goal.

    LOL

  • pepethecow (unregistered)

    Relying on just the \ is a bug, depending on the source of the path string. Both / and \ are valid path delimiters in Windows. (Maybe not in Win9x) I believe all Windows functions will report paths using the , however.

  • AdT (unregistered) in reply to snqow
    snqow:
    While I understand the function of the @ for string literals, I find this syntax a bit of a clutter... Why not stick to single and double apostrophe convention used in many languages (php, ruby, and others)?

    Explanation #1: In Cish languages, that syntax is already used for character constants, and might thus lead to confusion. Explanation #2: That syntax wasn't invented in Redmond and is actively used by child-eating wild-eyed open source communists.

    Captcha: billgates (I swear!)

  • orangeyoda (unregistered)

    The way he's doing string splits is a bit odd.

    ignoring the System.IO.Path.GetFileName()

    string[] parts = fullPath.Split('');

    CAPTCHA : pizza ... yum

  • Prad (unregistered)

    There is nothing wrong in not knowing some nitty little function a FCL might have (because it is mostly language dependent) ... and I dont beleive in smiling at somebody's lack of language specific skills (unless ofcourse he's rated himself 10/10 on that particular language)

    The "WTF" factor really was this -- foreach (string part in parts) result=part.ToString();

  • (cs)

    Wow, this is familiar. I think I can do you one better though.

    //find which site we're using: 
    $site_path_tmp = getenv("DOCUMENT_ROOT");
    $site_path_arr = preg_split("/\//", $site_path_tmp);
    
    $site_path = "";
    foreach($site_path_arr as $var)
    {
        if(preg_match("/site/i", $var) || preg_match("/html/i", $var))
            continue;
       
        $site_path .= $var . "/";
    }
    
    $site_arr = preg_split("/\//", $site_path);
    $site = $site_arr[count($site_arr) - 2];
    1. Note that none of these uses of the regular expression engine are even necessary.
    2. In fact, you don't even need to use strpos for that inner if condition. You can just compare directly with == .
    3. Using "continue" is also totally unnecessary.
    4. Just so the readers knows, "count($site_arr) - 2" is always, always, always 2.
    5. In fact, the base path is always /var/www.

    Enjoy.

  • TickleMeElmo (unregistered)

    My favourite part is the final string.ToString() call. I can picture the developer pausing to reflect at the end of his job on whether there was any way to make it just a little worse.

  • Neko (unregistered)

    Nice to see he's making .NET even more cross-platform than it was before.

  • Neomojo (unregistered) in reply to lynn
    lynn:
    Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

    I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.

    Maybe it's just me (A few things are. Peanut butter, ham and ketchup sandwiches, for example.) but when I'm trying to do something in .Net (or PHP), my first thought is "has it already been done?"

    I agree that there are a lot of namespaces, objects and functions to browse through, but generally (as with the Path class) they're in intuitive places. If only it were true always, and not just generally.

  • spamparranoid (unregistered) in reply to [email protected]

    Has anybody ever found their own code on here before? Has anybody admitted to it?

  • Chris (unregistered) in reply to Old Geezer
    Old Geezer:
    This isn't a WTF.

    In terms of Big-O, his solution is probably equivalent to the array construction routine.

    LOOOOOOOOOOOOOOOOL ! Are you kidding or trolling ? Seems not, so i can not help but quick reply :o)

    Even though you he does not know the existence of the getFileName method, any young developer would know that it exists somewhere. You may spend 10 minutes finding it, but then you do not spend 100 times 1 minute to write this code plus 50 times 15 minutes to debug it !

    Even more, instead of looking for it, this guy creates a regex. Regex is not such a lightweight object, i don't know in .NET but in java the string would go throug a regex compiler, and anyway a regex interpreter is overkill if what you want is just find the last .

    So, even though you want to develop it by yourself, you can simply reverse scan the string looking for . Once you find it (or not then you stop at index 0), then you can extrapolate the filename.

    This WTF is the result of a massively tired and/or stupid developper. The former can easily be cured by some hours of sleep, i suspect curing the later can take substantially more effort during the course of years, for no guaranteed result...

    Your exercise for WTFing over a WTF (WTF^2 seems to be your IT level): you write the corresponding code 10 times ;o)

    Chris

    PS : Alex, i think you should open a new website namely www.thedailywtfwtf.com in order to take advantage of the numerous WTFs in the forums. Or at least have a special dedicated page, we could even easily vote for comment "promotion", just add a "WTF" button next to the "comment" and "quote" ones, i would loooooooooooooove it ! :o)

  • (cs)

    If you must use a Regex, why not @".*\" ?

    I don't know .NET but if they are like C,Perl,Java,vi,sed regular expressions than match everything up to and including the last backslash.

  • Franz Kafka (unregistered) in reply to lynn
    lynn:
    Most people probably don't know about the 'Path' class. I found it in a blog 1.5 years ago and started to use it. I makes life so much easier. I have been using .Net since late 2001.

    I think in all fairness most people learning .Net have so much to tackle (ASP.net/WebForms/ASMX/ADO.net and now WPF and WCF) that a gem like the Path class is just not on the radar.

    The first time I had to do Path processing, I went into the Java libs and dug around and found some stuff that helped, mainly because it's a royal pain. I don't recall whether I wrapped that stuff in a class or not, but why would anyone just go reimplement that?

  • Mike (unregistered) in reply to Chris
    Chris:

    So, even though you want to develop it by yourself, you can simply reverse scan the string looking for . Once you find it (or not then you stop at index 0), then you can extrapolate the filename.

    Or, just use fullPath.LastIndexOf("") to return, conveniently enough, the last index of the specified character.

  • Chris (unregistered) in reply to Mike
    Mike:
    Chris:

    So, even though you want to develop it by yourself, you can simply reverse scan the string looking for . Once you find it (or not then you stop at index 0), then you can extrapolate the filename.

    Or, just use fullPath.LastIndexOf("") to return, conveniently enough, the last index of the specified character.

    But the goal is to develop it by yourself, and not to use these library methods that you need to search for 5 minutes in the documentation ;o)

  • Lonely Programmer (unregistered) in reply to Chris

    Isn't Intellisense Wonderful? Not only can you see what methods and properties are available, you can get it to type it for you.

    And yes, I realize that there are people masochistic enough to write C# code in Emacs, but still!

    Ooooh - poprocks? Where's my Coke!?!?

  • (cs) in reply to Jon Skeet
    Jon Skeet:
    Nope, he wanted two backslashes - this is a regex, don't forget.

    Somebody a few posts up claimed that the @ in front of a string means "dont escape any characters in this string" -- completely wrong of course, but it spurred somebody else to ask why the string was @"\" if @ turns off escaping.

    My favorite WTFs are the ones written in these comments.

  • Crotchety Old Guy (on a SunOS 4.1.3 box) (unregistered)

    You spoiled kids these days with your fancy schmancy library calls.

    In my day, we walked 10 miles through the snow and then wrote something like this:

    char *ptr = fullPathName; while( (ptr++) != 0); while((ptr-1) != '/' && --ptr != fullpath ); result = new String( ptr) ;

  • another Steve (unregistered) in reply to Old Geezer

    Agreed - this is one of the least exciting WTFs I've seen. The code isn't laughable or dangerous - just inefficient and shows he doesn't know the libraries well. Slow news day?

  • (cs) in reply to Crotchety Old Guy (on a SunOS 4.1.3 box)
    Crotchety Old Guy (on a SunOS 4.1.3 box):
    You spoiled kids these days with your fancy schmancy library calls.

    In my day, we walked 10 miles through the snow and then wrote something like this:

    I would hate to think how many miles you would walk for a complex solution.

    Crotchety Old Guy (on a SunOS 4.1.3 box):

    char *ptr = fullPathName; while( (ptr++) != 0); while((ptr-1) != '/' && --ptr != fullpath ); result = new String( ptr) ;

    first

    while( (ptr++) != 0); while((ptr-1) != '/' && --ptr != fullpath );

    should be

    char *last = NULL; for(;*ptr;i++) if (*ptr == '/') last = ptr; ptr = last;

    Second the C library already gives you a the strrchr function for doing this:

    char *ptr = strrchr(fillPathName, '/'); if (ptr) result = new String(ptr);

    @orginal post: striker has the best sollution to the wtf.

  • Cowboy Curtis (unregistered) in reply to savar
    savar:
    Jon Skeet:
    Nope, he wanted two backslashes - this is a regex, don't forget.

    Somebody a few posts up claimed that the @ in front of a string means "dont escape any characters in this string" -- completely wrong of course, but it spurred somebody else to ask why the string was @"\" if @ turns off escaping.

    My favorite WTFs are the ones written in these comments.

    Which is exactly the question he answered. @ turns off escaping in a string literal, and the extra slash was to get past the escaping of the regex engine.

  • Artemus Harper (unregistered) in reply to TickleMeElmo

    Although it makes no sense in this case, I've seen it used as a lazy man's way of asserting that the string is not null.

    captcha: billgates (my "boss")

  • Javier Martín Domínguez (unregistered) in reply to [email protected]
    string strPoster = "a/b/c/d/e/f/g/h/fist!"; string retval = ""; foreach( string s in strPoster.Split( "/" ) ) retval = s; Console.WriteLine( retval );

    Now, that's good humour!

Leave a comment on “The Long Road to Clean Up”

Log In or post as a guest

Replying to comment #:

« Return to Article