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

Style Poll: Hand-Rolling for() Loops

P: n/a
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:

i = 0;
while (i < n)
{
...
++i;
}

My initial reaction to seeing a while() loop is that the developer is doing
something *other* than one would have done with a for() loop. That is,
perhaps there is a special termination condition in the loop. Perhaps the
index is changed. But in this case the developer was doing absolutely
nothing different than the equivalent for() loop would have done. He had
just chosen to use a while() loop. As such, his implementation was
difficult for me to read.

So, I brought this up and the developer's position was that I was being
picky. He thought that hand-rolling his own for() loops was a reasonable
demonstration of an acceptable difference in style. Since the two of us
disagreed I thought I'd poll any readers on this group that felt like
responding.

Do you think that this type of structure is reasonable code? Or do you
think that the "common" implementation should be mandated? Am I being too
picky and should I just chill out?

Thanks!
Scott

--
Remove .nospam from my e-mail address to contact me.
Dec 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Scott Brady Drummonds wrote:
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:

i = 0;
while (i < n)
{
...
++i;
}

My initial reaction to seeing a while() loop is that the developer is doing
something *other* than one would have done with a for() loop. That is,
perhaps there is a special termination condition in the loop. Perhaps the
index is changed. But in this case the developer was doing absolutely
nothing different than the equivalent for() loop would have done. He had
just chosen to use a while() loop. As such, his implementation was
difficult for me to read.

So, I brought this up and the developer's position was that I was being
picky. He thought that hand-rolling his own for() loops was a reasonable
demonstration of an acceptable difference in style. Since the two of us
disagreed I thought I'd poll any readers on this group that felt like
responding.
What does he think for loops are for then? If the number of iterations
of the loop can be calculated at the outset, that's what for loops were
invented for. If the decision whether to end the loop depends on what
happens in the loop, that's what while loops were invented for.
Do you think that this type of structure is reasonable code? Or do you
think that the "common" implementation should be mandated? Am I being too
picky and should I just chill out?


No I don't think it's reasonable. If I was in charge of the code review
I would change the code. But I would also try and convince the
developer to change his mind and follow the common idiom precisely
because it is the common idiom.

However, to answer you're final question, yes you should just chill
out. Not because you are wrong, but because you must have more
important things to worry about in your job. Deeming whether the code
is acceptable is your boss's job.

Gavin Deane

Dec 14 '05 #2

P: n/a
Scott Brady Drummonds wrote:
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:

i = 0;
while (i < n)
{
...
++i;
}

My initial reaction to seeing a while() loop is that the developer is doing
something *other* than one would have done with a for() loop. That is,
perhaps there is a special termination condition in the loop. Perhaps the
index is changed. But in this case the developer was doing absolutely
nothing different than the equivalent for() loop would have done. He had
just chosen to use a while() loop. As such, his implementation was
difficult for me to read.

So, I brought this up and the developer's position was that I was being
picky. He thought that hand-rolling his own for() loops was a reasonable
demonstration of an acceptable difference in style. Since the two of us
disagreed I thought I'd poll any readers on this group that felt like
responding.

Do you think that this type of structure is reasonable code? Or do you
think that the "common" implementation should be mandated? Am I being too
picky and should I just chill out?


I see nothing wrong with your coworker's style. Personally I prefer
for-loops because they're more compact and because variables declared in
the initializer statement are local to the for loop (an advantage you
might want to point out to your coworker). Mandating such a style
however seems Draconian and unnecessary.

Mark
Dec 14 '05 #3

P: n/a
Mark P wrote:
Scott Brady Drummonds wrote:
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my
coworkers never used for() loops. Instead, he would use while()
loops such as the following:

i = 0;
while (i < n)
{
...
++i;
}

My initial reaction to seeing a while() loop is that the developer
is doing something *other* than one would have done with a for()
loop. That is, perhaps there is a special termination condition in
the loop. Perhaps the index is changed. But in this case the
developer was doing absolutely nothing different than the equivalent
for() loop would have done. He had just chosen to use a while()
loop. As such, his implementation was difficult for me to read.

So, I brought this up and the developer's position was that I was
being picky. He thought that hand-rolling his own for() loops was a
reasonable demonstration of an acceptable difference in style. Since the
two of us disagreed I thought I'd poll any readers on this
group that felt like responding.

Do you think that this type of structure is reasonable code? Or do
you think that the "common" implementation should be mandated? Am I
being too picky and should I just chill out?


I see nothing wrong with your coworker's style. Personally I prefer
for-loops because they're more compact and because variables declared
in the initializer statement are local to the for loop (an advantage
you might want to point out to your coworker). Mandating such a style
however seems Draconian and unnecessary.


I don't agree. Readability and maintainability is paramount AFA styles
are concerned. Not only such loop is difficult to comprehend because
to see the increment one needs to look at the end of the body of the
loop (instead of at the end of the line in a 'for' statement), but the
behaviour of the loop is going to be drastically different if someone
later decides to introduce a 'continue' somewhere in the middle.

V
Dec 14 '05 #4

P: n/a

Mark P wrote:
Scott Brady Drummonds wrote:
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:

i = 0;
while (i < n)
{
...
++i;
}

<snip>
I see nothing wrong with your coworker's style. Personally I prefer
for-loops because they're more compact and because variables declared in
the initializer statement are local to the for loop (an advantage you
might want to point out to your coworker). Mandating such a style
however seems Draconian and unnecessary.


In a professional environment with several developers working on the
same code base, your source code is how you communicate your intentions
to other developers. "How easily will everyone else understand what I'm
doing" is an extremely important question and one you should have in
mind at all times.

The fact that the while loop form has some technical disadvantages
(loop control variable scope you mention, maintainability problems
Victor Bazarov suggested) is certainly relevant, but almost more
important is the fact that it is simply not the clearest way of
expressing that intention.

Gavin Deane

Dec 14 '05 #5

P: n/a

Scott Brady Drummonds wrote:
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:

i = 0;
while (i < n)
{
...
++i;
}


If you do that, the question comes up why you don't write the goto
statements
that the compiler emits. Basically, he removes one level of abstraction
from a
for-loop, and leaves another one below.

BTW, for the same reason I'd flag a for-loop when an STL function is
more
appropriate. It confuses readers, as they can't find the special
circumstances
that prohibit the use of a ready-made solution.

HTH,
Michiel Salters

Dec 14 '05 #6

P: n/a

"Scott Brady Drummonds" <sc**********************@intel.com> wrote in
message news:dn**********@news01.intel.com...
Hi, everyone,

I was in a code review a couple of days ago and noticed one of my
coworkers never used for() loops. Instead, he would use while() loops
such as the following:

i = 0;
while (i < n)
{
...
++i;
}

My initial reaction to seeing a while() loop is that the developer is
doing something *other* than one would have done with a for() loop. That
is, perhaps there is a special termination condition in the loop. Perhaps
the index is changed. But in this case the developer was doing absolutely
nothing different than the equivalent for() loop would have done. He had
just chosen to use a while() loop. As such, his implementation was
difficult for me to read.

So, I brought this up and the developer's position was that I was being
picky. He thought that hand-rolling his own for() loops was a reasonable
demonstration of an acceptable difference in style. Since the two of us
disagreed I thought I'd poll any readers on this group that felt like
responding.

Do you think that this type of structure is reasonable code? Or do you
think that the "common" implementation should be mandated? Am I being too
picky and should I just chill out?

[SNIP]

Well, if reasonable = working then it's fine but for & while loops have
different intentions, even if the outcome can be the same. Style is
certainly different and arguable but IMHO your fellow developer is wrong
there. Despite the arguments of readability there is the issue of
optimization. Depending on the context and on the compiler you might easily
end up with different results regarding optimization as for loops can be
much easier to handle there.

Cheers
Chris

Dec 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.