1. RE: goto considered essential
- Posted by Lynn Kilroy <leks_transportation at hotmail.com> Jan 21, 2006
- 464 views
- Last edited Jan 22, 2006
Wasn't there an exit thingabobbers I saw mentioned somewhere? I thought it would be neat to use, because it doesn't exist in QBasic, and I had to setup escape sequences that would abort my loops by resetting variables and stuff. Really messy and unclear. Exit looked like a great alternative. Love & Friendship & Blessed Be! Lynn Erika Kilroy Vincent wrote: > > > posted by: Vincent <darkvincentdude at yahoo.com> > > Tom Dailey wrote: > > > > > > Consider the following program fragment, which copies one text file into > > another text file: > > > > sequence cmd_args > > sequence cmd_line_words > > sequence input_file_name > > integer input_file_nr > > object line > > sequence output_file_name > > integer output_file_nr > > > > cmd_line_words = command_line ( ) > > if length ( cmd_line_words ) != 4 then > > Printf ( "Exactly two arguments expected -- %d supplied.\n", > > { length ( cmd_line_words ) - 2 } ) > > else > > cmd_args = cmd_line_words [ 3..4 ] > > input_file_name = cmd_args [ 1 ] > > input_file_nr = open ( input_file_name, "r" ) > > if input_file_nr = -1 then > > Printf ( "Can't open input file %s.\n", > > { input_file_name } ) > > else > > output_file_name = cmd_args [ 2 ] > > output_file_nr = open ( output_file_name, "w" ) > > if output_file_nr = -1 then > > Printf ( "Can't open output file %s.\n", > > { output_file_name } ) > > else > > while 1 do > > line = gets ( input_file_nr ) > > if atom ( line ) then > > exit > > end if > > puts ( output_file_nr, line ) > > end while > > Printf ( "%s copied to %s.\n", > > { input_file_name, output_file_name } ) > > end if > > end if > > end if > > > > Without a goto statement, or something like it, we have the creeping > > margin > > problem. In production code, with many error checks in the main logic, > > this > > can make code very hard to understand. With a goto, we have > > > > cmd_line_words = command_line ( ) > > if length ( cmd_line_words ) != 4 then > > goto arg_count_is_wrong > > end if > > cmd_args = cmd_line_words [ 3..4 ] > > input_file_name = cmd_args [ 1 ] > > input_file_nr = open ( input_file_name, "r" ) > > if input_file_nr = -1 then > > goto cant_open_input_file > > end if > > output_file_name = cmd_args [ 2 ] > > output_file_nr = open ( output_file_name, "w" ) > > if output_file_nr = -1 then > > goto cant_open_output_file > > end if > > while 1 do > > line = gets ( input_file_nr ) > > if atom ( line ) then > > exit > > end if > > puts ( output_file_nr, line ) > > end while > > Printf ( "%s copied to %s.\n", > > { input_file_name, output_file_name } ) > > goto halt > > > > arg_count_is_wrong: > > Printf ( "Exactly two arguments expected -- %d supplied.\n", > > { length ( cmd_line_words ) - 2 } ) > > goto halt > > cant_open_input_file: > > Printf ( "Can't open input file %s.\n", { input_file_name } ) > > goto halt > > cant_open_output_file: > > Printf ( "Can't open output file %s.\n", { output_file_name } ) > > goto halt > > halt: > > > > Here, the main (non-error) logic is much more apparent. But what, you > > ask, > > is then to prevent evil programmers from generating spaghetti code? > > Well, > > nothing. So perhaps a more constrained construct would be better. In > > the > > spirit of Ada exceptions, we might try something like this: > > > > -- > > -- Function/procedure body or main program starts here. > > -- > > cmd_line_words = command_line ( ) > > exception arg_count_is_wrong if length ( cmd_line_words ) != 4 > > cmd_args = cmd_line_words [ 3..4 ] > > input_file_name = cmd_args [ 1 ] > > input_file_nr = open ( input_file_name, "r" ) > > exception cant_open_input_file if input_file_nr = -1 > > output_file_name = cmd_args [ 2 ] > > 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 > > Printf ( "%s has been copied to %s.\n", > > { input_file_name, output_file_name } ) <snip>
2. RE: goto considered essential
- Posted by Jason Gade <jaygade at yahoo.com> Jan 21, 2006
- 450 views
- Last edited Jan 22, 2006
Lynn Kilroy wrote: > > Wasn't there an exit thingabobbers I saw mentioned somewhere? I thought > it would be neat to use, because it doesn't exist in QBasic, and I had > to setup escape sequences that would abort my loops by resetting > variables and stuff. Really messy and unclear. Exit looked like a > great alternative. > > Love & Friendship & Blessed Be! > Lynn Erika Kilroy Yes, there is an exit statement. It will leave the current while or for loop that you are in. It only exits one level, though. And unfortunately there is no corresponding continue statement, that would jump back to the top of the loop. -- "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.
3. RE: goto considered essential
- Posted by Lynn Kilroy <leks_transportation at hotmail.com> Jan 22, 2006
- 448 views
Jason Gade wrote: > > > posted by: Jason Gade <jaygade at yahoo.com> > > Lynn Kilroy wrote: > > > > Wasn't there an exit thingabobbers I saw mentioned somewhere? I thought > > > > it would be neat to use, because it doesn't exist in QBasic, and I had > > to setup escape sequences that would abort my loops by resetting > > variables and stuff. Really messy and unclear. Exit looked like a > > great alternative. > > > > Love & Friendship & Blessed Be! > > Lynn Erika Kilroy > > Yes, there is an exit statement. It will leave the current while or for > loop that you are in. It only exits one level, though. > > And unfortunately there is no corresponding continue statement, that > would jump back to the top of the loop. > > -- > "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. Would there be an instance where exiting one would want you to exit another? I mean, without the other really expecting it? I mean, if you set it up to Exit one, you could, just prior to exiting the one loop, setup to exit the one it's nested in if necessary. Love & Friendship & Blessed Be! Lynn Erika Kilroy
4. RE: goto considered essential
- Posted by Jason Gade <jaygade at yahoo.com> Jan 22, 2006
- 443 views
Lynn Kilroy wrote: > > > Jason Gade wrote: > > > > > > posted by: Jason Gade <jaygade at yahoo.com> > > > > Lynn Kilroy wrote: > > > > > > Wasn't there an exit thingabobbers I saw mentioned somewhere? I thought > > > > > > it would be neat to use, because it doesn't exist in QBasic, and I had > > > to setup escape sequences that would abort my loops by resetting > > > variables and stuff. Really messy and unclear. Exit looked like a > > > great alternative. > > > > > > Love & Friendship & Blessed Be! > > > Lynn Erika Kilroy > > > > Yes, there is an exit statement. It will leave the current while or for > > loop that you are in. It only exits one level, though. > > > > And unfortunately there is no corresponding continue statement, that > > would jump back to the top of the loop. > > > > -- > > "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. > > Would there be an instance where exiting one would want you to exit > another? I mean, without the other really expecting it? I mean, if you > set it up to Exit one, you could, just prior to exiting the one loop, > setup to exit the one it's nested in if necessary. > > Love & Friendship & Blessed Be! > Lynn Erika Kilroy > > Good question. Generally there can be conditions in a deeply nested loop where you need to exit the whole thing. Of course that can introduce problems of its own, such as do you use a number or a variable or a label to determine how many loops you exit? Otherwise outer loops have to check flags to determine whether they need to exit or not. Sometimes you just have to refactor your routine to figure a better way of doing it and not get caught up trying to get too elegant. -- "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.
5. RE: goto considered essential
- Posted by Lynn Kilroy <leks_transportation at hotmail.com> Jan 22, 2006
- 452 views
Jason Gade wrote: > > > posted by: Jason Gade <jaygade at yahoo.com> > > Rad wrote: > >=20 > > C Bouzy wrote: > > >=20 > > > Tom, > > >=20 > > > If you were to do a search you will come across numerous discussions= > > > regarding > > > goto. Robert is not going to add goto, and in my opinion it does very= > > > little > > > to help the language.=20 > > >=20 > > > ----If you continue to do what you have always done, > > > you will get what you have always gotten.---- > >=20 > > If we are talking about "bad programming habits" as main deterrent for= > > goto, > > then I would say that no programming language can have any bad habits.= > > It is > > the programmer who can. > >=20 > > Any programming language provides various resources for the programmer= > > to > > build his/her logic. It is really up to the programmer to use the bad o= r > > good logic using these resources. > >=20 > > It is possible to write a program with worst logic which doesn't have a= > > single goto as well as a best program which makes use of goto=92s at > > essential points. To pick up these essential points is the programmer= =92s=20 > > job. > >=20 > > In end, goto=92s won=92t hurt from the programming language perspective= , but > > can hurt from programmer=92s viewpoint, if not used properly. > > Eu should implement goto if possible and allow the programmer to take > > a call on it. > >=20 > > Regards, > > Rad. > > I'm weird this way. I don't normally think this way but in this case I= > do. > > I don't want to tell people they can or can't do something, but I > wouldn't want to see goto added to Euphoria. I've seen a lot of good > arguments for it but none of them have swayed me. > > Adding goto probably wouldn't change my personal style. It is just one= > of those things that I think would lessen the beauty of the language. > > -- > "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. I tend to agree. I've not used GOTO in years. And I'm coming from QBasic - where most people see GOTO all the time. Come to think of it, I think the last time I identified a line at all was ... at least nine years ago? Gods! Where does the time go! Love & Friendship & Blessed Be! Lynn Erika Kilroy