Re: how do people feel about exit function from loop



"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:Offn5aA0HHA.3848@xxxxxxxxxxxxxxxxxxxxxxx
True enough, but our estimates of when Exit For is and isn't appropriate
(even ignoring structured programming arguments for the moment) vary
significantly. Maybe it just because I'm a DB programmer, but in my
experience, the type of looping structure I was referring to is not at all
uncommon.

I don't think that is true at all. Unfortunately I'm a DB programmer as well
and almost every loop I do is appropriate for exit for (assuming it needs to
terminate early at all). I almost never encounter a loop that needs a
variable to exit. When I say almost never I reckon I could count on 1 hand
in 10+ years. If you think this is common I think you are stetching the
truth.

Actually, no offense to Rick, but I think he demonstrates quite nicely how
sometimes more lines of code can be clearer than less (especially ONE)
line(s) of code. That said, some of his one-liners are quite elegant. I
find your logic about Exit For here interesting, and perhaps it explains
why we go back and forth on this so much...I've always thought it was
better to have clear exit conditions at the beginning of the loop and know
the conditions you're looking for as you scan through, rather than several
potential "unexpected" exit points.

This is a good point, it is possible for the maintenance programmer to miss
the exit for. Although I don't think it's a big deal, they could also miss
where is sets the exit variable to true.

I've seen some Exit For's where the logic is not at all obvious, so I am a
case in point. Your doubt is disproven. That said, I find most Exit
For's about the same as the equivalent logic without. As I've said many
MANY times now, it's all a matter of what you're used to reading...kinda
like how many people find the usage "immediately she was awake, she got
out of bed" rather jarring, even though it's technically perfectly
correct.

While it may be a question of style there is no question the original
example was much simpler with an exit for.

In simpler cases, often it does. Not always, though...

dim strTest as string
i = 100
While (strTest = "") and (i > 0)
strTest = SomeFunction(i)
i = i - 1
Wend

vs.

For i = 100 to 1 Step -1
if SomeFunction(i) = "" then exit for
Next

Yes, the For has 1 less LINE of code,

I've made a couple of changes and the second is half the lines of code as
the first. I don't see how this can be considered a bad thing. I don't see
how the second could not possibly be considered clearer.

but I see it as more complex because of the inverse stepping and the
additional If clause. Not to mention that you have to dig into the code
to find out what the exit conditions are. More complex examples of While's
might actually require less code, but even if they don't, what's a line or
two when you're talking dozens or even hundreds of lines?

Less code is still better when all else is equal. This does not mean you
should go too much out of your way to reduce line count but when it is as
simple as this I don't see why not.

Michael


.



Relevant Pages

  • Re: Dynamically resizing a buffer
    ... programmer, throwing your hands up in the air (and most likely to ... few calls to exit() in ancient code, so I can't claim I never use it. ... assert macro in conjunction with the exitfunction. ...
    (comp.lang.c)
  • Re: Comments are welcome for two sequential search C functions
    ... the next programmer" - clarity is ... I agree that clarity is more important than bureaucracy, ... "single exit point" technique because, in my view, it aids clarity. ...
    (comp.lang.c)
  • Re: COBOL aint quite dead - yet !
    ... always struck me as a chancy thing to do. ... an exit which wasn't used. ... a section that consisted of a paragraph and an exit which hasn't been ... programmer finds that such a imperative is forbidden because someone ...
    (comp.lang.cobol)
  • Re: Username & ComputerName With Access2003
    ... > programmer, and I have no idea what you are saying. ... The before insert only exists for the whole form, not a particular control. ... if the user decides to exit out of the form without saving ... and then on the properties sheet for that control, ...
    (microsoft.public.access.formscoding)
  • Re: Report enhancements
    ... I want to see the _normal_ exit conditions in UNTIL, ... If there are exception conditions that can cause a premature exit, ... infinite loop, therefore a bug. ... compiler to diagnose it as syntax error. ...
    (comp.lang.cobol)