473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Style Poll: Hand-Rolling for() Loops

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
6 1692

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
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
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

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim | last post by:
Hi, I noticed with popen2.Popen4 class, sometimes the poll() method will keep returning -1 although the command has finished. However, when a read is done ( ex. p4instance.fromchild.read() )...
14
by: Zac Hester | last post by:
I figured since a lot of us around here design sites for "customers" a lot, I'd ask a general question that might help a lot of us in the future. When dealing with clients asking for...
18
by: Randall Sell | last post by:
http://www.bytewise.com.au/test/iebug.html I can not see why this doesn't work under IE6. Perhaps someone can shed some light on this, and a possible hack to get it to work. My ultimate goal is...
1
by: ETYC | last post by:
I am not sure if this is a bug or not but it seems strange to me. If I set the tcp socket blocking to false and do a connect, then I can catch the socketexception. If the exception is...
2
by: Raed Sawalha | last post by:
I have the following table in ascx , when I click the button the table style not showing in the popup , it is ONLY showing on the page not in the popup...WHY? <TABLE style="BACKGROUND:...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
0
by: jinfeng_Wang | last post by:
I have writen the following souce code : m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Socket.Blocking = false; try {...
2
by: webcm123 | last post by:
I'm making some changes in poll module. I don't know which method of storing options of poll is better. The main data of polls are in POLLS table. Speed and efficiency is the most important...
1
by: ryezack | last post by:
i have an image map with an onmouseover event thats calls a javascript function style cursor is hand. this works in IE but not in firefox. here's the code: <script type="text/javascript"> <!--...
4
by: 7stud | last post by:
Hi, What is the difference between: 1) getting the returncode directly from the subprocess object 2) calling poll() on the subprocess object? Here is an example:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.