Simply Stu Goat Empowers You

Argh

Monday 12th June

What is wrong with the following code? (Bear in mind that ‘create_media’ is a highly complex remote asynchronous task involving ejecting and cueing an audio/video port, creating space, starting video and audio codecs and sending network and serial messages between various devices as each status is reached.)

if (can_create_media(x))
{
   bool success;
   success = create_media(x);
   if (success)
      print "media created";
   else
      print "error: media creation failed";
}
else
{
   print "error: can't create media";
}

It looks quite reasonable doesn’t it. That’s until you check the contents of the can_create_media() function. It reads…

bool can_create_media(x)
{
   return create_media(x);
}

So that’s why there are multiple replies and race conditions and intermittent failure.

Written by stu

June 12th, 2006 at 2:08 pm

Posted in Coding, Curiosities

7 Responses to 'Argh'

Subscribe to comments with RSS

  1. Schoolboy error.

    Probably.

    rich

    12 Jun 06 at 3:34 pm

  2. One for The Daily WTF!

    Code Monkey

    12 Jun 06 at 4:58 pm

  3. Other functions it would be nice to have implemented with the same coding pattern:

    bool can_buy_simon_an_icecream()
    {
    return buy_simon_an_icecream();
    }

    Mmm… Undocumented side effects have never tasted so good!

    Simon's Tall

    12 Jun 06 at 5:09 pm

  4. I’d be renaming that create_media_and_tell_me_if_it_went_swimmingly

    Sam

    12 Jun 06 at 10:26 pm

  5. Heck, I can’t even get blogger to accept what the internet tells me is a closing tag after it (blogger) decides a tag isn’t closed - despite everything ending with /span between angled brackets as in all the other posts it “transposed” into html for me. But then I have been subject to “intermittent failure” all my life…

    qaminante

    12 Jun 06 at 10:27 pm

  6. Does this mean you can play Pong now?

    Kouros

    13 Jun 06 at 7:19 am

  7. haha, I can optimize THAT, sam :-P

    sweavo

    15 Jun 06 at 3:27 pm

Leave a Reply