- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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
Admin
Sounds like that was written by an instructor - perhaps the developer copied it verbatim from his old coursework examples?
Admin
I think the assumption being made here is that the programmer copied the code from some professor's example code without actually reading it.
Admin
I am adding this comment here to illustrate how a comment can be added.
Admin
TRWTF are the ads below the post. Voilà!
Admin
Well, you learn something useful every day...
BTW:
Admin
...though this is not recommended. It's just for demonstration purposes.
Admin
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
Admin
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.
Admin
is so much neater..
Admin
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.
Admin
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...
Admin
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.
Admin
/* If you add comments on TheDailyWTF.com they might be visible to others. So I'm going to leave this here as an example */
Admin
Admin
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)
Admin
/*
/* That code is also redundant.
// End Comment
Admin
What's "not recommended" about it? It's clear, it's simple, and it doesn't waste display space.
Admin
Did you know? MasterPlanSoftware's ISP is Comcast.
Admin
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...
Admin
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."
Admin
/*
Admin
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:
these are most closely related to the convention for get-sets on properties.
string MyProperty { get { return _myProp; } set { _myProp = value; } }
Admin
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.
Admin
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.
Admin
I don't think the comment is the biggest WTF in that code. Think race condition.
Admin
/* 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. */
Admin
Admin
Actually, it's even harmful.
Admin
' This comment is in case you are hungry ' mac donalds delivery phone 555-5555
Admin
Kir Birger: You might also want to run this:
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 :)
Admin
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.)
Admin
<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
Admin
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.
Admin
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
Admin
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
Admin
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.
Admin
Part of OO design is that you trust the method to do the requested thing or fail - you don't worry about the internals.
Admin
Admin
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 :(
Admin
Admin
Admin
BTW...
//In C# you can use "using" directive to avoid typing //full namespaces everywhere in code
Admin
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.
Admin
I am adding this comment here to illustrate how a reply to a comment can be added.
Admin
@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.
Admin
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
Admin
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. :)
Admin
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.
Admin
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.)