- 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
I like the modifications this article that i can see on RSS ;-)
Admin
How many times did you click to submit this WTF? [:|]
Admin
[:|] What the....?
Admin
OK, let me see if I can follow this...I assume this is C++ and not C#/Java because of the pointer *randomFactor.
Case 1 and 2 (which should be enumerations, not magic numbers) both run if case 1 is encountered because there are no break; statements. And case 2 throws an exception based on...a random number? Seriously, why would you ever throw a send failure exception at random? Just to keep the user on their toes?
[Z] + [D] + [B] + [D] + [B] + [D] = This code.
Admin
It's a multiplication; not a pointer dereference...
The throw in case 1 would not allow the code to fall through to case 2.
Apparently, emoticons are still broken in Firefox?
Admin
If those were emoticons, they're broken in IE now too [:P]
Admin
If you use firefox to post a emoticon, it will not show up in IE. I have done this test. (I normally use FireFox.)[8-|]
Admin
Why would anyone reply to this by trying to explain how an enumeration works? The lunatic who wrote this obviously wouldn't understand. Sounds like everybody in that business has an urge to flip the smart ass bit and teach everybody how to code correctly.
Admin
I think the point of the WTF is that the code throws the same exception, regardless of the two cases.
It's like saying:
Admin
Well, that's part of it. The other part and I'd say the more insane part is that it randomly chooses whether to throw an exception in the case of two. To me that looks like deliberate malice but I don't understand the point of this code at all.
Admin
There are situations where most anything can make sense. In this case, figure a simulator. This could be a code fragment where case 1 means "network interface went down" and case 2 means "Whee, we simulate a faulty connection. Let's drop packets at random!" or something like that.
After all - those financial geek guys often determine some stuff's value through Monte Carlo simulations - basically trial and error. Let's throw the dice a couple million times, the average will be somewhere around right (we hope). This might be something just like that.
On the other hand, it may also be complete bogus.
Admin
WTF?
I see the genius who wrote this is a little weak on what break does, and where you need it.
Admin
The best part is that the exception in case 2 is only thrown if the random equation equals zero. If the randomFactor is say, 30, then the exception only happens once in like 60 times.
It's rather bad form overall, unless this is some sort of simulation attempting to test reliability over intermittant failures. Also, while the cases don't bleed through, its still a good idea to include the break statements. Which brings up another interesting question. If the exception is raised and handled, what happens to the former context? Is all that left on the stack somewhere, or can the garbage collector figure out how much of the stack is still in use?
Admin
Woah! The temptation to hit the "troll" button there was pretty great. Surely you've got to be kidding.
If not, think about what happens to the "former context" on the return from a function call.
As for the garbage collector cleaning up the stack, ha ha ha! That's a great one!
Admin
In case 2, probability of Math.random() returning 0 is going to be negligibly small. So for the exception to be thrown, randomFactor must be 0
WhyTF not just check randomFactor == 0 ???
Admin
Oops... Math.Round( ... ) :$
Admin
It's probably done by an intern?
Admin
Genius! The author will get support calls for 'intermittent errors', for which he can charge a high call-out fee to increase randomFactor a little.
'That should work fine now; let me know if it ever goes wrong again...'
Admin
I really don't see a WTF here at all. This seems clearly part of a test harness that was build into the code, and left in. During normal production, throwException would be set to 0, and nothing would happen. During development/debugging, it's set to 1 to always throw an error, or 2 to occasional throw an error. It's probably left over from when this module was stubbed and returned canned responses to unit test other modules.
Admin
Yeah, I thought the dupes were an attempt at satire.
Admin
Some of the comments today warrant more of a WTF than the original code snippet. Maybe one of these days you could have a WTF from the best WTF comments?[6]
Admin
I'm not sure you understand probablity very well. If randomFactor is 2, the probability of getting 0 is 50%. That's not negligible.
Admin
If randomFactor is 1, it still raises an exception half the time. That's because the random number gets rounded...
Admin
A note to those of you stating the break statement *should* be included following the throw, for "good form":
Some compilers will warn on the break following the throw. "Unreachable code detected". Worse yet, if you treat warnings as errors, your break following the throw will not compile. Therefore, IMHO, the break should be left off. Any programmer worth his salt ought to know that execution will not fall through to the next case after an unhandled throw.
Admin
Are we still talking about the same post?
1st, I don't know this Math class, but typically a random function returns 0 <= r < 1. I'll assume this to be the case (and ultimately it makes little difference).
2nd, if randomFactor is 0, then obviously the expression become zero. If 1, then the random no. is returned and has about a 50-50 chance of rounding to zero. Any higher ramdomFactor would decrease the odds that the result will round to zero.
So, 3rd, randomFactor of 2 gives probability of 25%.
4th, still not neglible.
OK, seems like this could probably be written better. But, hey, I'm just an ol'...
Admin
Yeah, I didn't notice the round, which is a little bit of the WTF in itself. So if randonFactor was 2, the exeception would be thrown 25% of the time.
Admin
This code appears to be Java (Math.round, Math.random) so putting a break after the throw would prevent the code from compiling with an unreachable code error.
Admin
Does random stupidity apply here?
Admin
I like the use of the word "occasional" used in one of the posts earlier, in regards to the fact that it will randomly deteremine whether or not to throw an exception.
Client: "So, does the application work?"
Contractor: "Ummm ... occasionally!"
Admin
Not only did Random Stupidity apply but they got the job!
Admin
not seeing a use case of this code or a bigger picture, i can only assume it's a prank. it gives me a good idea to write a virus payload routine: attach a thread and throw exceptions at random.
here's another theory: the unemployment rate for debuggers is too high, so the randomFactor kicks in to adjust the need for debugging. let's hope nobody ever discovers this, so the maintanence programmers can keep their jobs.
Admin
what's the time limit for editing your own post?
oh well...
just another thought: this code is ideal for my university's load balancing, where students are enforced to pick their courses on different scheduled dates since the db server is too weak to handle any reasonably large traffic. since senior students are given higher priority for course selection, let's put them in case2. first year students are definitly case 1 - they don't stand a chance
Admin
Are you sure you don't mean 33.3%?
You have the numbers 0, 1, and 2 to choose from.
Admin
Nevermind my last comment. You have twice as good of a chance of picking 1 than of picking 0 or 2.
Admin
Consider the ranges that will round to a particular number:
Thus the 25% since the numbers are equally distributed over the range 0.0 to 2.0.
Admin
Sigh...the forum gets me again
Forum: 4 Me: 0
Admin
Wow.. Just wow. That is messed.
Anyone have any idea why they created this monstrosity in the first place?
Admin
Or you can think of it this way, any result < 0.5 produces 0. The value goes from 0 to <2.0 This means 25% of the possible answers produce the 0 when rounded.
Admin
Absolutely! This test harness is great for testing continuous and intermittent interruptions. And I mean this seriously! No WTF at all [Y]
Admin
<font size="4">Actually, I know the person in question. This code is supposed to be a testcase, an emulator, so it's supposed to generate various kinds of errors. I don't know the details, but it looks like that is perfectly valid code.</font>
Admin
Ok, that was funny, isn't it?
But in real word this is a part of a simulator and this particular code simulates some bus errors.
I just want to mention, that there is a lot of code, that looks horrible when you show only a part of the code, whithout posting the needed explanations ;)
Admin
Sorry, I had to mark your post as a "troll", because you pointed out my mistake. I hate that.
Admin
There are plenty of languages where stack frames are garbage-collected because function calls can return more than once (search for "continuations" or "generators").
Admin
The code or this forum?