Re: Replacing GOTO. [was Re: Conceptual problem solved by GOTO]
- Posted by Fernando Bauer <fmbauer at hotmail.c??> Jun 07, 2008
- 770 views
Chris Bensler wrote: > > Peter Robinson wrote: > > > > Shawn Pringle wrote: > > > > > > Fernando Bauer wrote: > > > > > > > > Suppose we have GOTO and this kind of code (which I think is common): > > > > }}} <eucode> > > > > procedure proc1() > > > > if cond1 then > > > > .. some code here .. > > > > if cond2 then > > > > .. some code here .. > > > > if cond3 then > > > > .. some code here .. > > > > goto OUT > > > > end if > > > > .. some code here .. > > > > end if > > > > .. some code here .. > > > > end if > > > > OUT: > > > > .. some code here .. > > > > end procedure > > > > </eucode> {{{ > > > > > > > > In Eu 3.1, a possible implementation would be: > > > > }}} <eucode> > > > > procedure proc1() > > > > for i=1 to 1 do > > > > if cond1 then > > > > .. some code here .. > > > > if cond2 then > > > > .. some code here .. > > > > if cond3 then > > > > .. some code here .. > > > > exit > > > > end if > > > > .. some code here .. > > > > end if > > > > .. some code here .. > > > > end if > > > > end for > > > > .. some code here .. > > > > end procedure > > > > </eucode> {{{ > > > > > > > > Now, without GOTO, we are forced to (mis)use a loop construct (for-loop, > while-loop)</font></i> > > > > where there is NO loop. > > > > Conceptually, IMHO this makes no sense. > > > > To avoid the conceptual problem we could use flags, but IMHO this is > > > > even > worst.</font></i> > > > > > > > > Regards, > > > > Fernando > > > > > > All non-goto code has been about making code flow as through a tree. Up a > > > trunk, > > > choosing major branches, to smaller branches (routines) and going into > > > twigs > > > > > > (loops, blocks) and then returning through the points until finally > > > getting > > > to > > > a leaf and exiting. This loop idiom to replace goto is something I have > > > forgotten but I did this once. What you see as an argument that goto can > > > make > > > > > > clearer code I see as an argument that there are times we want to > > > implement > > > this kind of control flow but we lack something in the language so we > > > either > > > > > > implement with goto or with a loop. > > > > > > You have avoided spegetti code but that does look like misuse. Goto is > > > also > > > useful for porting from other languages, and there are times > > > you wish to jump to another neighboring twig but do not want to put the > > > overhead > > > of a function call to do it. So, we need goto. Yet, the reason why we > > > have > > > loops and multi-line if/blocks is because it usually easier if we divide > > > the > > > kinds > > > of jumps we are doing into kinds. Is it a loop? Is it an if block? Is > > > it a loop with an integer counter (for)? > > > > > > I feel there is a "missing" kind of structure for putting things in > > > non-looping > > > block and being able to jump out of it as though it were a loop with exit. > > > > > > The > > > one iteration loop works but it isn't a loop semantically speaking. > > > > > > The keywords while, for, and multi-line if blocks exist because people > > > decided > > > that they would replace some usages with goto with control structures that > > > could > > > > > > be easier to read. These are all replacements of usages of goto. The > > > control > > > > > > structure for replaces a usage of goto or of while more directly. We all > > > agree > > > > > > these were good changes even if goto still has a place. I think Fernando > > > has > > > hit > > > on a usage of for which could further be put into a different kind of > > > control > > > > > > structure. > > > > > > So, I want you to think of a new keyword that the developers > > > might decide to put into 5.0 but we mustn't keep changing the target of > > > 4.0 > > > > > > or nobody will ever be able to finish it > > > (we will see Starcraft II in stores first, the cows will come home first). > > > > > > > > > Shawn Pringle > > > > I've been thinking along the same lines, except that I don' believe that all > > the patterns can be defined with the precision needed to fix on one, or even > > two, constructs. One of the problems is that the same concept might appear > > in > > many different forms. You might test the "condition" of sequence data with > > an > > 'if' block, or loop through its elements, or use a while 1 construct, or > > just > > a series of top-level code. The most you can say about the pattern is that:- > > > > - A group of constructs execute some testing of data > > - The data either fails early, in which case you want to fall through, but > > maybe > > not right to the end - maybe to an error point, > > - or it triggers a retry, and you want to go back, but quite likely not to > > pass > > through all the same hoops again. > > > > It struck me that part of the lack of structure of the goto comes from the > > single > > label approach, rather than defining a block of code. If you could label a > > block > > of code, it would have more meaning. Just say you chose the keyword 'during' > > with a label. Code like Fernando's would look more structured:- > > > > procedure proc1() > > -- preliminary code > > during data_filtering > > if cond1 then > > -- code here > > if cond2 then > > -- code modifying data > > retry data_filtering > > if cond3 then > > exit data_filtering > > end if > > -- code here > > end if > > -- code here > > end if > > end data_filtering > > -- clean_up code > > end procedure > > > > This really does nothing more than labelled jumps, but the labelling of the > > code as a meaningful block makes it more structured. > > > > Of course, someone will say you could just wrap the innards in another > > procedure > > and use return to exit, and something else for retry, but that doesn't > > detract > > from Fernando's and your point that the language lacks a construct to > > represent > > a common logical pattern. > > > > And like you, I won't be holding my breath for something like this to appear > > in the language, which is why I'm settling for the current proposal. > > > > Cheers > > Peter Robinson > > I think goto still has a place, but I would agree to a block construct such > as suggested. I would prefer block/end block to during though I think. > Aside from creating a place to break out from without abusing the intent of > other constructs, it would provide a way to create nested private scopes. > > In my opinion all the various types of code blocks should be able to be > targettable > break points, not just loops. A block construct would be a universal solution. > > Code is Alchemy I really appreciate all your comments and ideas, thanks! Maybe it's still possible to extend the use of labels to 'if-then-else' construct. I read somewhere this kind of syntax: for i = 1 to length(s) label "xyz" do For now, by analogy and for completeness, maybe we could have this: if ..cond.. label "xyz" then Regards, Fernando