----- Original Message ----- 
From: "Andrey Hristov" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Thursday, July 29, 2004 8:00 PM
Subject: Re: [PHP-DEV] GOTO operator


--- snip ---

> Is the average Joe going to write parsers in the everydays work? Probably
not.

how about:

function pseudocode()
{
    $a=allocate_resource_z();
    $b=allocate_resource_y();

    $res=do_stuff();
    if(!$res)
        goto err_out;

    $c=allocate_resource_x();
    $res=do_more_stuff();
    if(!$res)
        goto err_out;

    $d=allocate_resource_foo();
    $res=do_even_more_stuff();
    if(!$res)
        goto err_out;

    return true;

err_out:
    free_resouce($a);
    free_resouce($b);
    if(isset($c)) free_resouce($c);
    if(isset($d)) free_resouce($c);

    return false;
}

here the do_stuff()s would be trivial inline code that makes no sense to
break out, each being a few slocs. everything looks neat and it is perfectly
clear what is going on.

with no goto:

function pseudocode()
{
    $a=allocate_resource_z();
    $b=allocate_resource_y();

    $res=do_stuff();
    if(!$res)
    {
        free_resource($a);
        free_resource($b);
        return false;
    }

    $c=allocate_resource_x();
    $res=do_more_stuff();
    if(!$res)
    {
        free_resouce($a);
        free_resource($b);
        free_resource($c);
        return false;
    }

    $d=allocate_resource_foo();
    $res=do_even_more_stuff();
    if(!$res)
    {
        free_resouce($a);
        free_resource($b);
        free_resource($c);
        free_resource($d);
        return false;
    }

    return true;
}

.. now imagine this isn't pseudocode, but actual code. you're doing a bunch
of stuff and the repetition becomes extremely unmanagable, especially if you
are doing miantenance later. ie, say you allocate another resource at the
beginning - add a free_resource() in one place only with the goto example,
in N places without.

before you discount the resource allocation stuff (in C this would be
primarily malloc()s), there is quite enough stuff that you can 'allocate' in
real code that isn't going to get gc'ed.

and yes, i've been missing goto dearly when doing real development. right
now, i'm emulating it by implementing allocation functions such that they
keep a list (or hashtable rather) of resources belonging to each function
and i can release them all in one line of code, but i'm still replicating
that line of code everywhere and, quite frankly, it's a kludge.

i agree with ilia - dont' castrate the language in a misguided effort to
protect the clueless. abuse of goto is best prevented by education on its
proper uses (few very specific cases).

paul

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to