Re: GOTO operator

From: Date: Fri, 30 Jul 2004 09:25:30 +0000
Subject: Re: GOTO operator
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-11653@lists.php.net to get a copy of this message
Jan Dittmer wrote:
Andrey Hristov wrote:
Robert Cummings wrote:
Unmaintainable because of the ability to write cryptic code. There's nothing cryptic about:
    ...code
    goto cleanup:
    ...code
    cleanup:
do { .....code... if (something) break; ...code.... } while (0); ...cleanup code...
This works for one level, but imagine: ... code ... (which exits with goto_out) if (!alloc x) goto out; ... code ... (which exits with goto_outfreex) if (!alloc y) goto out_freex; ... code ... (which exits with goto_outfreey) if (!alloc z) goto out_freey; ... code ... free z; out_freey: free y; out_freex: free x; out: print bye; For this you'll need 3 nested do{}while(); loops and your indention is so far to the right, that you can barely write more than two more words on a line. This really looks a lot cleaner with goto.
I just woke up but I think this can be solved with one do..while and using bitfield for example. so when you break, just check the bitfield what to clean up. $clean = 0; do { $res = some_op(); if (!$res && $clean|=CLEAN_TYPE1) break; $res = some_op2(); if (!$res && $clean|=CLEAN_TYPE1) break; $res = some_op3(); if (!$res && $clean|=CLEAN_TYPE2) break; } while (0); if ($res & CLEAN_TYPE1) { //do clean type 1 } if ($clean & CLEAN_TYPE2) { // do clean type 2 } But I see that in your case simple variable and then having switch() after the do..while will be enough, just fall-through will be used. regards, andrey p.s. thanks for the nice example :) good exercise for the brain early in the morning :)

« previous php.internals (#11653) next »