• ClaudeSuck.de (unregistered) 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. */
    Depends on the keyboard. I have it on above on the $-sign (AZERTY)
    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 */

    // End Comment

    ... and then you handle the error? Hmmm... I would call that Bad Programming Practice (BPP).

  • (cs) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    SenTree:
    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.

    Remember the old DOS days with GW-BASIC and other flavors of it. It was quite common that variables had only two characters because that was the maximum recognicable length for the interpreter. More characters were allowed but they were not significant for the distinction of variables. Hence AB1 was the same variable as AB2 since only AB was used by the interpreter to define the name of the variable. Thus, I suppose the superior probably still had the number 2 in mind but didn't remember why only 2 had to be used. You know, after years of programming and then several years after this can actually happen.

    That was the point of my comment - I was wondering how long it would take for someone to make the connection ;)

    I just recently rediscovered my copy of Best Of Dr. Dobbs Volume 1, with all those Tiny Basic articles, which was why it came to mind.

  • Rhialto (unregistered) in reply to Kir Birger
    Kir Birger:
    http://www.leepoint.net/notes-java/flow/if/30if-braces.html
    The real WTF is that none of these references mentions that there is always only one statement after the "if" anyway. The braces just group multiple statements into one. It is a fundamental difference to the syntax whether the braces "belong to" the if-statement, or to statements in general.
  • (cs) in reply to sino
    /*
     * 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)
     */
    

    There, fixed it for you.

  • (cs) in reply to Kir Birger
    Kir Birger:
    @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.

    There is a very easy way to handle the race condition:

    try {
       System.IO.File.Delete(file);
    } catch (Exception) {
      // Something wicked happened and we couldn't
      // delete the file.  We need to do something
      // about it.
    }
    

    When the OS tries to delete the file, if it cannot find it, there is no exception, and the operation is ignored -- which is pretty much the original intention.

    In this case, since there is no exception when the file does not exist, then there is no real race condition; it is just a waste of time to call the FileExists() function.

    If there were an exception thrown when the file is not found, (as is DirectoryNotFoundException, for when the path is invalid), then the programmer may want to catch that exception and either ignore it or do something about it.

    The point is that handling any errors raised by the function call avoids any race condition that could arise between checking the file's state and operating on it.

     -dZ.
    
  • (cs) in reply to Andrew
    Andrew:
    n4 is hydrogen peroxide. n12 is Tang.

    And their sum n17 is, of course, Cherry Coke.

     -dZ. 
    
  • (cs) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    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 */

    // End Comment

    ... and then you handle the error? Hmmm... I would call that Bad Programming Practice (BPP).

    Why would that be Bad Programming Practice? The key thing here to notice is that the file system is outside of your program's control -- so the state of any file is completely unpredictable at any particular time. This means that checking its state before operating on it will introduce a race condition. The alternative to operating on the file directly and handling errors is to somehow lock the file, which may be even more expensive than just handling an error.

    Also, keep in mind that the error we are talking about here is attempting to delete a non-existing file, which pressumably is inconsequential and trivial to this particular program, and therefore ignored.

    If, on the other hand, a missing file implies some strange integrity error in the application (why would it be trying to delete the file that was never there? did someone delete it by hand? did its creation fail?), then the correct way of handling it would be the check if the file exists and fail the operation if it didn't (perhaps raising an exception).

    But in this particular program snippet a missing file seems to just be a trivial thing, and is ignored altogether. In such a case, it would be more efficient to just not check for errors, catch exceptions (since FileDelete() smartly does not raise an exception when the file is not found), and not even check if the file exists.

    -dZ.
    
  • (cs) in reply to Jay
    Jay:
    I've seen plenty of comments in programs that explain the programming language rather than the program. Like, I used to work on a system that was filled with examples like:
    n17=n4+n12; // Add n4 to n12
    

    i.e. all the variable names were "n" followed by a number, and the comments were like the above.

    So, uh, yeah. If I was really struggling with that obscure feature of C that "+" means "add", this might be helpful.

    Comments like that are dangerous. If the you don't know what "=" does, you're liable to think the above is equivalent to

    n12 += n4; // C# uses semicolons because they take up less disk space than colons.

  • Hans (unregistered) in reply to ClaudeSuck.de
    ClaudeSuck.de:
    Hans:
    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.

    Danke Hans. If you need comments of this kind then I must doubt your programming capabilities. One of the rules for comments is that thou shalt not comment stuff which is obvious. It distracts and disturbs to have rubbish comments. Say why the piece of code is executed but don't give programming lessons in the code.

    Actually, you were saying the comment was alright and I was complaining that it was bad, but don't let that distract you from a good rant...

  • (cs)

    Even the comment is very funny ... (hope that such programmers never produce production code if not knowning programming basics) ...

    Form1_FormClosing() is also funny for me Form1!? Why not Form234234? Maybe it's just a sample for beginners...

    BTW: File.Delete() can be asynchron in some rare cases (but it's an operating system issue...) So you can't be sure after File.Delete() returns that the file is really deleted (wait sime ms ;-))))

    http://msdn.microsoft.com/en-us/library/kztecsys(VS.71).aspx

  • ClaudeSuck.de (unregistered) in reply to Spinal Tapped
    Spinal Tapped:
    I am adding this comment here to illustrate how a reply to a comment can be added.

    I didn't know you can do that!

  • (cs)

    Must be hiring alot of interns and attachment students at his workplace.

  • ClaudeSuck.de (unregistered) in reply to Andrew
    Andrew:
    The only WTF was the this programmer didn't get his "Enter" pinky amputated.

    Any programmer too lazy to put braces around a single line "then" clause should be fired on the spot. It will save numerous bugs down the road.

    ...and all those who don't believe me will be shot right away!

  • ClaudeSuck.de (unregistered) in reply to Andrew
    Andrew:
    Jay:
    I've seen plenty of comments in programs that explain the programming language rather than the program. Like, I used to work on a system that was filled with examples like:
    n17=n4+n12; // Add n4 to n12
    

    i.e. all the variable names were "n" followed by a number, and the comments were like the above.

    So, uh, yeah. If I was really struggling with that obscure feature of C that "+" means "add", this might be helpful. But what I really need to know is: What in the world do those variables contain?

    n4 is hydrogen peroxide. n12 is Tang.

    So the whole gives us: hydrogen peroxide tang. Hmmmm.

    N is Nitrogen, and N4 doesn't exist. Hence the formula is wrong. Using Sulphur it's different. S4 exists and I'm sure S12 also and why not S16? Just put the stuff in a bag and heat it. You won't even need a computer.

    CAPTCHA: odio - I hate??? But what?

  • ClaudeSuck.de (unregistered) in reply to ben
    ben:
    In assembly you can write code the controls your processor. I find this interesting.

    In English you can write language understandable. Interesting, too.

  • ClaudeSuck.de (unregistered) in reply to DZ-Jay
    DZ-Jay:
    ClaudeSuck.de:
    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 */

    // End Comment

    ... and then you handle the error? Hmmm... I would call that Bad Programming Practice (BPP).

    Why would that be Bad Programming Practice? The key thing here to notice is that the file system is outside of your program's control -- so the state of any file is completely unpredictable at any particular time. This means that checking its state before operating on it will introduce a race condition. The alternative to operating on the file directly and handling errors is to somehow lock the file, which may be even more expensive than just handling an error.

    Also, keep in mind that the error we are talking about here is attempting to delete a non-existing file, which pressumably is inconsequential and trivial to this particular program, and therefore ignored.

    If, on the other hand, a missing file implies some strange integrity error in the application (why would it be trying to delete the file that was never there? did someone delete it by hand? did its creation fail?), then the correct way of handling it would be the check if the file exists and fail the operation if it didn't (perhaps raising an exception).

    But in this particular program snippet a missing file seems to just be a trivial thing, and is ignored altogether. In such a case, it would be more efficient to just not check for errors, catch exceptions (since FileDelete() smartly does not raise an exception when the file is not found), and not even check if the file exists.

    -dZ.</div></BLOCKQUOTE>
    

    As has been mentionend before in several places: there can be more than just a non-existing file. The WTF seems to be more that they check if the file exists. In fact, when the file is locked the error is not handled directly in this procedure but eventually somewhere else.

  • in fact (unregistered) in reply to tdittmar

    plus he's wrong. The lines doesn't count but the amount of commands.

  • ClaudeSuck.de (unregistered) in reply to Vempele
    Vempele:
    Jay:
    I've seen plenty of comments in programs that explain the programming language rather than the program. Like, I used to work on a system that was filled with examples like:
    n17=n4+n12; // Add n4 to n12
    

    i.e. all the variable names were "n" followed by a number, and the comments were like the above.

    So, uh, yeah. If I was really struggling with that obscure feature of C that "+" means "add", this might be helpful.

    Comments like that are dangerous. If the you don't know what "=" does, you're liable to think the above is equivalent to

    n12 += n4; // C# uses semicolons because they take up less disk space than colons.

    But they can poke you in the eye.

    CAPTCHA: bene - I wonder if this so good.

  • fred (unregistered) in reply to Kir Birger

    /*

    • And as an added value,
    • you can now enjoy a race condition
    • in the code in case the file
    • is deleted between the two system call
    • (and of course, exists does not mean deletable) */
  • Parker Abercrombie (unregistered)

    I once saw in a piece of Java code:

    // test comment

    Just to make sure that the commenting feature is working...

  • ipsocurator (unregistered)

    File.Delete() does not throw if the file doesn't exist. This rare single-line if is completely redundant.

  • (cs)

    Deleting a file in an event that can be canceled would also make for much fun.

  • tbrown (unregistered) in reply to Hans

    Exactly, it's right up there with...

    // increment i ++i;

    or

    // Delete file if it exists if (File.Exists(file)) File.Delete(file);

    Perhaps worse since at least the above are trying to comment about what the program is actually doing rather than comment about the programming style.

  • Steve Commisso (unregistered) in reply to tdittmar

    Yeah I know this is an old post, but I'm just getting around to reading some of the archives I've missed the past few months.

    This post reminds me of some ASP.NET code I had to maintain on my first contract job. The developer used a static field somewhere and preceded its declaration with a ~10 line comment that started with something similar to: "You may not know what static means"...

    And before anyone asks, it was VB.NET and the guy was obviously a VB6 (or older) programmer (yeah, it was actually "shared", not "static").

    You can guess what the rest of the code looked like based on that comment. A bunch of web forms named "WebForm#.aspx", the odd "Copy of Webform#.aspx", each with cut and pasted code... Did I mention that one of these web forms had 49k lines -- most of it in a giant if/else block that executed most of the same logic in each branch?

    Luckily the application didn't have much real logic to it, and much of what was there was horribly broken, so I was able to get a go-ahead to just rewrite the entire thing...

  • Ex Corporate Programmer (unregistered)

    The real WTF is that checking for the existence of the file is unnecessary.

    "Deletes the specified file. An exception is not thrown if the specified file does not exist."

    There should be some exception checking, as this guy could throw any of about 8 different exceptions.

    Corporate programming shops are full of this sort of wankworthy code.. // Wank wank but the real problem is completely missed.

  • g man wiz (unregistered) in reply to tdittmar

    If you use // commenting, it is also easier to comment out larger parts of code by mearly sourouding it with /* ... /, but if you use / / for commenting, you can not souround those comments with more / ... */ because of how it is parsed, it will only comment out until it reaches the first */ then when u compile ur code, it will go apeshit =)

  • Meow (unregistered) in reply to tdittmar

    In C#, instead of writing C++++ you write:

    C ++ ++

    and you get C#.

    just wanted to let you know if anyone else tells you about it.

  • Steve (unregistered)

    Am I missing something? This is ooollllllllld news. You can do this in both regular C and C++ so it's not really any wonder you can do it in C#.

    I think everyone should have to learn C first so they learn how to actually program before using something like C#, which is great for accomplishing stuff but hides a lot of the workings from you.

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

Log In or post as a guest

Replying to comment #:

« Return to Article