Velocity Reviews > C++ > Loops and compitency of recent CS grads

# Loops and compitency of recent CS grads

Josh Mcfarlane
Guest
Posts: n/a

 10-28-2005
Just sort of curious because of a trend I've been noticing.

Around here, most of the newer students and even some professionals
that pick up C++ or another similar language tend to do inefficient
things with loops / iterations, such as:

array[0] = 0;
for (int i =1; i < size; ++i)
{
array[i] = i;
}

or my favorite
for (int i = 0; i < 2*size; ++i)
{
if (i % 2 == 0)
dofunct(array[i/2]);
else
dootherfunct(array[i/2]);
}

Is proper iteration really that hard of a concept to grasp?

int2str@gmail.com
Guest
Posts: n/a

 10-28-2005

It's "competency"

Josh Mcfarlane wrote:
> array[0] = 0;
> for (int i =1; i < size; ++i)
> {
> array[i] = i;
> }

I may be outing myself as incompetent here, but what's wrong with this
loop?

Cheers,
Andre

Josh Mcfarlane
Guest
Posts: n/a

 10-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> It's "competency"
>
> Josh Mcfarlane wrote:
> > array[0] = 0;
> > for (int i =1; i < size; ++i)
> > {
> > array[i] = i;
> > }

>
> I may be outing myself as incompetent here, but what's wrong with this
> loop?

Sorry for the mispelling. =P

Basically, in this example it may have been misleading, but I see code
where they duplicate the initial value.

A better example may have been

array[0] += x
for (int i = 1; i < size; ++i)
{
array[i] += x;
}

Or some other common operation that needs to be performed on the entire
iteration. To me, it's code duplication and more of a hassle to
maintain code where the initial iteration (0) is outside of the loop,
while the remaining iterations are inside.

Josh McFarlane

Neil Cerutti
Guest
Posts: n/a

 10-28-2005
On 2005-10-28, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> It's "competency"
>
> Josh Mcfarlane wrote:
>> array[0] = 0;
>> for (int i =1; i < size; ++i)
>> {
>> array[i] = i;
>> }

>
> I may be outing myself as incompetent here, but what's wrong with this
> loop?

He may wish it were the more idiomatic:

for (int i = 0; i < size; ++i)
{
array[i] = i;
}

If I were to see the original code, with

for (int i = 1;

at the beginning, little alarms would start going off in my head,
and I'd have to waste time proving to myself it wasn't wrong.

--
Neil Cerutti

Neil Cerutti
Guest
Posts: n/a

 10-28-2005
On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> Just sort of curious because of a trend I've been noticing.
>
> Around here, most of the newer students and even some professionals
> that pick up C++ or another similar language tend to do inefficient
> things with loops / iterations, such as:

> or my favorite
> for (int i = 0; i < 2*size; ++i)
> {
> if (i % 2 == 0)
> dofunct(array[i/2]);
> else
> dootherfunct(array[i/2]);
> }
>
> Is proper iteration really that hard of a concept to grasp?

The second loop just looks like nonsense. I can't think of an
improvement other than erasing it.

--
Neil Cerutti

Josh Mcfarlane
Guest
Posts: n/a

 10-28-2005

Neil Cerutti wrote:
> On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> > Just sort of curious because of a trend I've been noticing.
> >
> > Around here, most of the newer students and even some professionals
> > that pick up C++ or another similar language tend to do inefficient
> > things with loops / iterations, such as:

>
> > or my favorite
> > for (int i = 0; i < 2*size; ++i)
> > {
> > if (i % 2 == 0)
> > dofunct(array[i/2]);
> > else
> > dootherfunct(array[i/2]);
> > }
> >
> > Is proper iteration really that hard of a concept to grasp?

>
> The second loop just looks like nonsense. I can't think of an
> improvement other than erasing it.

It baffled me too, but more or less what they were trying to achieve
was

for(int i = 0; i < size; ++i)
{
dofunct(array[i]);
dootherfunct(array[i]);
}

Now, why they didn't do that, I have no clue.

Dave Townsend
Guest
Posts: n/a

 10-28-2005

"Josh Mcfarlane" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> Neil Cerutti wrote:
> > On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> > > Just sort of curious because of a trend I've been noticing.
> > >
> > > Around here, most of the newer students and even some professionals
> > > that pick up C++ or another similar language tend to do inefficient
> > > things with loops / iterations, such as:

> >
> > > or my favorite
> > > for (int i = 0; i < 2*size; ++i)
> > > {
> > > if (i % 2 == 0)
> > > dofunct(array[i/2]);
> > > else
> > > dootherfunct(array[i/2]);
> > > }
> > >
> > > Is proper iteration really that hard of a concept to grasp?

> >
> > The second loop just looks like nonsense. I can't think of an
> > improvement other than erasing it.

>
> It baffled me too, but more or less what they were trying to achieve
> was
>
> for(int i = 0; i < size; ++i)
> {
> dofunct(array[i]);
> dootherfunct(array[i]);
> }
>
> Now, why they didn't do that, I have no clue.
>

Your original post was about efficiency, but you seem to be commenting on
clarity
which is quite a different matter.

Josh Mcfarlane
Guest
Posts: n/a

 10-28-2005

Dave Townsend wrote:
> Your original post was about efficiency, but you seem to be commenting on
> clarity
> which is quite a different matter.

Well, by efficiency I was more referring to the efficiency of the
programmer and the overall effect it had on the code / program
development (in this case, it made debugging it more pesky as I had to
check two areas instead of one.)

Adding a logic check each loop and looping twice as much as necessary
seems inefficient to me as you're executing 2x the instructions
necessary.

=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a

 10-28-2005
Josh Mcfarlane wrote:

> Around here, most of the newer students and even some professionals
> that pick up C++ or another similar language tend to do inefficient
> things with loops / iterations, such as:

I have seen many bad code over the years, don't think is a recent trend. In
C++, C, Java, Pascal, Basic....

--
Salu2

TIT
Guest
Posts: n/a

 10-28-2005
> Neil Cerutti wrote:
>
>>On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
>>
>>>Just sort of curious because of a trend I've been noticing.
>>>
>>>Around here, most of the newer students and even some professionals
>>>that pick up C++ or another similar language tend to do inefficient
>>>things with loops / iterations, such as:

>>
>>>or my favorite
>>>for (int i = 0; i < 2*size; ++i)
>>>{
>>>if (i % 2 == 0)
>>>dofunct(array[i/2]);
>>>else
>>>dootherfunct(array[i/2]);
>>>}
>>>
>>>Is proper iteration really that hard of a concept to grasp?

>>
>>The second loop just looks like nonsense. I can't think of an
>>improvement other than erasing it.

>
>
> It baffled me too, but more or less what they were trying to achieve
> was
>
> for(int i = 0; i < size; ++i)
> {
> dofunct(array[i]);
> dootherfunct(array[i]);
> }
>
> Now, why they didn't do that, I have no clue.
>

A complex mind lacks simple thoughts =)

TIT