• Art (unregistered)

    I like the modifications this article that i can see on RSS ;-)

  • (cs)

    How many times did you click to submit this WTF? [:|]

  • (cs)

    [:|] What the....?

  • (cs)

    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.

  • (cs) in reply to pjabbott
    pjabbott:

    OK, let me see if I can follow this...I assume this is C++ and not C#/Java because of the pointer *randomFactor.

    It's a multiplication; not a pointer dereference...

    pjabbott:

    Case 1 and 2 (which should be enumerations, not magic numbers) both run if case 1 is encountered because there are no break; statements.

    The throw in case 1 would not allow the code to fall through to case 2.

    pjabbott:

    [Z] + [D] + [B] + [D] + [B] + [D] = This code.

    Apparently, emoticons are still broken in Firefox?

  • (cs) in reply to redtetrahedron

    If those were emoticons, they're broken in IE now too [:P]

  • (cs) in reply to rogueRPI

    rogueRPI:
    If those were emoticons, they're broken in IE now too [:P]

    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-|]

  • Myself As Someone (unregistered)

    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.

  • gabe (unregistered) in reply to Myself As Someone

    I think the point of the WTF is that the code throws the same exception, regardless of the two cases.

    It's like saying:

    switch( something ) {
    case 1: throw new FileNotFoundException();
    case 2: throw new FileNotFoundException();
    }

  • (cs) in reply to gabe

    Anonymous:
    I think the point of the WTF is that the code throws the same exception, regardless of the two cases.

    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.

  • Anonymous (unregistered) in reply to dubwai

    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.

  • (cs)

    WTF?

    I see the genius who wrote this is a little weak on what break does, and where you need it.



  • jldugger (unregistered)

    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?

  • (cs) in reply to jldugger
    Anonymous:
    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?


    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!
  • (cs)

    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 ???

  • (cs) in reply to snandy
    snandy:
    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 ???



    Oops... Math.Round( ... ) :$

  • (cs)

    It's probably done by an intern?

  • (cs)

    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...'

  • (cs)

    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.

  • (cs) in reply to rogueRPI

    Yeah, I thought the dupes were an attempt at satire.

  • sploing (unregistered) in reply to V.

    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]

  • (cs) in reply to snandy

    snandy:
    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

    I'm not sure you understand probablity very well.  If randomFactor is 2, the probability of getting 0 is 50%.  That's not negligible.

  • (cs) in reply to dubwai

    If randomFactor is 1, it still raises an exception half the time. That's because the random number gets rounded...

  • diaphanein (unregistered)

    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.

  • (cs) in reply to dubwai
    dubwai:

    snandy:
    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

    I'm not sure you understand probablity very well.  If randomFactor is 2, the probability of getting 0 is 50%.  That's not negligible.


    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'...

  • (cs) in reply to Katja Bergman

    Katja Bergman:
    If randomFactor is 1, it still raises an exception half the time. That's because the random number gets rounded...

    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.

  • (cs) in reply to diaphanein
    Anonymous:

    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.

    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.

  • anonymouse (unregistered) in reply to dubwai

    Does random stupidity apply here?

  • (cs) in reply to anonymouse

    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!"

     

  • no it was the other guy (unregistered) in reply to anonymouse

     

    Reply to:
    Does random stupidity apply here?

     

    Not only did Random Stupidity apply but they got the job!

  • (cs)

    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.

  • (cs) in reply to Kink

    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

  • (cs) in reply to sas
    sas:


    So, 3rd, randomFactor of 2 gives probability of 25%. 



    Are you sure you don't mean 33.3%?

    You have the numbers 0, 1, and 2 to choose from.
  • (cs) in reply to Scott

    Nevermind my last comment.  You have twice as good of a chance of picking 1 than of picking 0 or 2.

  • Daveh (unregistered) in reply to Scott
    Scott:
    Are you sure you don't mean 33.3%?

    You have the numbers 0, 1, and 2 to choose from.

    Consider the ranges that will round to a particular number:

      0.0 - 0.5: 0
      0.5 - 1.0: 1
      1.0 - 1.5: 1
      1.5 - 2.0: 2
    

    Thus the 25% since the numbers are equally distributed over the range 0.0 to 2.0.

  • Daveh (unregistered) in reply to Daveh

    Sigh...the forum gets me again

    Forum: 4 Me: 0

  • f00sh (unregistered) in reply to Daveh

     Wow.. Just wow. That is messed.

    Anyone have any idea why they created this monstrosity in the first place?

  • (cs) in reply to Scott

    Scott:
    Nevermind my last comment.  You have twice as good of a chance of picking 1 than of picking 0 or 2.

    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.

  • (cs) in reply to Anonymous
    Anonymous:

    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.

    Absolutely! This test harness is great for testing continuous and intermittent interruptions. And I mean this seriously! No WTF at all [Y]

  • Dimiter Georgiev (unregistered)

    <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>

  • Valentin Valchev (unregistered)

    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 ;)

  • (cs) in reply to diaphanein
    Anonymous:

    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.



    Sorry, I had to mark your post as a "troll", because you pointed out my mistake.  I hate that. 

  • (cs) in reply to cjs
    cjs:
    Anonymous:
    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?


    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!


    There are plenty of languages where stack frames are garbage-collected because function calls can return more than once (search for "continuations" or "generators").
  • Jetlag (unregistered) in reply to f00sh
    Anonymous:

    Anyone have any idea why they created this monstrosity in the first place?

    The code or this forum?

Leave a comment on “Just Try Clicking it Again”

Log In or post as a guest

Replying to comment #:

« Return to Article