- 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
Better:
bool is_witch(victim) { witch = acquire_lock(victim); Duck quacky = new Duck(); if (witch.getWeight() != quacky.getWeight()) return false; // not a witch; sorry return true; }
Admin
This brings to mind an old quandry of mine. Should a File.Delete method return a FileNotFound error, or just take the credit and return success if the file wasn't there in the first place. I have to admit I have created plenty of code that steals credit for work that is already done when the method is called. Am I creating WTF fodder?
Admin
enum {TRUE, FALSE, FILE_NOT_FOUND} - nuff said.
Admin
Actually, the real wtf is that they didn't just write:
Admin
Just checked, there isn't a single try catch in the TryParse method and the methods it calls
Admin
Well you would expect that after calling the Delete function that the file doesn't exist anymore, so I would just return true if it never existed anyway.
Admin
HAHA, Im ashamed to say that I did something very similar when I first started using c#... Only not quite as bad as that.
Some of that is still in production on at leat 2 big web sites that I worked on.
Captch: goddamnsonofabitchstolemymonkeypaddleandspankedmewithit
Admin
Let me get this straight, the developer knew about the File object, but didn't bother to check what the "Exists" method does? How sad, especially with visual studio, where the names of all the methods pop up as you type....
Free books, tutorials, and documents at http://www.BookGoldMine.com
Admin
One word: Bad.
Exceptions should be used for exceptional conditions. You really should test to see if the value is defined and then take the appropriate action. If the value cannot be parsed to a number that exception should be handled correctly since that's a configuration problem. I'd still vote for even logging that you are using the default value as either a debug or an info to make life easier for the administrators.
In any case, swallowing that exception is still a bad practice (especially since it's Exception and not something more specific). There are all sorts of problems that could occur with a supposedly set value (incorrect type, misspelling of the variable name, file not found, etc.).
Admin
Admin
Nope. That should be: witch test_witch(victim) { witch = acquire_lock(victim); submerge(witch); sleep(3600); if (!witch.is_dead()) witch.burn_at_stake(); return witch; }
Admin
In C# TryParse does not just catch and swallow an exception, try using Reflector on MSCORLIB and you can examine the code.
The main problem with using the Try/Catch technique rather than the TryParse method is catching exceptions is resource intensive and slow.
Try it, write a little test app and you'll find in a good-case scenario, TryParse and Try/Catch perform about the same. However, in the bad-case scenario, TryParse performs faster than the good-case, while Try/Catch is several orders of magnitude slower....
-Me
Admin
Three things are certain: Death, taxes, and lost data. Guess which has occurred.
Admin
Since your example looks for a system property, you could have done:
No catching needed.
Personally I'd go a step farther and put out a high-level log message (or maybe even throw a RuntimeException) for a badly formatted property. If someone went to the effort to set it, they will want to know why it's being ignored.
Admin
BOFH Programming... "You mean this file?"
Admin
does VB.Net still exists?
Admin
captcha: smile (VB.NET: smile for the camera. boom ok, that wasn't a camera)
Admin
Your comment reminds me of the DBAs at my first company. These people had such a narrow focus, that they only understood database theory and their database environment. They had no clue how to use and no desire to learn how to use, the operating system installed on their desktops. (I agree that DBAs don't need to know much about their desktop OS. However, some understanding of it can only help them. Even if it's only to detect/fix/avoid simple problems.)
I don't consider these types of people my peers. I don't even consider them to be true IT workers.
IMO, if your curiosity isn't insatiable, you don't belong in IT.
As you've mentioned, the problem is a narrow focus on one's paycheck.
captcha: pinball ... "ouch"
Admin
As others have already noted, this function will return true unless it is unable to create or overwrite the file. This would normally be a permissions issue, but it's possible some sort of intermittent problem could cause an exception to be thrown, and the function to return false. Fortunately, a steady diet of WTFs has taught me how to deal with this sort of thing:
Admin
Admin
Thing is, you keep forgetting to release_lock(). Going to crash and burn later along the line ;)
Admin
No quack
Admin
WTF?
I'm astounded by the stupidity of this comment. Are you seriously suggesting that if you learned to program in a different language it would seem reasonable to do this?
You're fired!
Admin
That kind of test function brings this to mind:
Admin
How many users of that app have rung Symantec saying a new virus is spreading havoc in their filesystem?
Admin
Admin
FUN! ;-)))
Admin
So maybe the original post isn't a WTF at all. Maybe the full algorithm was
Test if file Exists. If the file doesn't exist, try writing something to it. If we can't write to it, it did exist after all.
Alternatively, maybe Pinky did write the function.
Admin
That looks perfectly sensible to me. If the user doesn't have sufficient permissions to read the file, good security says that the user shouldn't even know whether the file exists or not. It's kinda like when someone enters an invalid username or password; good security says you don't tell them which one was wrong. Bad security says "You entered the wrong username. There's no user named johndoe, but there is a johndont. Try that one instead."
Admin
No, no, no. That's always bad. If there is no TryParse or equivalent available, I would sometimes rely on this method but you should never write catch(Exception e)! What about catch(FormatException e) or catch(ParseException e) or catch(OverflowException e)! Just because it's a narrow block of code doesn't mean that Something Bad(tm) can't happen in that block of code and get swallowed, meaning that weird errors start your program because you've run out of memory (for example) and you don't know about it.
Admin
Arrgh! I hate not being able to edit posts. Please ignore all speling[sic] errors in the previous post!
Admin
Admin
Ha ha. Test to destruction -- on a file! (Er, in retrospect, it doesn't really take that much to do it.)
Admin
Well, I guess the reasoning being that if you can't do squat with the file anyway then it might just as well not exist to you. I realize that this is not really adequate in all situations ... like when trying to create a file with the same name.
Admin
unfortunately it is not a joke... I am an assembler programmer, in fact i am the "antivirus" guy, i do reverse engineering for viruses, helping my colleagues to create antivirus programs in my company, and i found plenty of examples of such a code.. I have a big collection of such things.... sad but true...
"who is this general failure, and WTF is he doing in my computer?"...
Admin
You're probably thinking of ken: "We have persistent objects, they're called files". He was referring to the latest buzz-phrase of the time.
http://en.wikipedia.org/wiki/Ken_Thompson http://en.wikiquote.org/wiki/Kenneth_Thompson
-- Ralph.
Admin
Sadly, the joke is on you. I work for a large bank, and see this kind of "a comp sci intern wouldn't even code it" crap going to production on a daily basis...
The problem is, in my company (which I doubt is alone) - over 1/2 the developers have never had even 1 course in computer science. They're "philosophy" majors who learned how to program on their own (and other non-technical majors).
So, without learning the basics from the ground up, there's a lot of - "if it does what you expect it to do when you call in the function you currently call it from, it's good enough".
Who cares what you call it...or what it does...or... My head is hurting just thinking about this. I'm glad my interview went well yesterday. I'll be out of the madness soon...
Admin
Childhood memories? Boy Howdy do I feel old. I was already graduating college when narf/brain came out.......
Geez!
Still -- I like the idea, what if instead of 4 characters of gibberish, this had been real code inserted? :-)
Captcha: Old School!
Admin
Just for fun, here's the same thing in VBA, I alwasys aim at One-Line Code where possible.
But with no WTFs: boolCheckIt = IIf(Len(Dir("PathAndFileName")) > 0, True, False)
Quite neat huh.
Any one else know the same job in an other language with just One-Line Code.
Admin
That's my thought too. However, I have seen a lot of code in my time that threw an exception if a file didn't exist during a delete. Maybe it is just me, but useless error messages like "Unable to delete file. File Not found." are pretty frustrating.
Admin
ZORT!
so, if "narf" was not the result of the usual "obfuscation tactics", we know that said developer obviously was so busy taking over the world that there simply wasn't any time to look up the proper functions...
captcha: atari!!! well, I never... the reminescences do add up today, don't they...
Admin
Actually there are a number of uses for that sort of thing... it can often mean that another user/thread etc is still using a shared access file.
Admin
BRAIN: Pinky! Are you pondering what I'm pondering?
PINKY: Well, I think so, Brain, but wouldn't that just destroy the data in the file if it does exist?
Admin
Sorry, but I had to jump in because you guys did not really get the point of comparing str.Length instead of empty string.
If you ever trace on string comparison before, you will find that two empty string does not necessarily equal to each other. Though string are immutable, they can still have different instances. This is why empty string are should not be reference compares (== or .Equals) but with .Length <= 0. Summary, it's possible that "" != string.Empty
Admin
But since they're talking about .NET and in .NET the equality is not a pointer comparison but a real content comparison, they can be tested with ==. And naturally it's possible that in some environment String.Empty is not "" but in .NET it is.
Also to the File.Exists() talk, as someone already said, it's a security thing that the program isn't told if a file it has no permissions exists or not. Otherwise you could brute force find all files on the machine and even get some information from the names.
Admin
Admin
But Alex, if the monkey uses his tail for the mouse, how does he use the thumb buttons?
:)
Admin
lol dats f'ed