By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

How do I exit a for loop in C#

P: n/a
What is the sytax for exiting a for loop in C#?
Nov 16 '05 #1
Share this Question
Share on Google+
65 Replies


P: n/a
try "break;"
"Aaron Ackerman" <no**@nospam.com> дϢ
news:#1*************@tk2msftngp13.phx.gbl...
What is the sytax for exiting a for loop in C#?

Nov 16 '05 #2

P: n/a
"Aaron Ackerman" <no**@nospam.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
What is the sytax for exiting a for loop in C#?


continue; will stop processing in the current loop and jump straight to the
next

break; will exit the entire loop
Nov 16 '05 #3

P: n/a
"Aaron Ackerman" <no**@nospam.com> wrote in message news:<#1*************@tk2msftngp13.phx.gbl>...
What is the sytax for exiting a for loop in C#?


for(..)
{
...
break;
}

Aaron
Nov 16 '05 #4

P: n/a
On Mon, 5 Apr 2004 23:00:15 -0400, "Aaron Ackerman" <no**@nospam.com>
wrote:
What is the sytax for exiting a for loop in C#?


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.
Nov 16 '05 #5

P: n/a
Brady Kelly <bk****@icom-it.co.za> wrote:
What is the sytax for exiting a for loop in C#?


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.


I disagree on that. There are *loads* of times where you basically just
want to get out of a loop at a certain point - and that may be
different points in the execution of the block within the loop.

It's one of those "rules" which might sound okay in principle, but ends
up making the code completely unreadable in practice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
On Mon, 5 Apr 2004 23:00:15 -0400, "Aaron Ackerman" <no**@nospam.com>
wrote:
What is the sytax for exiting a for loop in C#?


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.
Nov 16 '05 #7

P: n/a
Brady Kelly <bk****@icom-it.co.za> wrote:
What is the sytax for exiting a for loop in C#?


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.


I disagree on that. There are *loads* of times where you basically just
want to get out of a loop at a certain point - and that may be
different points in the execution of the block within the loop.

It's one of those "rules" which might sound okay in principle, but ends
up making the code completely unreadable in practice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
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.

that's the way i see it, but i could be completly wrong.

regards
roy fine
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Brady Kelly <bk****@icom-it.co.za> wrote:
What is the sytax for exiting a for loop in C#?


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.


I disagree on that. There are *loads* of times where you basically just
want to get out of a loop at a certain point - and that may be
different points in the execution of the block within the loop.

It's one of those "rules" which might sound okay in principle, but ends
up making the code completely unreadable in practice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
BREAKS in a FOR loop means that you should have picked a WHILE loop instead.
Not necessarily. When there are multiple places in the loop where you
might want to break, it can be a complete pain to recode that as a
while loop. Either you end up with a horrendously large condition, or
you end up with a temporary variable which only serves to let you break
out - in which case you might as well break out directly, in my view.
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.
I don't accept Code Complete to be flawless in every particular. That
seems a very bold statement about the for loop which would certainly
need more backing up than what you've presented. He's stated that *he*
likes to use the for loop only for simple loops, but at least in the
passages you've quoted, he hasn't stated *why*.
"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)
Well, I disagree.
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
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.
2) The goal IS to create readable code (i.e. easier to understand, easier to
maintain)
On that we agree.
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.


The exceptions seem pretty obvious to me. Here's an example - the task
is to see whether or not a string looks like a valid decimal number.
The rules are (for the purposes of this case):

o No whitespace anywhere
o No + or - (as that can be dealt with more effectively outside the
loop)
o A single decimal point is allowed (only allow '.' not ',')
o All other characters must be in the range '0'-'9'

To me, the simplest way of expressing that in C# is:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c != '.' && (c < '0' || c > '9'))
{
return false;
}
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
}
}
return true;
}

An alternative might be:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
continue;
}
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}

(Obviously the foreach can be easily recoded as a "real" for if you
want.)

What is the more readable while loop here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

P: n/a
On Tue, 6 Apr 2004 20:10:32 -0400, "Roy Fine"
<rl****@twt.obfuscate.net> wrote:
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.


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.
Nov 16 '05 #11

P: n/a
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.

that's the way i see it, but i could be completly wrong.

regards
roy fine
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Brady Kelly <bk****@icom-it.co.za> wrote:
What is the sytax for exiting a for loop in C#?


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.


I disagree on that. There are *loads* of times where you basically just
want to get out of a loop at a certain point - and that may be
different points in the execution of the block within the loop.

It's one of those "rules" which might sound okay in principle, but ends
up making the code completely unreadable in practice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12

P: n/a
Brady Kelly <bk****@icom-it.co.za> wrote:
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.


If it's readable, then in what way is it "not correct"?

There's no point in adhering to rules which are counter-productive. If
using a for loop gives you greater readability than using a while loop
and with no other adverse effects, then go for it - ignore rigid rules
which attempt (and fail) to cover every situation.

A *guideline* of "wherever you find yourself using a break in a for
loop, consider using a while loop" is one thing - but saying that it's
*incorrect* to use a break in a for loop is an entirely different
matter, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13

P: n/a
Jon

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.

regards
roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
BREAKS in a FOR loop means that you should have picked a WHILE loop instead.

Not necessarily. When there are multiple places in the loop where you
might want to break, it can be a complete pain to recode that as a
while loop. Either you end up with a horrendously large condition, or
you end up with a temporary variable which only serves to let you break
out - in which case you might as well break out directly, in my view.
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.


I don't accept Code Complete to be flawless in every particular. That
seems a very bold statement about the for loop which would certainly
need more backing up than what you've presented. He's stated that *he*
likes to use the for loop only for simple loops, but at least in the
passages you've quoted, he hasn't stated *why*.
"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)


Well, I disagree.
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


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.
2) The goal IS to create readable code (i.e. easier to understand, easier to maintain)


On that we agree.
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.


The exceptions seem pretty obvious to me. Here's an example - the task
is to see whether or not a string looks like a valid decimal number.
The rules are (for the purposes of this case):

o No whitespace anywhere
o No + or - (as that can be dealt with more effectively outside the
loop)
o A single decimal point is allowed (only allow '.' not ',')
o All other characters must be in the range '0'-'9'

To me, the simplest way of expressing that in C# is:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c != '.' && (c < '0' || c > '9'))
{
return false;
}
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
}
}
return true;
}

An alternative might be:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
continue;
}
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}

(Obviously the foreach can be easily recoded as a "real" for if you
want.)

What is the more readable while loop here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #14

P: n/a
Hi !

"Aaron Ackerman" wrote:
What is the sytax for exiting a for loop in C#?


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")...
Nov 16 '05 #15

P: n/a
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.


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.

roy
Nov 16 '05 #16

P: n/a
read this


http://groups.google.com/groups?hl=e...com%26rnum%3D2

roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brady Kelly <bk****@icom-it.co.za> wrote:
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.


If it's readable, then in what way is it "not correct"?

There's no point in adhering to rules which are counter-productive. If
using a for loop gives you greater readability than using a while loop
and with no other adverse effects, then go for it - ignore rigid rules
which attempt (and fail) to cover every situation.

A *guideline* of "wherever you find yourself using a break in a for
loop, consider using a while loop" is one thing - but saying that it's
*incorrect* to use a break in a for loop is an entirely different
matter, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #17

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
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.


But why accept them if they're silly? A rule such as "never break out
of a for loop, even if it means you've got to write a far less readable
while loop" *are* silly. I refuse to accept any argument *solely* on
the grounds that other people have accepted it. If it can't stand up on
its own merits, I'm not going to accept it.

So, where's the *more* readable while loop version of the code I
posted?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #18

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
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.


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.


I have a uniform style - but one which can't be rigidly defined in
terms of rules, as it involves stylistic decisions which sometimes have
to be made on a case-by-case basis.

A standard doesn't have to be "in *every* case, you *must* do this" in
order to be useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19

P: n/a
Michael Voss <mi**********@lvrREMOVE.deCAPS> wrote:
"Aaron Ackerman" wrote:
What is the sytax for exiting a for loop in C#?


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")...


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #20

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
read this http://groups.google.com/groups?hl=e...adm=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


I don't need to be persuaded that it's a popular book. That doesn't
mean that every line of it should be accepted without looking at the
arguments presented, does it?

I note that you haven't actually tried to really present the arguments
- you've just said that someone who is well respected made the
argument, and left it at that.

Why not address the actual issue, rather than just appealing to
authority? If his arguments stand up, present his arguments. If they
don't - well, then why should you accept them just because he makes
good points about *other* situations?

Why not address my specific example where I believe a for loop is more
readable than a while loop?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #21

P: n/a
Jon

the OP was quite specific - how do you exit a for loop? the answer is in
the header. if that doesn't work, or is too complicated, then use the while
loop. if you have specific reasons for otherwise, they are specific - keep
them to yourself and don't try to inject them as a norm or standard or as a
general excuse for not adhering to a style, esp when said style is
universally adopted, and well respected.

you seem to have a very strong sense of right and wrong, esp vis-a-vis
programming style - and you seem to be advocating your own set of style
guides. if that works for you, that's quite OK with me - I have no
misplaced perceptions of being able to convince you otherwise.

roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
read this

http://groups.google.com/groups?hl=e...adm=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


I don't need to be persuaded that it's a popular book. That doesn't
mean that every line of it should be accepted without looking at the
arguments presented, does it?

I note that you haven't actually tried to really present the arguments
- you've just said that someone who is well respected made the
argument, and left it at that.

Why not address the actual issue, rather than just appealing to
authority? If his arguments stand up, present his arguments. If they
don't - well, then why should you accept them just because he makes
good points about *other* situations?

Why not address my specific example where I believe a for loop is more
readable than a while loop?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #22

P: n/a
HI !
"Jon Skeet [C# MVP]" wrote:

[...snip...]
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.
I think, moving the loop into another method will lead to shorter methods in
most (if not close to all) cases and thus increase readability most of the
time - but that's just my opinion...
Just because other languages don't have a certain feature doesn't mean
that it shouldn't be used.


[...snip...]

Of course not; I was merely presenting another alternative to the op. But,
on the other hand, being given a certain feature by the designers of a
language does not make this feature the #1 choice in all of the cases (or
even in the majority of the cases), either.

Nov 16 '05 #23

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
the OP was quite specific - how do you exit a for loop? the answer is in
the header. if that doesn't work, or is too complicated, then use the while
loop. if you have specific reasons for otherwise, they are specific - keep
them to yourself and don't try to inject them as a norm or standard or as a
general excuse for not adhering to a style, esp when said style is
universally adopted, and well respected.
It's clearly *not* universally adopted though - I see breaks and
continues in for-loops all over the place.

Just because the style a book espouses is *in the main* well respected
doesn't mean that *every* part of it should be.

I really don't see why criticising a supposedly respected style by
bringing up a reasonable common counter-example should make you suggest
I should keep that to myself. My example wasn't particularly specific
to me - it's the kind of loop lots of people need to write, a lot of
the time.

Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?
you seem to have a very strong sense of right and wrong, esp vis-a-vis
programming style - and you seem to be advocating your own set of style
guides. if that works for you, that's quite OK with me - I have no
misplaced perceptions of being able to convince you otherwise.


I wasn't so much advocating my own set of style guides as suggesting
that the ones you were quoting should at least be backed up by
arguments better than "Well, it's in Code Complete, so it must be
right".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #24

P: n/a
Michael Voss <mi**********@lvrREMOVE.deCAPS> wrote:
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.


I think, moving the loop into another method will lead to shorter methods in
most (if not close to all) cases and thus increase readability most of the
time - but that's just my opinion...


Yes, it will almost always lead to more, shorter methods. That
*doesn't* automatically increase readability, however. If you end up
needing to pass in several variables, some of them by reference, some
not, etc, then it'll quite possibly end up lowering readability.

Just thinking up the name for the replacement method is quite a good
guideline, I find. If it really doesn't have a good description because
it only makes sense in the circumstance of the calling method anyway,
it may well *not* be a good candidate for refactoring. (This guideline
doesn't always work, of course.)

Keeping methods short is a good rule of thumb, until you end up with
hundreds of methods, each of which is only two lines long, and which
just call other methods. There's a balance to be struck. (I don't
believe you were particularly arguing against that though.)
Just because other languages don't have a certain feature doesn't mean
that it shouldn't be used.


[...snip...]

Of course not; I was merely presenting another alternative to the op. But,
on the other hand, being given a certain feature by the designers of a
language does not make this feature the #1 choice in all of the cases (or
even in the majority of the cases), either.


Absolutely. A language feature should stand on its own merit: not on
the grounds that:

a) It's been included in this language, so it must be good
b) It's been excluded from other languages, so it must be bad
c) The author of Code Complete doesn't like it, so it must be bad

Those are all "meta-reasons" - what I'm far more interested is
concrete, specific reasons why it should or shouldn't be used in
specific situations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #25

P: n/a

Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?


1) I am not trying to stifle
2) the discussion has not risen to the level of reasonable
3) for loops with breaks in should be avoided - you want to make them
standard
roy
Nov 16 '05 #26

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?
1) I am not trying to stifle


Really? What other purpose does a comment such as:

"if you have specific reasons for otherwise, they are specific - keep
them to yourself"

serve then? As soon as you start telling people to keep their opinions
to themselves during a discussion, you're stifling debate to my mind.
2) the discussion has not risen to the level of reasonable
That's because you've refused to engage with the meat of the
discussion, preferring to just recite the mantra of "do it because Code
Complete tells you to".
3) for loops with breaks in should be avoided - you want to make them
standard


And still you present no actual *reasons* - or indeed a more readable
while loop than my for loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #27

P: n/a
Jon Skeet [C# MVP] wrote:
I wasn't so much advocating my own set of style guides as suggesting
that the ones you were quoting should at least be backed up by
arguments better than "Well, it's in Code Complete, so it must be
right".


ok, my curiosity got the better of me, and I pulled my copy of Code
Complete of the shelf and had a look. Steve discusses Loop with exits
on page 326 and 327, he certainly doesn't advocate that they are not
used AFAICT, in fact he quotes the Software productvity consortium of
1989 that shows that in Ada this is the preferred kind of loop control
and that a student study showed that students scored 25% higher on a
comprehension test with this type of structure (Soloway, Bonar and
Ehrlich 1983)

For what it is worth, when Steve McConnell makes authoritive statements
in his book, they are always backed up by Hard Data, it is very rarely
a subjective thing. It's quite amazing to me that this book which is
now over 10 years old is still very relevant today I recommend it
highly. But I don't think anyone should become a slave to his
recommendations and conclusions, good judgment and experience count for
just as much as good texts.

Rgds Tim.
Nov 16 '05 #28

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
BREAKS in a FOR loop means that you should have picked a WHILE loop instead.
Not necessarily. When there are multiple places in the loop where you
might want to break, it can be a complete pain to recode that as a
while loop. Either you end up with a horrendously large condition, or
you end up with a temporary variable which only serves to let you break
out - in which case you might as well break out directly, in my view.
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.
I don't accept Code Complete to be flawless in every particular. That
seems a very bold statement about the for loop which would certainly
need more backing up than what you've presented. He's stated that *he*
likes to use the for loop only for simple loops, but at least in the
passages you've quoted, he hasn't stated *why*.
"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)
Well, I disagree.
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
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.
2) The goal IS to create readable code (i.e. easier to understand, easier to
maintain)
On that we agree.
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.


The exceptions seem pretty obvious to me. Here's an example - the task
is to see whether or not a string looks like a valid decimal number.
The rules are (for the purposes of this case):

o No whitespace anywhere
o No + or - (as that can be dealt with more effectively outside the
loop)
o A single decimal point is allowed (only allow '.' not ',')
o All other characters must be in the range '0'-'9'

To me, the simplest way of expressing that in C# is:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c != '.' && (c < '0' || c > '9'))
{
return false;
}
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
}
}
return true;
}

An alternative might be:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
continue;
}
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}

(Obviously the foreach can be easily recoded as a "real" for if you
want.)

What is the more readable while loop here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #29

P: n/a
On Tue, 6 Apr 2004 20:10:32 -0400, "Roy Fine"
<rl****@twt.obfuscate.net> wrote:
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.


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.
Nov 16 '05 #30

P: n/a
Brady Kelly <bk****@icom-it.co.za> wrote:
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.


If it's readable, then in what way is it "not correct"?

There's no point in adhering to rules which are counter-productive. If
using a for loop gives you greater readability than using a while loop
and with no other adverse effects, then go for it - ignore rigid rules
which attempt (and fail) to cover every situation.

A *guideline* of "wherever you find yourself using a break in a for
loop, consider using a while loop" is one thing - but saying that it's
*incorrect* to use a break in a for loop is an entirely different
matter, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #31

P: n/a
Jon

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.

regards
roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
BREAKS in a FOR loop means that you should have picked a WHILE loop instead.

Not necessarily. When there are multiple places in the loop where you
might want to break, it can be a complete pain to recode that as a
while loop. Either you end up with a horrendously large condition, or
you end up with a temporary variable which only serves to let you break
out - in which case you might as well break out directly, in my view.
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.


I don't accept Code Complete to be flawless in every particular. That
seems a very bold statement about the for loop which would certainly
need more backing up than what you've presented. He's stated that *he*
likes to use the for loop only for simple loops, but at least in the
passages you've quoted, he hasn't stated *why*.
"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)


Well, I disagree.
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


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.
2) The goal IS to create readable code (i.e. easier to understand, easier to maintain)


On that we agree.
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.


The exceptions seem pretty obvious to me. Here's an example - the task
is to see whether or not a string looks like a valid decimal number.
The rules are (for the purposes of this case):

o No whitespace anywhere
o No + or - (as that can be dealt with more effectively outside the
loop)
o A single decimal point is allowed (only allow '.' not ',')
o All other characters must be in the range '0'-'9'

To me, the simplest way of expressing that in C# is:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c != '.' && (c < '0' || c > '9'))
{
return false;
}
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
}
}
return true;
}

An alternative might be:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
continue;
}
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}

(Obviously the foreach can be easily recoded as a "real" for if you
want.)

What is the more readable while loop here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #32

P: n/a
Hi !

"Aaron Ackerman" wrote:
What is the sytax for exiting a for loop in C#?


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")...
Nov 16 '05 #33

P: n/a
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.


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.

roy
Nov 16 '05 #34

P: n/a
read this


http://groups.google.com/groups?hl=e...com%26rnum%3D2

roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brady Kelly <bk****@icom-it.co.za> wrote:
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.


If it's readable, then in what way is it "not correct"?

There's no point in adhering to rules which are counter-productive. If
using a for loop gives you greater readability than using a while loop
and with no other adverse effects, then go for it - ignore rigid rules
which attempt (and fail) to cover every situation.

A *guideline* of "wherever you find yourself using a break in a for
loop, consider using a while loop" is one thing - but saying that it's
*incorrect* to use a break in a for loop is an entirely different
matter, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #35

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
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.


But why accept them if they're silly? A rule such as "never break out
of a for loop, even if it means you've got to write a far less readable
while loop" *are* silly. I refuse to accept any argument *solely* on
the grounds that other people have accepted it. If it can't stand up on
its own merits, I'm not going to accept it.

So, where's the *more* readable while loop version of the code I
posted?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #36

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
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.


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.


I have a uniform style - but one which can't be rigidly defined in
terms of rules, as it involves stylistic decisions which sometimes have
to be made on a case-by-case basis.

A standard doesn't have to be "in *every* case, you *must* do this" in
order to be useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #37

P: n/a
Jon Skeet [C# MVP] wrote:
[...]
To me, the simplest way of expressing that in C# is:

public static bool IsDecimal (string data)
{
bool gotPoint = false;

foreach (char c in data)
{
if (c != '.' && (c < '0' || c > '9'))
{
return false;
}
if (c=='.')
{
if (gotPoint)
{
return false;
}
gotPoint = true;
}
}
return true;
}
[...]


This is an excellent example of both exiting a loop early, and returning
from a method early. Thanks!
Nov 16 '05 #38

P: n/a
Michael Voss <mi**********@lvrREMOVE.deCAPS> wrote:
"Aaron Ackerman" wrote:
What is the sytax for exiting a for loop in C#?


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")...


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #39

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
read this http://groups.google.com/groups?hl=e...adm=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


I don't need to be persuaded that it's a popular book. That doesn't
mean that every line of it should be accepted without looking at the
arguments presented, does it?

I note that you haven't actually tried to really present the arguments
- you've just said that someone who is well respected made the
argument, and left it at that.

Why not address the actual issue, rather than just appealing to
authority? If his arguments stand up, present his arguments. If they
don't - well, then why should you accept them just because he makes
good points about *other* situations?

Why not address my specific example where I believe a for loop is more
readable than a while loop?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #40

P: n/a
Jon

the OP was quite specific - how do you exit a for loop? the answer is in
the header. if that doesn't work, or is too complicated, then use the while
loop. if you have specific reasons for otherwise, they are specific - keep
them to yourself and don't try to inject them as a norm or standard or as a
general excuse for not adhering to a style, esp when said style is
universally adopted, and well respected.

you seem to have a very strong sense of right and wrong, esp vis-a-vis
programming style - and you seem to be advocating your own set of style
guides. if that works for you, that's quite OK with me - I have no
misplaced perceptions of being able to convince you otherwise.

roy

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
read this

http://groups.google.com/groups?hl=e...adm=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


I don't need to be persuaded that it's a popular book. That doesn't
mean that every line of it should be accepted without looking at the
arguments presented, does it?

I note that you haven't actually tried to really present the arguments
- you've just said that someone who is well respected made the
argument, and left it at that.

Why not address the actual issue, rather than just appealing to
authority? If his arguments stand up, present his arguments. If they
don't - well, then why should you accept them just because he makes
good points about *other* situations?

Why not address my specific example where I believe a for loop is more
readable than a while loop?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #41

P: n/a
HI !
"Jon Skeet [C# MVP]" wrote:

[...snip...]
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.
I think, moving the loop into another method will lead to shorter methods in
most (if not close to all) cases and thus increase readability most of the
time - but that's just my opinion...
Just because other languages don't have a certain feature doesn't mean
that it shouldn't be used.


[...snip...]

Of course not; I was merely presenting another alternative to the op. But,
on the other hand, being given a certain feature by the designers of a
language does not make this feature the #1 choice in all of the cases (or
even in the majority of the cases), either.

Nov 16 '05 #42

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
the OP was quite specific - how do you exit a for loop? the answer is in
the header. if that doesn't work, or is too complicated, then use the while
loop. if you have specific reasons for otherwise, they are specific - keep
them to yourself and don't try to inject them as a norm or standard or as a
general excuse for not adhering to a style, esp when said style is
universally adopted, and well respected.
It's clearly *not* universally adopted though - I see breaks and
continues in for-loops all over the place.

Just because the style a book espouses is *in the main* well respected
doesn't mean that *every* part of it should be.

I really don't see why criticising a supposedly respected style by
bringing up a reasonable common counter-example should make you suggest
I should keep that to myself. My example wasn't particularly specific
to me - it's the kind of loop lots of people need to write, a lot of
the time.

Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?
you seem to have a very strong sense of right and wrong, esp vis-a-vis
programming style - and you seem to be advocating your own set of style
guides. if that works for you, that's quite OK with me - I have no
misplaced perceptions of being able to convince you otherwise.


I wasn't so much advocating my own set of style guides as suggesting
that the ones you were quoting should at least be backed up by
arguments better than "Well, it's in Code Complete, so it must be
right".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #43

P: n/a
Michael Voss <mi**********@lvrREMOVE.deCAPS> wrote:
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.


I think, moving the loop into another method will lead to shorter methods in
most (if not close to all) cases and thus increase readability most of the
time - but that's just my opinion...


Yes, it will almost always lead to more, shorter methods. That
*doesn't* automatically increase readability, however. If you end up
needing to pass in several variables, some of them by reference, some
not, etc, then it'll quite possibly end up lowering readability.

Just thinking up the name for the replacement method is quite a good
guideline, I find. If it really doesn't have a good description because
it only makes sense in the circumstance of the calling method anyway,
it may well *not* be a good candidate for refactoring. (This guideline
doesn't always work, of course.)

Keeping methods short is a good rule of thumb, until you end up with
hundreds of methods, each of which is only two lines long, and which
just call other methods. There's a balance to be struck. (I don't
believe you were particularly arguing against that though.)
Just because other languages don't have a certain feature doesn't mean
that it shouldn't be used.


[...snip...]

Of course not; I was merely presenting another alternative to the op. But,
on the other hand, being given a certain feature by the designers of a
language does not make this feature the #1 choice in all of the cases (or
even in the majority of the cases), either.


Absolutely. A language feature should stand on its own merit: not on
the grounds that:

a) It's been included in this language, so it must be good
b) It's been excluded from other languages, so it must be bad
c) The author of Code Complete doesn't like it, so it must be bad

Those are all "meta-reasons" - what I'm far more interested is
concrete, specific reasons why it should or shouldn't be used in
specific situations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #44

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
Why are you trying to stifle reasonable discussion of whether for loops with breaks in should always be avoided?
1) I am not trying to stifle


Really? What other purpose does a comment such as:

"if you have specific reasons for otherwise, they are specific - keep
them to yourself"


You cut and past at will -- here is the entire quote -
keep them to yourself and don't try to inject them as a norm
or standard or as a general excuse for not adhering
to a style, esp when said style is
universally adopted, and well respected.


AND DONT TRY TO INJECT THEM AS NORM OR STANDARD OR AS ....

You want to adopt your way because you have found a circumstance that make
sense. That's fine, but you must accept that it is then an exception, and
then you need to adopt exception handling - i.e. comments the code to
mitigate the complexities.

If you want your style to be generally adopted, then publish them and let
them stand the test of peer review. But keep in mind that your styles are
against currently accepted styles, so the burden will be substantially
higher; you have to first show that the existing standard is bad, and then
show that yours is clearly better. That is a VERY tough process.

You want to exchange jabs based on specifics, and as you noticed, I
steadfastly refuse - to make a proof for a generalized statement based on
specific examples, there would have literally thousands of examples - and
that is not the way a proof is done here.

I would gladly review any code and help you rework it to become more
supportable, based as a style guide - you dictate the style guide, and I'll
follow the rules. It seems that you either knew nothing of McConnell's work
or a priori dislike for it. If thats the case, I can accept that.

regards
roy fine
serve then? As soon as you start telling people to keep their opinions
to themselves during a discussion, you're stifling debate to my mind.
2) the discussion has not risen to the level of reasonable


That's because you've refused to engage with the meat of the
discussion, preferring to just recite the mantra of "do it because Code
Complete tells you to".
3) for loops with breaks in should be avoided - you want to make them
standard


And still you present no actual *reasons* - or indeed a more readable
while loop than my for loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #45

P: n/a

Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?


1) I am not trying to stifle
2) the discussion has not risen to the level of reasonable
3) for loops with breaks in should be avoided - you want to make them
standard
roy
Nov 16 '05 #46

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
Why are you trying to stifle reasonable discussion of whether for loops
with breaks in should always be avoided?
1) I am not trying to stifle


Really? What other purpose does a comment such as:

"if you have specific reasons for otherwise, they are specific - keep
them to yourself"

serve then? As soon as you start telling people to keep their opinions
to themselves during a discussion, you're stifling debate to my mind.
2) the discussion has not risen to the level of reasonable
That's because you've refused to engage with the meat of the
discussion, preferring to just recite the mantra of "do it because Code
Complete tells you to".
3) for loops with breaks in should be avoided - you want to make them
standard


And still you present no actual *reasons* - or indeed a more readable
while loop than my for loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #47

P: n/a
Jon Skeet [C# MVP] wrote:
I wasn't so much advocating my own set of style guides as suggesting
that the ones you were quoting should at least be backed up by
arguments better than "Well, it's in Code Complete, so it must be
right".


ok, my curiosity got the better of me, and I pulled my copy of Code
Complete of the shelf and had a look. Steve discusses Loop with exits
on page 326 and 327, he certainly doesn't advocate that they are not
used AFAICT, in fact he quotes the Software productvity consortium of
1989 that shows that in Ada this is the preferred kind of loop control
and that a student study showed that students scored 25% higher on a
comprehension test with this type of structure (Soloway, Bonar and
Ehrlich 1983)

For what it is worth, when Steve McConnell makes authoritive statements
in his book, they are always backed up by Hard Data, it is very rarely
a subjective thing. It's quite amazing to me that this book which is
now over 10 years old is still very relevant today I recommend it
highly. But I don't think anyone should become a slave to his
recommendations and conclusions, good judgment and experience count for
just as much as good texts.

Rgds Tim.
Nov 16 '05 #48

P: n/a
Roy Fine <rl****@twt.obfuscate.net> wrote:
"if you have specific reasons for otherwise, they are specific - keep
them to yourself"
You cut and past at will -- here is the entire quote -
keep them to yourself and don't try to inject them as a norm
or standard or as a general excuse for not adhering
to a style, esp when said style is
universally adopted, and well respected.


AND DONT TRY TO INJECT THEM AS NORM OR STANDARD OR AS ....


Yes, note the "and" here. You were telling me to keep my opinions to
myself. In other words, you were telling me to shut up and not to
criticise a "well accepted" practice.
You want to adopt your way because you have found a circumstance that make
sense. That's fine, but you must accept that it is then an exception, and
then you need to adopt exception handling - i.e. comments the code to
mitigate the complexities.
No, I *don't* believe it's an exception. I believe that quite
*frequently* using breaks (or returning from the for loop itself) leads
to more readable code than rewriting it as a while loop.
If you want your style to be generally adopted, then publish them and let
them stand the test of peer review.
It's not so much that I particularly want my style to be generally
adopted - I just take issue with a universal dictum that I should write
code in a particular way solely because someone else thinks it's a good
idea. If it's a good idea, say *why* it is.
But keep in mind that your styles are
against currently accepted styles, so the burden will be substantially
higher; you have to first show that the existing standard is bad, and then
show that yours is clearly better. That is a VERY tough process.
If it's so far against currently accepted styles, would it surprise you
if virtually every significant piece of published code contained breaks
in that style? I bet if we were to look through code for, say, Apache,
Tomcat, JBoss, Linux, ROTOR etc, we'd find plenty of examples which
break the rule.

Note that I certainly haven't proposed that while loops should be
avoided - just that you shouldn't *forbid* yourself from breaking out
of a for loop, as that can end up with more readable code than
converting the whole thing into a while loop.
You want to exchange jabs based on specifics, and as you noticed, I
steadfastly refuse - to make a proof for a generalized statement based on
specific examples, there would have literally thousands of examples - and
that is not the way a proof is done here.
If a rule does not *work* universally, it should not be *adopted*
universally, or espoused universally. It makes no sense to pretend I'm
the only one who ever has to write methods such as the one I posted.

A proof cannot be made *for* a style rule based on one specific example
- but if one specific example works *against* a style rule, that's
proof that it shouldn't be applied universally.
I would gladly review any code and help you rework it to become more
supportable, based as a style guide - you dictate the style guide, and I'll
follow the rules. It seems that you either knew nothing of McConnell's work
or a priori dislike for it. If thats the case, I can accept that.


I don't have an a priori dislike for it - I have a dislike for being
told *what* to do, but not *why* I should do it. I very much doubt that
McConnell fails to give reasons, so why are you so averse to sharing
them and debating them? Do you consider Code Complete to be "above"
peer review itself?

If you would gladly review code, then please review the code I've
already provided, and rewrite that according to McConnell's guide -
we'll see how readable it ends up.

(Of course, the whole of the above is based on the premise that
McConnell *does* universally forbid breaking out of for loops.
According to another post, he doesn't do so at all.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #49

P: n/a
Roy, I've never found a single comment from Jon to be unworthy of a listen,
even when I've disagreed strongly. I, like many here, value his OPINIONS on
many topics as much as you seem to value some book.

I never got the impression that Jon said the exception should be the rule as
you are trying to claim, so it seems at this point this is largely a
misunderstanding anyway.

Can we just leave this as Roy things standards should never have exceptions
and Jon does?

If you want my opinion (LOL), way back in high school I came up with this
"All rules have exceptions or get broken. The main difference is the former
remains under the control of the rule maker." I've seen this same kind of
(what I call) fanatic adherence to "patterns", where people think first and
foremost in terms of trying to fit every new problem they face into some
existing pattern.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Roy Fine <rl****@twt.obfuscate.net> wrote:
"if you have specific reasons for otherwise, they are specific - keep
them to yourself"


You cut and past at will -- here is the entire quote -
>keep them to yourself and don't try to inject them as a norm
>or standard or as a general excuse for not adhering
>to a style, esp when said style is
>universally adopted, and well respected.


AND DONT TRY TO INJECT THEM AS NORM OR STANDARD OR AS ....


Yes, note the "and" here. You were telling me to keep my opinions to
myself. In other words, you were telling me to shut up and not to
criticise a "well accepted" practice.

Nov 16 '05 #50

65 Replies

This discussion thread is closed

Replies have been disabled for this discussion.