Style Poll: Hand-Rolling for() Loops

Discussion in 'C++' started by Scott Brady Drummonds, Dec 14, 2005.

  1. 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.
    Scott Brady Drummonds, Dec 14, 2005
    #1
    1. Advertising

  2. Scott Brady Drummonds

    Guest

    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, 2005
    #2
    1. Advertising

  3. Scott Brady Drummonds

    Mark P Guest

    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
    Mark P, Dec 14, 2005
    #3
  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;
    >> }
    >>
    >> 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
    Victor Bazarov, Dec 14, 2005
    #4
  5. Scott Brady Drummonds

    Guest

    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, 2005
    #5
  6. Scott Brady Drummonds

    Guest

    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, 2005
    #6
  7. Scott Brady Drummonds

    Chris Theis Guest

    "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
    Chris Theis, Dec 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon

    Outlook style left hand menu bar

    Simon, May 30, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    687
    bruce barker
    May 30, 2007
  2. birdsong

    select.poll.poll() never blocks

    birdsong, Feb 12, 2009, in forum: Python
    Replies:
    2
    Views:
    441
    birdsong
    Feb 12, 2009
  3. Jean-Paul Calderone

    Re: select.poll.poll() never blocks

    Jean-Paul Calderone, Feb 12, 2009, in forum: Python
    Replies:
    3
    Views:
    431
    birdsong
    Feb 12, 2009
  4. Ken Varn
    Replies:
    0
    Views:
    428
    Ken Varn
    Apr 26, 2004
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page