in reply to Unconventional exit from while loop
Hmmm, I would suppose that, as of today, we are all writing in structured programming, aren't we?
The old debate on GOTOs is gone and has been away for quite a while, isn't it?
I have written quite a number of programs packed with GOTOs or equivalent statements in various languages (Fortran, Basic, Assembler, etc.) in the 1980s. Thanks God, N. Wirth and E. Dijstra, among others, have given us better constructs, such as if/then/else, case or switch, etc.
Having said that, I am using very commonly a proprietary language where the GOTO is the only exit forward. I have a procedure looping on the records of a database, I am making a number of tests on the current record, but my only way to say that this record is not one that I want to process is to say something like "goto next record". I do not have a 'next' or a 'continue' instruction or whatever, so the goto is my favorite.
Consider the following pseudo code:
if (a != b) if (a != c) if (a != d) if (a != e) f = a else ... endif endif endif
This is just a short example, I have seen examples with about ten level of if/then/else nesting. This is becoming difficult to follow. Compare to:
next if a == b; next if a == c; next if a == d; ...
Isn't the second version clearer?
In the proprietary language I am talking about, I have to do something like this:
if (a == b) goto next_record; if (a == c) goto next_record; ...
Should I faint on the GOTO word?
I do not think so. In such a case, I claim that GOTOs make the code much cleaner and much more readable that a strict adhesion to some idealized version of structured programing.
The authors ot the Camel Book say somewhere (if I remember correctly) that progamming is often a matter of descending a decision tree. There is nothing wrong, in their view, with using the next, continue and last keywords to shortcircuit annoying cases and get right to the interesting stuff.
Having said all that, it is of course up to you to decide how far you want to go challenging some principles that we have all been nurtured with.
Well, my two cents worth...
|
|---|