![]() |
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? |
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 |
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 |
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 |
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 |
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. |
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. |
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. |
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 |
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.