A
Aaron Ackerman
What is the sytax for exiting a for loop in C#?
What is the sytax for exiting a for loop in C#?
Aaron Ackerman said:What is the sytax for exiting a for loop in C#?
What is the sytax for exiting a for loop in C#?
Brady Kelly said:To be picky, you should use a while loop and include whatever would
cause an exit from your for loop in the loop condition of the while
loop.
Roy Fine said:BREAKS in a FOR loop means that you should have picked a WHILE loop instead.
If you accept McConnell's work in Code Complete as the style authority for
software construction - see section 15. The FOR loop is to be used when the
number of iterations is know aforehand.
"The FOR loop is for simple loops. Most complicated looping tasks are
better handled by the WHILE loop." (pg 329)
"Limiting yourself to only one statement to control a loop's exit consdition
is a powerful way to simplify your loop." (pg 328)
While you may disagree or have a personal preference or can cite some
specific instance to the contrary, Code Complete is hard to agrue with
because :
1) It's adopted by a lot of programmers and their repsective shops
2) The goal IS to create readable code (i.e. easier to understand, easier to
maintain)
3) While opinions relating to programming style issues are like ears (every
one has at least two), McConnell certainly established the baseline - and
one has to build a strong case for the exceptions.
While you may disagree or have a personal preference or can cite some
specific instance to the contrary, Code Complete is hard to agrue with
because :
1) It's adopted by a lot of programmers and their repsective shops
2) The goal IS to create readable code (i.e. easier to understand, easier to
maintain)
3) While opinions relating to programming style issues are like ears (every
one has at least two), McConnell certainly established the baseline - and
one has to build a strong case for the exceptions.
that's the way i see it, but i could be completly wrong.
Brady Kelly said:I did say I was being picky when I said one should always use a while
loop to 'break out' of. In terms of readability, a loop body
shouldn't be long enough to create readability problems using any
approach, and in my opinion, although no correct, a 'break' from a for
loop is quite readable. It means, jump to the first statement after
all this loop code, and it doesn't require me to visit the pre/post
loop check to evaluate conditions.
That doesn't mean that every bit of it is absolutely perfect though,
does it? It makes sense to adopt most of a coding standard which you
mostly agree with, but not take the whole thing without allowing
yourself to disagree with any part of it.
Aaron Ackerman said:What is the sytax for exiting a for loop in C#?
Roy Fine said:Read CodeComplete first.
It is not about finding exceptions to the rule, or even about making your
own rules. Rather, it is about following the rules that are already
accepted.
Roy Fine said:then you have no standard, and you have no uniform style - what you have
just described is ad hoc, and that's what get's serious programs into
trouble.
Michael Voss said:One might as well want to put the loop in a new method and just return from
that method with whatever you'd like to give as a return value whenever you
like (other oo languages do not have (or need) something like "break" or
"continue")...
Roy Fine said:read this
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=i36b25.a92
.ln%40sol.dimaga.com&rnum=2&prev=/groups%3Fq%3DMcConnell%2Bcode%2Bcomp
lete%2Bgroup:comp.*%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.*%26
selm%3Di36b25.a92.ln%2540sol.dimaga.com%26rnum%3D2
Sometimes that's appropriate. Other times, it's not. In a few cases it
can increase the readability to move the method out - but not in
*every* case.
Just because other languages don't have a certain feature doesn't mean
that it shouldn't be used.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.