Style Poll: Hand-Rolling for() Loops

  • Thread starter Scott Brady Drummonds
  • Start date
S

Scott Brady Drummonds

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
 
D

deane_gavin

Scott said:
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
 
M

Mark P

Scott said:
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
 
V

Victor Bazarov

Mark said:
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
 
D

deane_gavin

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
 
M

Michiel.Salters

Scott said:
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
 
C

Chris Theis

Scott Brady Drummonds said:
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
 

Ask a Question

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.

Ask a Question

Similar Threads

loops 1
Performance of hand-optimised assembly 99
rolling frame (3) 3
Optimisation for tight loops... 18
noob question on loops 16
Famous Emacs People With Hand Injuries 0
fun with nested loops 14
Stylistic note on loops 221

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top