Re: goto considered essential, revisited
- Posted by Jason Gade <jaygade at yahoo.com> Jan 22, 2006
- 459 views
Igor Kachan wrote: > > Tom Dailey wrote: > > > > > > This conversation seems to be wandering away from my original > > point. I am NOT advocating the addition of the goto statement > > to Euphoria. In my 38 years of experience as a systems > > programmer, I have seen much confusion caused by the unrestricted > > use of goto statements (or, before 1977, branch instructions), > > and I have spent literally thousands of hours untangling and > > rewriting the resulting spaghetti code. What I AM advocating is > > the addition of a very constrained control flow construct that > > will allow graceful coding of abnormal procedure returns, AND > > NOTHING ELSE. Please read the following code carefully. > > > > function copyfile > > ( > > sequence input_file_name > > sequence output_file_name > > ) > > integer input_file_nr > > integer output_file_nr > > object line > > > > input_file_nr = open ( input_file_name, "r" ) > > exception cant_open_input_file if input_file_nr = -1 > > output_file_nr = open ( output_file_name, "w" ) > > exception cant_open_output_file if output_file_nr = -1 > > while 1 do > > line = gets ( input_file_nr ) > > exit if atom ( line ) > > puts ( output_file_nr, line ) > > end while > > close ( input_file_nr ) > > close ( output_file_nr ) > > return outcome__success > > -- > > -- Exception handlers must follow the main body of > > -- the function or procedure. They are ONLY executed > > -- via an exception statement in the main body. > > -- > > when cant_open_input_file do > > return outcome__cant_open_input_file > > when cant_open_output_file do > > close ( input_file_nr ) > > return outcome__cant_open_output_file > > > > end procedure -- copyfile > > > > In considering this code, remember that, in a real world case, > > a function might detect 5 or 6 abnormal conditions, and, for > > each one, need to perform several cleanup actions before returing > > an appropriate error code. The proposed exception handling > > construct is merely a disciplined mechanism for removing such > > actions from the main (non-error) logic, with the intent of > > making that logic easier to read. > > > > Tom > > > Hello Tom, > > Try please: > > }}} <eucode> > > function copyfile > ( > sequence input_file_name > sequence output_file_name > ) > integer input_file_nr > integer output_file_nr > object line > > input_file_nr = open ( input_file_name, "r" ) > -- exception cant_open_input_file if input_file_nr = -1 > if input_file_nr = -1 then > return outcome__cant_open_input_file > end if > output_file_nr = open ( output_file_name, "w" ) > -- exception cant_open_output_file if output_file_nr = -1 > > if output_file_nr = -1 then > close(input_file_nr) > return outcome__cant_open_output_file > end if > > while 1 do > line = gets ( input_file_nr ) > exit if atom ( line ) > puts ( output_file_nr, line ) > end while > close ( input_file_nr ) > close ( output_file_nr ) > return outcome__success > -- -- > -- -- Exception handlers must follow the main body of > -- -- the function or procedure. They are ONLY executed > -- -- via an exception statement in the main body. > -- -- > -- when cant_open_input_file do > -- return outcome__cant_open_input_file > -- when cant_open_output_file do > -- close ( input_file_nr ) > -- return outcome__cant_open_output_file > > end function -- procedure -- copyfile > > </eucode> {{{ > > You can do it just now, program can *jump* from *any* point of a procedure > or a function body. Just put 'return' in a procedure, and 'return something' > in a function. > > Same thing with loops - just put 'exit' and program jumps to the next > operator after 'end for' or 'end while'. > > Maybe I just do not understand your problem here ... > <snip> Yeah, procedures and functions with multiple returns, along with if/else/elsif structures are the gotos of Euphoria. I like that quote "A single door to enter a room, but many windows to jump out." That's clever. With yield(), though, there'll be more than one door as well! -- "The author regrets that he is unable to reconcile himself to the thoughtful point of view you have expressed. However, it must be kept in mind that being raised in different cultures and different places can result in such differences of viewpoint between individuals. The author is from planet Earth." [author unknown] j.