Re: Goto (Was Re: Open Source)
- Posted by Pete Lomax <petelomax at blueyonder.?o.uk> Oct 19, 2007
- 779 views
c.k.lester wrote: > > In all my years of using Euphoria, I've never come across a situation where > I needed a goto. Happened to me once, when translating some old COBOL I had lying around... Otherwise I have no need for it. That said, I would accept goto with the following restrictions, ottomh: 1) goto is not permitted outside routines, much like the return statement. (bleats of "why not?" are quite likely to go unanswered) 2) All labels are local/private and all goto targets must be resolved by the <end routine> statement. There is no chance ever of jumping from one routine to another, by accident or design. 3) goto labels are simple fixed static literals, not variables/strings. I personally think <label>:: is best, to avoid conflict with namespaces. 4) Backward jumps are illegal while any forward jump is unresolved. When a label is defined, if any forward references to it are resolved then all prior labels are tagged as invalid. These measures should effectively prevent spaghetti code. You can have as many forward jumps to as many labels as you like, or the same for backward jumps, but once they start to overlap then all the problems begin. 5) The tail-resursive goto <this routine>(params) should be supported. (technical details permitting) 6) next j and redo j should be supported at any nesting level within a for j=<init> to <limit> loop. while loops have no such special keywords. For some reason I always find "continue" a bit vague and ambiguous. 7) Jumps into for loops are not permitted since the control var is not initialised and anything that deals with it up to and including the endfor is optimised to not have to deal with that case (trust me, you'd be miffed at the performance hit if it had to). 8) exit N and friends should not be supported as they thwart refactoring, as well as, I predict, being worse than goto to decipher. 9) The docs should say something like: goto can be used to escape from deeply nested clauses, in cases where you cannot see a better way to do it or you want to avoid setting a flag and testing it 3 or 4 times on the way out, or if you want a single block of error handling code (with multiple entry points) separate from the main algorithm. However if you find yourself adding more than one or two goto statements a day, there is probably something wrong with your coding style. To avoid excessive use, Eu prohibits overlapping forward and backward jumps (aka "spaghetti code"), also jumps into for loops are not permitted since <as above> While I certainly do not want to see the likes of goto "Z" & ias[idx], I would accept a (fixed literal) jump table as eg:
goto {Zint,Zatm,Zseq}[idx] Zint:: puts(1,"Z is an integer, well idx is 1 anyway\n") return Zatm:: puts(1,"Z is an atom, well idx is 2 anyway\n") return Zseq:: puts(1,"Z is a sequence, well idx is 3 anyway\n") return
That is, until someone comes up with something better (as opposed to something horribly inelegant and downright dangerous, doh). Again though, I have no personal need for any of this stuff. Regards, Pete