Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Style Poll: Hand-Rolling for() Loops

Reply
Thread Tools

Style Poll: Hand-Rolling for() Loops

 
 
Scott Brady Drummonds
Guest
Posts: n/a
 
      12-14-2005
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.


 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-14-2005

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

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      12-14-2005
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-14-2005
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


 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-14-2005

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

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      12-14-2005

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

 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      12-14-2005

"Scott Brady Drummonds" <> wrote in
message news:dnno80$6d1$...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
elegant python style for loops ian.team.python@saltmob.com Python 8 05-10-2007 08:34 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
1.5 style "for" loops Chanchal Java 5 11-28-2005 05:17 PM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57