On Mon, Feb 6, 2012 at 11:34 PM, Tim Streater <tim@xxxxxxxxxxxxxxxx> wrote: > On 06 Feb 2012 at 20:51, Simon J Welsh <simon@xxxxxxxxxxx> 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 > Since SQLite is open source, you can atleast modify the source to make it not use try/catch but only use return vars. Still, I think you might be confused with something else. SQLite is written in pure C IIRC, so how would that use exceptions? Back to the GOTO discussion. I don't think there's anything wrong with GOTO at all, if used correctly. I have browsed/modified plenty of the ASM bootloader code of the Linux Kernel for example, and it's still pretty easy to understand (it does have lots of GOTOs). If there are only a few used, labeled correctly, and not too much crossing each other, there's nothing wrong. An exception based approach is quite a bit different, and I think that mostly leads to even worse programming. People tend to put quite some code inside a single big try block, and than try to handle every exception inside 1 except block, while there might be different cases to handle. Nested if-then-else statements can be fine, but having them nested too deeply makes the code chaotic. (I had to work with code from a different programmer, which included a single function having +- 2500 lines and nested 35 levels deep...) In the end: Use what fits for each function you write. It can depend on coding speed, execution speed, readability, portability and, of course, personal preferences. - Matijn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php