Re: Good Use of GOTO
- Posted by Derek Parnell <ddparnell at b?gp?nd.com> Jun 06, 2008
- 840 views
Kat wrote: > I gave code for that also. It involved: > goto exit > goto retry > goto restart > goto next > goto cleanup&return > etc. > > The only way that's not clear is if you don't read english. For example: ----- fileA.e ---- function qwerty( integer b) integer a a = b label start: if a < 10 then a = foo(b) if a < 0 then goto exit -- Which exit does this go to? end if b = bar(a) goto start -- which start? end if label exit: label start: a = 5 while a < 10 do a = bar(b) if a < 0 then goto exit end if b = foo(a) foto start end if label exit: return a end function Oh hang on, all labels in the scope must be unique. So now I have to numbering? them. function qwerty( integer b) integer a a = b label start: if a < 10 then a = foo(b) if a < 0 then goto exit end if b = bar(a) goto start end if label exit: label start1: a = 5 if a < 10 do a = bar(b) if a < 0 then goto exit1 end if b = foo(a) goto start1 end if label exit1: return a end function So now not only do I have more to type and more to read I also have to create unique label names. Then we've got this sort of problem.... a = b label exit: if a < 10 then a = foo(b) if a < 0 then goto start end if b = bar(a) goto exit end if label start: Identical syntax from Euphoria's point of view, but very misleading for humans to read. Yes, I know is a silly example but the point is that it is possible to use label names that are actually misleading in terms of informing the reader about the coder's intentions. The solution we have chosen is this ... function qwerty( integer b) integer a a = b while a < 10 do a = foo(b) if a < 0 then exit end if b = bar(a) end while a = 5 while a < 10 do a = bar(b) if a < 0 then exit end if b = foo(a) end while return a end function There is no misleading control flow statements now. They are shorter. They inform you of the coder's intentions. And this is already in Euphoria 3.1 -- Derek Parnell Melbourne, Australia Skype name: derek.j.parnell