Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Loops and compitency of recent CS grads (http://www.velocityreviews.com/forums/t449420-loops-and-compitency-of-recent-cs-grads.html)

Josh Mcfarlane 10-28-2005 04:08 PM

Loops and compitency of recent CS grads
 
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 10-28-2005 05:58 PM

Re: Loops and compitency of recent CS grads
 

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 10-28-2005 06:55 PM

Re: Loops and compitency of recent CS grads
 
int2str@gmail.com 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 10-28-2005 06:57 PM

Re: Loops and compitency of recent CS grads
 
On 2005-10-28, int2str@gmail.com <int2str@gmail.com> 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 10-28-2005 06:59 PM

Re: Loops and compitency of recent CS grads
 
On 2005-10-28, Josh Mcfarlane <darsant@gmail.com> 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 10-28-2005 07:04 PM

Re: Loops and compitency of recent CS grads
 

Neil Cerutti wrote:
> On 2005-10-28, Josh Mcfarlane <darsant@gmail.com> 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 10-28-2005 07:14 PM

Re: Loops and compitency of recent CS grads
 

"Josh Mcfarlane" <darsant@gmail.com> wrote in message
news:1130526242.361790.83620@o13g2000cwo.googlegro ups.com...
>
> Neil Cerutti wrote:
> > On 2005-10-28, Josh Mcfarlane <darsant@gmail.com> 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 10-28-2005 07:32 PM

Re: Loops and compitency of recent CS grads
 

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 10-28-2005 07:50 PM

Re: Loops and compitency of recent CS grads
 
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 10-28-2005 07:52 PM

Re: Loops and compitency of recent CS grads
 
Josh Mcfarlane sade:
> Neil Cerutti wrote:
>
>>On 2005-10-28, Josh Mcfarlane <darsant@gmail.com> 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


All times are GMT. The time now is 04:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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