• ClaudeSuck.de (unregistered)

    And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.

    And BTW: Frist

  • anon (unregistered)

    Sounds like that was written by an instructor - perhaps the developer copied it verbatim from his old coursework examples?

  • (cs) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.

    And BTW: Frist

    I think the assumption being made here is that the programmer copied the code from some professor's example code without actually reading it.

  • Kluge Doctor (unregistered)

    I am adding this comment here to illustrate how a comment can be added.

  • ClaudeSuck.de (unregistered)

    TRWTF are the ads below the post. Voilà!

  • (cs)

    Well, you learn something useful every day...

    BTW:

    // In C# you can use single line comments using "//"
    // If you do that for more than one line, you've
    // got multi-line comments without using /* ... */
    // I just put this here in case you see it in other
    // modules...
    
  • ClaudeSuck.de (unregistered) in reply to Kluge Doctor
    Kluge Doctor:
    I am adding this comment here to illustrate how a comment can be added.

    ...though this is not recommended. It's just for demonstration purposes.

  • jethrotull (unregistered)

    On the Daily WTF You can add a comment containing the captcha text. Though this is not recomended. I put it here just for illustration.

    CAPTCHA: conventio

  • myddrin (unregistered)

    This reminds me of some code I worked on in the late nineties, it was written for the US gov't as part of a large contract and then the gov't sold it to a small company that I worked for in order for the company to commercialize the code.

    I kept coming across comments that said "From Chap X, exercise XX, code to blah blah blah." Not only that, all variable names were at most two letters (seemingly picked at random).

    As near as I could figure at least one of the coders on the project was learning how to code on the taxpayer's dime.

    That wasn't the only oddity, all the variable names were two letters, which were apparently selected at random. (Or perhaps the logic of their selection was beyond me....)

    When I asked my nominal superior on the project about that, I was told that "Long variable names make the code run slower or something. I don't know, that's what I was told."

    I really should write this up as a WTF, but its so long ago now that I think I've repressed quiet a bit of what actually happened.

  • (cs)
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        if (System.IO.File.Exists(file)) System.IO.File.Delete(file);

    is so much neater..

  • Eric Shinn (unregistered) in reply to A Nonny Mouse

    I shy away from that coding style; you can't set a breakpoint inside the if.

    Even if it's just

    if (param == null) return;

    I like it on two lines.

  • Hans (unregistered)

    if (UserSaidYes) printf ("hello!\n"); system ("format c:\"); // on one line

    (disclaimer: maybe it really works differently in C#, but I sort of doubt it...)

    It will be quite a surprise for the user who said "no", I can tell you...

  • Hans (unregistered) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    And so what? Where is the WTF? This code deletes a file if it exists. The comment, though, makes the former programmer a little professorish, but that's all.

    Apparently the real WTF here is that some of us don't care enough about good comments that they don't even see the WTF.

  • sino (unregistered)

    /* If you add comments on TheDailyWTF.com they might be visible to others. So I'm going to leave this here as an example */

  • (cs) in reply to myddrin
    myddrin:
    When I asked my nominal superior on the project about that, I was told that "Long variable names make the code run slower or something. I don't know, that's what I was told."
    No, no, no! They make it bigger. Trust a manager to get it wrong.
  • sino (unregistered) in reply to Eric Shinn
    Eric Shinn:
    you can't set a breakpoint inside the if. ...

    Learn your IDE because you can set a conditional break point to the same value the if statement is on. Did you know you can break at different parts of a for (or foreach) loop by putting your cursor on that section when you press F9 (or insert break point)

  • Kir Birger (unregistered)

    /*

    • You can make your multiline comments
    • Uniform by beginning each line with
    • an "*", performed by holding shift,
    • located next to your "Z" key, and
    • pressing "8" located above the alpha-
    • bet keys. */

    /* That code is also redundant.

    • File.Delete makes a kernel call
    • which automatically checks if the file
    • doesn't exist, so you only need the last
    • line */

    // End Comment

  • (cs)

    What's "not recommended" about it? It's clear, it's simple, and it doesn't waste display space.

  • (cs)

    Did you know? MasterPlanSoftware's ISP is Comcast.

  • (cs)

    And in the true spirit of teaching he demonstrated the concept using code that some future young coder would go looking for--wondering why files get deleted every time this application closed--thus drawing the student to the lesson...

  • Michi (unregistered)

    BTW, you don't even need to check if the file exists, as the doc states: "An exception is not thrown if the specified file does not exist."

  • sino (unregistered) in reply to Kir Birger
    Kir Birger:
    /* That code is also redundant. * File.Delete makes a kernel call * which automatically checks if the file * doesn't exist, so you only need the last * line */

    /*

    • That is unless you want
    • a FileNotFound exception
    • thrown. Which adds quite
    • a bit of extra processing
    • to handle the exception
    • which means nothing cause
    • you could have found it
    • before it even happened. */
  • Kir Birger (unregistered)

    http://www.leepoint.net/notes-java/flow/if/30if-braces.html

    It's a maintenance issue. It can make your code less readable if you have a large block, and it's more work if you need to add a second line to a conditional. That's all.

    Other possibilities are:

    1. if (someCondition) { [return] doSomething();}
    2. if (someCondition2) { return doSomething2();}

    these are most closely related to the convention for get-sets on properties.

    string MyProperty { get { return _myProp; } set { _myProp = value; } }

  • (cs)

    The WTF that everyone's missed so far is that the file exists check is useless. Nothing assures you that by the time the computer moves on to the delete command, the file will still be there, or that you can delete it.

    The right way.

    /*
    In C# if you only have one line of code in a 'try' 
    statement you still have to use the {}. I put this here
    just for illustration as the syntax error isn't enough to
    penetrate my thick cretaceous skull. 
    */
    
    try { System.IO.File.Delete(file); } catch { Messagebox.Show( "OH SHIT." ); }
    
  • Kir Birger (unregistered) in reply to sino

    After some more investigation, you're right. I was quick to assume that .net wouldn't just swallow one arbitrary exception. But here's another WTF for you.

    System.IO.File.Delete executes the DeleteFile function from Kernel and then specifically checks for Error Code 2 (File Not Found), and swallows that error. Any other error it throws. I have always assumed that it just does a check.

    sino:
    Kir Birger:
    /* That code is also redundant. * File.Delete makes a kernel call * which automatically checks if the file * doesn't exist, so you only need the last * line */

    /*

    • That is unless you want
    • a FileNotFound exception
    • thrown. Which adds quite
    • a bit of extra processing
    • to handle the exception
    • which means nothing cause
    • you could have found it
    • before it even happened. */
  • BestCondition (unregistered)

    I don't think the comment is the biggest WTF in that code. Think race condition.

  • Under Cover (unregistered)

    /* In C# you can use a large number of chained if statements. Or you can use the switch statement. I've just used the if statement to show that is possible in case you see it in other place. */

  • Graham Stewart (unregistered) in reply to Kir Birger
    Kir Birger:
    But here's another WTF for you.

    System.IO.File.Delete executes the DeleteFile function from Kernel and then specifically checks for Error Code 2 (File Not Found), and swallows that error.

    That seems perfectly reasonable to me. If the file can't be found then it is deleted - ergo Success.

  • BestCondition (unregistered) in reply to Sunstorm

    Actually, it's even harmful.

  • (cs)

    ' This comment is in case you are hungry ' mac donalds delivery phone 555-5555

  • sino (unregistered) in reply to Kir Birger

    Kir Birger: You might also want to run this:

    /* The @ sign before the string means
     * it is a literal string, this allows
     * C# programmers to not escape every
     * '\' with '\\' or run the risk of
     * having the string:
     * "I'm going /running away" show up
     * as "I'm going
     * unning away
     */
    File.Delete(@"C:\this\path\is\fake\so\try\to\delete\me.txt");

    which will error unless you check for the file, now you could make a System.IO.Path.GetDirectoryName() call then check if the directory exist, but that to much code to type... If the file doesn't exist then who cares if the path is valid :)

  • (cs)

    Seems to me that the WTF the OP was trying to point out, was the original coder's thoughts on not always needing braces. I get the impression that the OC thought that it was an "ooh, ahh, ohh! k3wl!" feature of C#, never seen before anywhere else, so bleeding-edge that future readers of the code would probably never have come across it before or even thought it might be within any realm of possibility, so he just HAD to "teach" them about it.

    Kinda like someone coming onto TDTWF and leaving us helpful hints about how we can leave comments without having to be quoting someone else's comments... not realizing that of course the "fr1st" commenter must always do that.

    The only case I can think of where such a comment might be justified, is Duff's Device. First time I saw it, I was surprised it was even legal syntax.

    (As for the race condition, yes that's bad coding, but not nearly so rare as to be a WTF. Sadly.)

  • (cs) in reply to Kir Birger
    Kir Birger:
    /* * You can make your multiline comments * Uniform by beginning each line with * an "*", performed by holding shift, * located next to your "Z" key, and * pressing "8" located above the alpha- * bet keys. */

    /* That code is also redundant.

    • File.Delete makes a kernel call
    • which automatically checks if the file
    • doesn't exist, so you only need the last
    • line */

    // End Comment

    <8 <My code isn't compiling sayng sintax error <on every line of comment i type! <i followed the instructions but i cant <display those freaky characters <on my Italian qwert keyboard <pls send me the codez! <8

  • wds (unregistered) in reply to Kir Birger
    Kir Birger:
    http://www.leepoint.net/notes-java/flow/if/30if-braces.html
    The points of that link seems to be that 1) if you don't use bracers you might put a ";" behind the if. Which is a moot point because in java, if you put a bracer behind that if(false); and close it properly underneath it the code still compiles without warnings, and you still have a bug, bracers or no bracers. (at least with java 1.5, it's a rule in C and C-like languages that you can always start a locally scoped block anywhere in the code, without needing control statements around it)

    And 2) if you don't use bracers you might add a line of code beneath it that's not within the if-block. Now I see myself as an intermediate programmer at best, but on all the projects I've worked on except the very first (in college) I can't say I've ever made that mistake, and for those very early ones I'm sure there's plenty of other bugs that were harder to find for me as a beginning programmer.

    So it's "not recommended" for programmers just starting out, but for the rest of us in the real world it can eliminate some of those extraneous bracers on simple checks, making your code more and not less readable. It's really a matter of preference. There's no maintenance issue here.

    BTW the extra half-second it takes you to add bracers when you add a line (assuming your IDE is not completely retarded) is exactly the half second you saved writing the line in the first time. End sum = 0.

  • swt (unregistered) in reply to tdittmar

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // In C# you can keep auto-generated, uninformative // class names like "Form1". I just left this // one here to toughen you up in case you see it // out in the cruel real world

  • Nerf Herder (unregistered) in reply to swt

    There are different ways to change text color. Note that you may see it either as [ color=red]Red Text[/color] or [ color=#FF0000]Red Text[/color]

    Resulting in Red Text Red Text

  • grg (unregistered)

    The code isn't even correct. The file could disappear between the test and the delete. Or it might be locked by someone else. Or the user might not have delete permissions. Or it might be on a CDROM. Or the network connection might go down. Joe code fer sure.

  • G (unregistered) in reply to sino
    sino:
    Kir Birger:
    /* That code is also redundant. * File.Delete makes a kernel call * which automatically checks if the file * doesn't exist, so you only need the last * line */

    /*

    • That is unless you want
    • a FileNotFound exception
    • thrown. Which adds quite
    • a bit of extra processing
    • to handle the exception
    • which means nothing cause
    • you could have found it
    • before it even happened. */
    TRWTF is that testing for file's existance does not qualify it for successful deletion - file permissions might still deny it. Thus, you should always try...catch the Delete method, and do no other "smart"/"sanity" checking or "optimization"

    Part of OO design is that you trust the method to do the requested thing or fail - you don't worry about the internals.

  • (cs) in reply to swt
    swt:
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // In C# you can keep auto-generated, uninformative // class names like "Form1". I just left this // one here to toughen you up in case you see it // out in the cruel real world
    // Also, you may come across variables that aren't // declared in the current method. I decided to not // write the fully qualified name of the member // variable 'file' to illustrate this.
  • Matthijs (unregistered)

    I have, unfortunately, worked with "programmers" that would indeed find this comment usefull.

    To the extend that they would probably use several lines of code in an if statement without brackets and then wonder why their code behaved abnormally.

    My boss often wonders why I have developped several twitches when reading trough some of their code :(

  • (cs) in reply to G
    G:
    TRWTF is that testing for file's existance does not qualify it for successful deletion - file permissions might still deny it. Thus, you should always try...catch the Delete method, and do no other "smart"/"sanity" checking or "optimization"
    TRWTF is that there are still people who think they're contributing anything by pointing this out after 7 other people already have.
  • Formentar (unregistered) in reply to wds
    wds:
    Kir Birger:
    http://www.leepoint.net/notes-java/flow/if/30if-braces.html
    The points of that link seems to be that 1) if you don't use bracers you might put a ";" behind the if. Which is a moot point because in java, if you put a bracer behind that if(false); and close it properly underneath it the code still compiles without warnings, and you still have a bug, bracers or no bracers.
    I prefer to give my code a good belting.
  • BlackTiger (unregistered) in reply to tdittmar

    BTW...

    //In C# you can use "using" directive to avoid typing //full namespaces everywhere in code

  • (cs) in reply to wds
    wds:
    And 2) if you don't use bracers you might add a line of code beneath it that's not within the if-block. Now I see myself as an intermediate programmer at best, but on all the projects I've worked on except the very first (in college) I can't say I've ever made that mistake, and for those very early ones I'm sure there's plenty of other bugs that were harder to find for me as a beginning programmer.

    So it's "not recommended" for programmers just starting out, but for the rest of us in the real world it can eliminate some of those extraneous bracers on simple checks, making your code more and not less readable. It's really a matter of preference. There's no maintenance issue here.

    LOL. I just did that yesterday while working with someone else's code, and I've been doing this for more than a couple years now. Of course, I caught it after the program didn't work right.

    Still, that wouldn't stop me from writing single-line if statements without brackets; it's a valid style.

  • Spinal Tapped (unregistered) in reply to Kluge Doctor

    I am adding this comment here to illustrate how a reply to a comment can be added.

  • Kir Birger (unregistered) in reply to wds

    @wds: it's also about maintainability as I said. it's more clear to someone else who needs to read your code and add a line. that person shouldn't have to add curly braces.

    @sino: quick bench tests on 10,000 trials have shown me that it's actually FASTER to fail 10,000 times to delete a file that doesn't exist, than to check 10,000 times that it does

    Are you saying that if I add a @ to my string that it will somehow cause the exception to be thrown versus swallowed? That would be a peculiar anomaly unless I'm missing something - am i?

    @all who say there's a race condition. True. Unfortunately I don't see a practical way of handling that because the OS determines context switches, so between any two operations you could lose "focus" to another thread that wants to change the file.

  • http://blog.thinkaloud.in (unregistered)

    Looks like he copied the usage of "if" from the help file... Funny how these guys get job as a programmer... http://blog.thinkaloud.in

  • (cs) in reply to DaveAronson
    DaveAronson:
    The only case I can think of where such a comment might be justified, is Duff's Device. First time I saw it, I was surprised it was even legal syntax.

    Heh, yeah, that's crazy coding. :) I've also seen it used to similar effect to fake threading...

    http://www.sics.se/~adam/pt/

    Marvellous. :)

  • kevin (unregistered)

    I think the real WTF is that you don't even need to check if it exists. File.Delete() doesn't throw an exception if the file doesn't exist, so the if statement isn't even required.

  • (cs)

    Well, we learn something every day. It had hitherto escaped my attention that "helpful" can, under the right circumstances, be a synonym for "pointless, condescending, and irritating."

    Can I be the first person on this site to claim to be a Semantics Nazi? I mean, being a Grammar Nazi is soooo ... sixty-three years ago. (Unless you live in Montana, of course.)

Leave a comment on “That's... Helpful”

Log In or post as a guest

Replying to comment #195641:

« Return to Article