Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:11727 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29958 invoked by uid 1010); 30 Jul 2004 20:26:46 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 29915 invoked from network); 30 Jul 2004 20:26:46 -0000 Received: from unknown (HELO procata.com) (65.19.190.98) by pb1.pair.com with SMTP; 30 Jul 2004 20:26:46 -0000 Received: from [65.111.195.203] ([65.111.195.203]) by procata.com for ; Fri, 30 Jul 2004 13:26:41 -0700 In-Reply-To: <410A5CBC.9090500@townnews.com> References: <20040729021337.73894.qmail@pb1.pair.com> <200407291117.09573.lpedretti@suserver.com> <200407291037.12396.ilia@prohost.org> <41098F91.3000606@hristov.com> <004801c475bd$812f3580$0200a8c0@rusko> <410A5CBC.9090500@townnews.com> Mime-Version: 1.0 (Apple Message framework v618) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Message-ID: Content-Transfer-Encoding: quoted-printable Cc: Paul G , internals@lists.php.net Date: Fri, 30 Jul 2004 16:27:25 -0400 To: Shaun Thomas X-Mailer: Apple Mail (2.618) Subject: Re: [PHP-DEV] GOTO operator From: jeff@procata.com (Jeff Moore) On Jul 30, 2004, at 10:35 AM, Shaun Thomas wrote: > Paul G wrote: > >> function pseudocode() >> { >> $a=3Dallocate_resource_z(); >> $b=3Dallocate_resource_y(); >> $res=3Ddo_stuff(); >> if(!$res) >> goto err_out; >> $c=3Dallocate_resource_x(); >> $res=3Ddo_more_stuff(); >> if(!$res) >> goto err_out; >> $d=3Dallocate_resource_foo(); >> $res=3Ddo_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; >> } > > While this is valid, it's not really a good example of pseudocode that=20= > requires a goto to be efficient. You could do the same thing with=20 > try/catch/throw: > > function pseudocode() > { > $a=3Dallocate_resource_z(); > $b=3Dallocate_resource_y(); > > try { > $res=3Ddo_stuff(); > if(!$res) > throw SOME_ERROR; > > $c=3Dallocate_resource_x(); > $res=3Ddo_more_stuff(); > if(!$res) > throw SOME_ERROR; > > $d=3Dallocate_resource_foo(); > $res=3Ddo_even_more_stuff(); > if(!$res) > throw SOME_ERROR; > > return true; > } > > catch SOME_ERROR { > free_resouce($a); > free_resouce($b); > if(isset($c)) free_resouce($c); > if(isset($d)) free_resouce($c); > > return false; > } > } I would prefer to write the original code as follows: function pseudocode() { try { $a =3D allocate_resource_z(); $b =3D allocate_resource_y(); $res =3D do_stuff(); $c =3D allocate_resource_x(); $res =3D do_more_stuff(); $d =3D allocate_resource_foo(); $res =3D do_even_more_stuff(); } catch ( Exception $e ) { free_resouce($a); free_resouce($b); if(isset($c)) free_resouce($c); if(isset($d)) free_resouce($c); =A0 =A0 =A0 =A0throw ($e); } } Where did the if statements go? do_stuff(), do_more_stuff(), and=20 do_even_more_stuff() should throw exceptions rather than return boolean=20= error indicators. (Following the principle of raising the error as=20 soon as it can be detected.) This implementation of pseudocode() also=20= re-throws the exception, rather than use a boolean error return value. =20= One major benefit of exceptions is to eliminate conditional statements=20= as error codes "bubble up." goto does not help with this.=20=