Re: Long Live GOTO
| From: | Tim Streater | Date: | Mon, 06 Feb 2012 22:34:00 +0000 |
| Subject: | Re: Long Live GOTO | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-316504@lists.php.net to get a copy of this message | ||
On 06 Feb 2012 at 20:51, Simon J Welsh <simon@welsh.co.nz> wrote:
> On 7/02/2012, at 9:44 AM, Marco Behnke wrote:
>
>> Am 06.02.12 17:23, schrieb Alain Williams:
>>> However: a few GOTOs can make things clearer. Think of a function that
>>> can fail in several different places (eg data validation, ...). But it
>>> is reading a file which needs to be closed before the function
>>> returns. I have seen code where some $IsError variable is tested in
>>> many places to see if things should be done. That is just as bad as
>>> lots of GOTO -- often when having to write something like that I will
>>> have a GOTO (in
>>
>> Good code uses Exceptions and try catch for that kind of scenarios.
>
> Exceptions have a lot of overhead and should only be used in exceptional
> circumstances. I don't see how data validation failing is an exceptional
> circumstance.
>
> I find that using Exceptions and try/catch for something this trivial to be
> more confusing and harder to read (thus worse code) than a goto. It is also
> much easier to make a mistake, especially if you're expecting the catching to
> happen outside of the validation function.
While it is true that try/catch adds another level just like an extra if-then-else, there are times
when it's unavoidable. During initialisation of my app, I have to check which of the files in a
directory may be SQLite databases that belong to the app. So I have to check:
a) whether this file is an SQLite database
b) whether it has the two tables I expect to find there
Last time I checked the SQLite API in question, it looked as though try/catch was my only option.
--
Cheers -- Tim