Re: how do people feel about exit function from loop





Robert Morley wrote:
You know, I think you've gotten to the heart of why I don't like Exit For and similar constructs, never mind that structured programming says you shouldn't use them. Imagine a complex loop rendered into English:

For w. Exits
"For each of these items, I want you to do some things to them. Except if you run across this. Oh, and also stop if you find something else. Or if you run into any problems."

While/Until/etc.
"Unless you run across this, find something else, or run into any problems, do some things to each of these items."

Now, you might argue that the change in sentence structure makes the first appear more disruptive, but in truth, when I look at most Exit For conditions, that's *exactly* how I perceive them...something unexpected when you thought you were going to flow smoothly. When I see a While loop, I know that there are conditions to be fulfilled, can examine them all at once, then find out what I do as long as those conditions aren't fulfilled.
The problem with the first approach is that there are more than one exit from the routine, which means
the reader has to examine the whole routine for exceptions to the control logic before they can examine
the data operations. Keeping loop code to a single entry and exit point makes sense and should be the
preferred method of implementation. If a routine has this many exceptions it should probably be
re-factored.

Heres a great series of questions to ask yourself when writing loops...

http://www.cc2e.com/Page.aspx?hid=233

D.
.



Relevant Pages

  • Re: Using ZLib
    ... >> If only it were so easy - I tried using a repeat..until loop, ... >when Count>bytes left, inflate() sets FZRec.avail_out to 0, otherwise ... the main exit condition, and suggest that you check your Delphi CD. ... routine to ensure that they are asking for /exactly/ the decompressed ...
    (comp.lang.pascal.delphi.misc)
  • Re: Infinite Loops and Explicit Exits
    ... > CS> One of these proposals relaxes the current restriction that an EXIT ... > termination of the loop is not visible at that point. ... > terminating condition is visible in that context. ... > You now want to allow this remote procedure, ...
    (comp.lang.cobol)
  • Re: Houston-related rants was Re: forced merges/inside lane merges/AASHTO "tapered merges"
    ... >> Are you sure about the North Loop and I-45? ... >> in particular the westbound exit for Fondren/Gessner. ... How could HCTRA put an exit ramp for eastbound ... and the West Houston Center Blvd exit still shows Old Westheimer ...
    (misc.transport.road)
  • Re: Exit Do
    ... I've been instructed by a dot net / asp programmer that Exit Do is ... I am using nested loops and need to exit out of one loop into ... Code Stage B ... likely to lead to bugs and readability problems. ...
    (microsoft.public.scripting.vbscript)
  • Re: Exit Do
    ... I've been instructed by a dot net / asp programmer that Exit Do is ... I am using nested loops and need to exit out of one loop into ... If Some Reason to stop loop then Exit Do ... Code Stage B ...
    (microsoft.public.scripting.vbscript)