Re: how do people feel about exit function from loop
- From: "Michael C" <nospam@xxxxxxxxxx>
- Date: Mon, 30 Jul 2007 10:37:22 +1000
"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
.
- Follow-Ups:
- Re: how do people feel about exit function from loop
- From: Robert Morley
- Re: how do people feel about exit function from loop
- From: Steve Gerrard
- Re: how do people feel about exit function from loop
- References:
- how do people feel about exit function from loop
- From: greg
- Re: how do people feel about exit function from loop
- From: Ken Halter
- Re: how do people feel about exit function from loop
- From: Jan Hyde (VB MVP)
- Re: how do people feel about exit function from loop
- From: Steve Gerrard
- Re: how do people feel about exit function from loop
- From: Michael C
- Re: how do people feel about exit function from loop
- From: Robert Morley
- Re: how do people feel about exit function from loop
- From: Michael C
- Re: how do people feel about exit function from loop
- From: Robert Morley
- Re: how do people feel about exit function from loop
- From: Michael C
- Re: how do people feel about exit function from loop
- From: Robert Morley
- Re: how do people feel about exit function from loop
- From: Michael C
- Re: how do people feel about exit function from loop
- From: Robert Morley
- how do people feel about exit function from loop
- Prev by Date: Re: Help for a VB prog
- Next by Date: Re: how do people feel about exit function from loop
- Previous by thread: Re: how do people feel about exit function from loop
- Next by thread: Re: how do people feel about exit function from loop
- Index(es):
Relevant Pages
|