- 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
Admin
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.
Admin
Admin
There, fixed it for you.
Admin
There is a very easy way to handle the race condition:
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.
Admin
And their sum n17 is, of course, Cherry Coke.
Admin
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.
Admin
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.
Admin
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...
Admin
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
Admin
I didn't know you can do that!
Admin
Must be hiring alot of interns and attachment students at his workplace.
Admin
...and all those who don't believe me will be shot right away!
Admin
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?
Admin
In English you can write language understandable. Interesting, too.
Admin
Admin
plus he's wrong. The lines doesn't count but the amount of commands.
Admin
But they can poke you in the eye.
CAPTCHA: bene - I wonder if this so good.
Admin
/*
Admin
I once saw in a piece of Java code:
// test comment
Just to make sure that the commenting feature is working...
Admin
File.Delete() does not throw if the file doesn't exist. This rare single-line if is completely redundant.
Admin
Deleting a file in an event that can be canceled would also make for much fun.
Admin
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.
Admin
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...
Admin
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.
Admin
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 =)
Admin
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.
Admin
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.