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
|