tag:blogger.com,1999:blog-3224008808345429390.post2867255091654705613..comments2024-03-06T00:59:29.668-05:00Comments on Zaemis: Goto and ExceptionsTimothy Boronczykhttp://www.blogger.com/profile/00015151416507514182noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-3224008808345429390.post-59654398812409444912010-05-17T15:35:13.397-04:002010-05-17T15:35:13.397-04:00Tim,
Isn't it more elegant if you had used a ...Tim,<br /><br />Isn't it more elegant if you had used a "break" statement in your second to last example?<br /><br />If you had put a "break" statement in the "try" block, then you wouldn't need any "retry" variable to keep track.<br /><br />You could use a "for(;;)" or a "while(true)" loop, and insert a break if it succesfully opens a file.fnnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-62685923162330410542009-10-20T12:25:38.102-04:002009-10-20T12:25:38.102-04:00I read your first example and stopped reading, it ...I read your first example and stopped reading, it appeared as though you purposefully coded it poorly to show how goto's are better, write that first example without using multiple return statements, use if/elseif/else statements and an enum or equivalent for storing which error occured and you'll find its cleaner than the goto version and much easier to follow and not fragile at all. Some developers prefer multiple return statements over using if/elses, their code might LOOK arguably more AESTHETIC, but its not cleaner and its harder to maintain.Trevorhttp://devtrev.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-88630277589457263402009-08-23T03:00:46.253-04:002009-08-23T03:00:46.253-04:00Return statements are Goto statements. It's si...Return statements are Goto statements. It's simply another way of saying:<br /><br />goto exit_label;<br /><br />That was inherited from C language, unfortunately, since people didn't pay attention to the way Pascal handled functions. A function in pascal is like a variable that you could assign a value to. <br />function bar: integer;<br />begin<br /> bar := YourResultHere; <br />end;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-28846644009103799382009-06-12T10:18:41.955-04:002009-06-12T10:18:41.955-04:00Your very first example (the one that didn't u...Your very first example (the one that didn't use goto) was complete spaghetti code with return statements scattered all through it. I stopped reading at this point, it's hard to trust somebody on the usefulness of goto when their non goto containing code is a mess.<br /><br />I might come in the future and overlook your mistakes to see if you have a point somewhere in there, but right now I feel I would be only wasting my time by reading any further.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-18402488959405047422009-06-12T07:01:20.103-04:002009-06-12T07:01:20.103-04:00Goto can be handy and useful! i like its implement...Goto can be handy and useful! i like its implementations and how easy to use!<br /><br />but goto is unlike other methods where it got a specific structure that i can trace easily!<br /><br />so the main problem is that its going to effect the language and its developers in long terms!<br /><br />how? because it will effect the way programmer minds working in accomplishing their work when they use goto!<br /><br />so at the end, we may end up with programmers making their job done in messy way!<br /><br />which is of course Not desired by any client or customer who wants an application to be maintainable!<br />and this will make clients avoid language all together for the sake of maintainablity!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-23212243218809611672009-06-11T13:01:40.167-04:002009-06-11T13:01:40.167-04:00I much prefer a nice "goto label" over a...I much prefer a nice "goto label" over a "break 4" out of some nasty nested logic. It is hard to work backwards to see exactly where that break will end up, but with a label you can see it easily. As long as you can't goto labels outside of your scope and you can goto a spot inside a deeper construct, which is how I understand PHP goto is implemented, I don't see the issue at all. Just another handy tool for the pragmatic PHP programmer.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-88711146466539432672009-06-11T10:48:35.952-04:002009-06-11T10:48:35.952-04:00I'm sure there is a good reason to use GOTO so...I'm sure there is a good reason to use GOTO somewhere sometime in PHP code.<br /><br />If nothing else, if Sara needed it, then whatever she was doing is probably the best example.<br /><br />It may not fit well in a blog post, but there it is.<br /><br />I don't think these examples make a good use-case of goto, however. Sorry.Richard Lynchhttp://richardlynch.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-26548557088526285072009-03-26T12:55:00.000-04:002009-03-26T12:55:00.000-04:00Ultimately I agree with you, Federico, about the d...Ultimately I agree with you, Federico, about the danger of goto and that there is no reasonable application of it in high level languages. I disagree that somehow a discourse like this would confuse a programmer that's worth their weight. If this were a forum for a 100 level course in university, I'd agree. If we stop challenging even the most seemingly axiomatic of our assumptions, however, we'd never improve as an individuals nor as an industry.degzhaushttps://www.blogger.com/profile/05305172626395427542noreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-70231344279446413142009-03-25T11:59:00.000-04:002009-03-25T11:59:00.000-04:00Hi TimThis subject has been discussed in the PHP c...Hi Tim<BR/><BR/>This subject has been discussed in the PHP community for almost 5 years, and in the programming community for almost 30. And here we go again...<BR/><BR/>I believe this post will do more harm than good. In a way, you are encouraging new developers to use GOTO, by justifying the unjustifiable. Cleaner syntax? Limitations with the object model? Performance boost? No, no, no. Please don't. PHP programmers should NOT use GOTO. <BR/><BR/>It's not clear what limitations you see in the object model. The last example doesn't prove your point, it confuses people even more. While some people argue that GOTO is useful for error handling when writing procedural, I don't know any Java or PHP programmer that uses GOTO to overcome "limitations" in the exception handling model. Some of your examples are wrong, others can be refactored. <BR/><BR/>So what's the next excuse: "GOTO's can be very handy if you are writing a... parser?". If you are using GOTO in PHP, well, then you are probably doing something wrong. But if you are writing a parser in PHP, then you are doing everything wrong.<BR/><BR/>> CLI scripting with PHP is becoming more popular.<BR/><BR/>Is it? Last time I checked, all the sys admins are still using Perl and Python. And this trend has continued to grow in the last 10 years.<BR/><BR/>> Another drawback is that PHP requires the label to be within the same scope as the goto call.<BR/><BR/>You see this as a drawback? This is the only thing that can save your company from spending time and money developing a software that is unmaintainable.<BR/><BR/>> All too often a programmer will catch the exception and then simply terminate the program. <BR/><BR/>How often? I've never seen something like that in my entire life. You can't use that as an excuse, or to say that exceptions are easy to abuse. Everything is easy to abuse when it comes to programming languages. The only limit is your knowledge and experience (or your imagination).<BR/><BR/>Your last procedural example, the TRY_OPEN_FILE, is a clear example of why you shouldn't use goto as opposed to if/then and do/while. What you did there has a name, and I believe it's called: spaghetti code.<BR/><BR/>> But I wanted to do something different-- not the same old "goto is evil"<BR/><BR/>Well, maybe the reason why people, including award winning computer scientist, keep saying that GOTO is evil, believe it or not, it's because it really is :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-88124817942848435572009-03-24T15:19:00.000-04:002009-03-24T15:19:00.000-04:00As an example of catching exception the right way,...As an example of catching exception the right way, I work with Zend Framework where if an exception is thrown it is subsequently catched in the front controller that displays a nice error page (defined as a view in error controller), and with a little work even wrapping the error message with the common layout of pages.Giorgiohttps://www.blogger.com/profile/12689416577856305650noreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-7750591458088127282009-03-24T14:47:00.000-04:002009-03-24T14:47:00.000-04:00Well met, colleague. I guess one thing that I'm h...Well met, colleague. I guess one thing that I'm having a hard time understanding is how the implementation with goto (final code snippet) is *not* event driven. Obviously, retrying a section of code that failed with a given input with the same input will produce the same result, so invariably, the use case will be event driven (asking for user input) even if just via CLI.degzhaushttps://www.blogger.com/profile/05305172626395427542noreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-89264210585363058912009-03-24T14:30:00.000-04:002009-03-24T14:30:00.000-04:00I'm glad you appreciate the discourse! I was a bit...I'm glad you appreciate the discourse! <BR/><BR/>I was a bit nervous putting this out there honestly in fear of someone reading it wrong and then getting me blacklisted from all the cool PHP conferences and chat rooms because I wrote "favorably" about goto. But I wanted to do something different-- not the same old "goto is evil" mantra. Best practices are in place for a reason, but that doesn't mean we should blindly follow an idea and not at least think about the hows and whys.<BR/><BR/>Yes, the first point is using goto to fake exceptions would be good if PHP didn't already provide exception handling. There is more to exception handling than just jumping to a section of code, and the actual exception handling mechanism provided by a language should be used if one is available since they are "safer" and more flexible.<BR/><BR/>The second point is that goto can be used responsibly to augment the existing exception handling mechanism in PHP. You can kill stop an event if there was an error when you're running in an event-driven enironment without terminating the entire application. But PHP is procedural, and too many people just catch the error and kill the entire script. That's the equivalent of you're web browser closing itself every time you run across a 404.<BR/><BR/>Of course retrying a section of code that threw an exception can be accomplished without using goto, but there's an interplay between the outter loop and setting the loops sentinel value from within the catch block I don't like. My sample is just a short code sample, but over a large section of code with a lot of catch statements its too easy to lose track of the sentinel.<BR/><BR/>If you can provide a cleaner alternative to retrying a block without the brittleness of a do/while loop or the impurity of goto, I'd be happy to ponder it.Timothy Boronczykhttps://www.blogger.com/profile/00015151416507514182noreply@blogger.comtag:blogger.com,1999:blog-3224008808345429390.post-38365649918711164582009-03-24T13:40:00.000-04:002009-03-24T13:40:00.000-04:00Very cool Tim! I always appreciate your ability t...Very cool Tim! I always appreciate your ability to think through concepts and best practices yourself and not take tips from anybody (even Dijkstra, though :-p ?) until thoroughly challenged and discussed.<BR/><BR/>That said...: It seems you're making two points, the first of which even you concede is not best practice (swapping exceptions with goto implementation). The second argument, however, is intriguing. <BR/><BR/>If scoped appropriately, the code in the final snippet is sexy. It's readable, clean and concise. My only question would be: can the same logic flow be implemented without the use of goto. I'd argue yes and a good example would be your second to last snippet. I think even that snippet could be optimized but I'd argue that even if it looks uglier than the final it's ultimately better than using goto.<BR/><BR/>In closing: Dijkstra's smarter than you :-p<BR/><BR/>Keep challenging! This is good discourse!!degzhaushttps://www.blogger.com/profile/05305172626395427542noreply@blogger.com