- 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
File.WriteAllText(strPath, "Frist!");
Admin
Lol ok this is great, a true WTF
Admin
The fact that they've implemented a method called FileExists, when File.Exists(String) already exists, tells you it's going to be bad. It even returns false if the caller doesn't have permissions to read the file, just like this method.
I'm guessing what they want to know is whether or not there is a file they can overwrite there, or if they can add a file there if it doesn't exist, and make that a one-liner. But then you have File.OpenWrite which tells you that, except it tells you when you can't by means of exceptions.
Admin
JFC now that's a WTF. Big yikes.
Admin
I'm trying to imagine why
File.WriteAllText
itself was even included in the .net APIs.Admin
Should be called FileExisted.
Admin
Why not? They have many helper methods which are shortcuts for a series of boiler plate steps. It increases productivity.
Admin
No, it should've been called DoNotCallAndDeleteMe.
Admin
No, it should be named CallAndDeleteMe. And it should return a phone number, just so you know who to delete.
Admin
Classic tri-state Boolean, revisited.
True / False / File Destroyed
Admin
Oh, come on, it's not that WTF-y, it only returns true or false and not FILE_NOT_FOUND.
Admin
It even returns true if the file does not exist, because File.WriteAllText creates the file for you if it is not already there …
Admin
if (!FileExists(companyDBPath)) { ' File existed, sharing violation } else { ' File existed, past tense }
Admin
This expects that any file you give it is new and not containing anything important. That's the ONLY reason I can think of using this method, and even then, you'd be better off with a method that creates a new file instead and returns the name.
I would burn this piece of code or at least rename it and HEAVILY restrict it's invocation to ONLY safe cases.
Admin
File.Exists is broken though. Though someone who manages to find that isn't writing this horror.
Admin
Should be named EnsureFileExists.
Admin
.. and let's not forget that it can also succeed for any named thing that can be opened, like a device, or scanner, or printer or tty or /proc and who knows that else.
Admin
This method was written by the same guy who wrote the method "MemoryAvailable" which deletes everything off your hard drive and returns how much data it can hold.
Admin
Even more disappointing was the placeholder text, "blah", missing an opportunity to do something cute like "I love cats".
Admin
😂
Admin
Well, it would do if it wasn't C#. And if it had access to "/proc" or anything else that could be "opened." And if the File God Object was able to open something on a *nix system, sitting under "/proc" or wherever.
But none of that is true, so you've just managed to inject a whole new WTF on top of something that is already a really classy WTF of its own.
Admin
If this was Reddit, I would have actually just spent money to award you.
Admin
While the name is a true WTF, the purpose of this function seems pretty clear to me. It's intended to be used before you open a file for writing, to ensure that the file exists and is writable. I guess they didn't want to deal with exception handling, so this turns the exception into a boolean.
Admin
It should be called "CanOverwriteFile" -- then you're giving permission for what it tries to do.
Admin
OK, this is the funniest comment I've seen in a while!
Admin
Not that far fetched actually, there is Mono, you know?
Admin
This is not touch()! That makes sure the file exists; if it is there, update the read timestamp. If it is not there create an empty file. In no case does touch overwrite an existing file.
Admin
I am wondering if this was left as a time-bomb by a disgruntled employee just before he quit. Time to check the source control and see how many important files just say "blah".
Admin
Or .NET Core, which runs quite nicely on Linux (and has far better performance and reliability than Mono, at least from my experiences involving running ASP.NET applications).
Admin
if(FileExisted()) bool fuckThisCodebase = true;
Admin
It would still be wrong, with a subtle TOCTOU bug - it might find the file could be created, but then when you try creating the real file with the same name something has happened to prevent it and you're left with the file with junk in it instead of the proper contents.
Admin
Anyone remember the opening voice intro to Hexen? That's what I hear when I subvocalize this method's name. Like spell-casting by a Ring Wraith that got queer-eyed.
Admin
I can't tell you how much this one made me laugh today. :)
Admin
Not that far fetched actually, there is Mono, you know?
Not to mention .Net Core! (which is what all .Net will be as of 5.0)
Admin
At first look, I thought it's from my famous cow-orker Kevin. But it isn't - Kevin would add a
LastError = ex.Message;
or something like that in the Exception handler.