Loops and compitency of recent CS grads

Discussion in 'C++' started by Josh Mcfarlane, Oct 28, 2005.

  1. 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;
    }

    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?
    Josh Mcfarlane, Oct 28, 2005
    #1
    1. Advertising

  2. Josh Mcfarlane

    Guest

    It's "competency" ;)

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


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

    Cheers,
    Andre
    , Oct 28, 2005
    #2
    1. Advertising

  3. wrote:
    > It's "competency" ;)
    >
    > Josh Mcfarlane wrote:
    > > array[0] = 0;
    > > for (int i =1; i < size; ++i)
    > > {
    > > array = 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 += 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
    Josh Mcfarlane, Oct 28, 2005
    #3
  4. Josh Mcfarlane

    Neil Cerutti Guest

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

    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, Oct 28, 2005
    #4
  5. Josh Mcfarlane

    Neil Cerutti Guest

    On 2005-10-28, Josh Mcfarlane <> 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
    Neil Cerutti, Oct 28, 2005
    #5
  6. Neil Cerutti wrote:
    > On 2005-10-28, Josh Mcfarlane <> 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);
    dootherfunct(array);
    }

    Now, why they didn't do that, I have no clue.
    Josh Mcfarlane, Oct 28, 2005
    #6
  7. "Josh Mcfarlane" <> wrote in message
    news:...
    >
    > Neil Cerutti wrote:
    > > On 2005-10-28, Josh Mcfarlane <> 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);
    > dootherfunct(array);
    > }
    >
    > 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.
    Dave Townsend, Oct 28, 2005
    #7
  8. 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.
    Josh Mcfarlane, Oct 28, 2005
    #8
  9. 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
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 28, 2005
    #9
  10. Josh Mcfarlane

    TIT Guest

    Josh Mcfarlane sade:
    > Neil Cerutti wrote:
    >
    >>On 2005-10-28, Josh Mcfarlane <> 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);
    > dootherfunct(array);
    > }
    >
    > Now, why they didn't do that, I have no clue.
    >


    A complex mind lacks simple thoughts =)

    TIT
    TIT, Oct 28, 2005
    #10
  11. TIT wrote:
    > A complex mind lacks simple thoughts =)


    Sometimes, but rarely do these over-the-top cases work as intended,
    which is part of what brought about this post.
    Josh Mcfarlane, Oct 28, 2005
    #11
  12. Josh Mcfarlane wrote:
    > TIT wrote:
    > > A complex mind lacks simple thoughts =)

    >
    > Sometimes, but rarely do these over-the-top cases work as intended,
    > which is part of what brought about this post.

    Why don't you show us a sample of your code? Thus, the coding problems
    may be inherent to a completely different source. Additionally, your
    English writing doesn't reflect any teacher/professorial
    attributes...can't say no more, for it is already off-topic...
    puzzlecracker, Oct 29, 2005
    #12
  13. Josh Mcfarlane

    Ian Guest

    Josh Mcfarlane 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:
    >

    Many people "know" a language, which often means one way of doing
    something and they are often unwilling to experiment or expand their
    knowledge. I've seen this a lot over the years and not just with junior
    staff.

    Ian
    Ian, Oct 29, 2005
    #13
  14. puzzlecracker wrote:
    > Why don't you show us a sample of your code? Thus, the coding problems
    > may be inherent to a completely different source. Additionally, your
    > English writing doesn't reflect any teacher/professorial
    > attributes...can't say no more, for it is already off-topic...


    This wasn't about my code. This was about a common problem I noticed
    with loops and some of the programmers around here. I end up having to
    go in and fix things, not because they're unclear in meaning, but in
    attempting to be complex or doing things in the first mentioned
    example, they screw something up (such as record iteration) and it
    brings in undefined behavior.
    Josh Mcfarlane, Oct 29, 2005
    #14
  15. Josh Mcfarlane

    Jim Langston Guest

    "Josh Mcfarlane" <> wrote in message
    news:...
    > 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;
    > }


    A few ways this could come about. Some new programmer trying to think how
    to do something to a variable. "lets see. I want to initialize it to 0.
    That would be X = 0. Oh, X is an array, so I need X[0] = 0. There got it.
    Oh, gotta do the rest. lets see, already did 0 so..."

    Or, most likely. "Ahh, let me initialize my size variable array. for (int
    i = 1; i < size; ++i)" later "Dang, what bug, oh, forgot to initialize 0.
    Umm.. I'll just throw it up top."

    >
    > 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?
    >


    Actually, for people new to programming iteration is a total mystery. They
    still have a hard time understanding how X = X + 1 can be true when their
    algebra teacher told them both sides need to be equal...

    Just unlearning a lot of things and learning back over.

    I remember going through this phase many many years ago. When I didn't even
    understand 2/3 of what I wrote, but it worked dag nab it so it must be
    right! I better not touch it or I might break it again...
    Jim Langston, Oct 29, 2005
    #15
  16. Josh Mcfarlane

    TIT Guest

    Josh Mcfarlane sade:
    > 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:
    >


    I doubt that you'll only find irritating code in iterations. The
    recent decade of mass production of programmers may have led to
    a decay of the avarage programmers capabilities.

    I once accused a c++ teacher at a university in 2001 for
    teaching the concept of pointers way too late, and he replied:
    "You know, in Java you don't even have pointers."

    TIT
    TIT, Oct 29, 2005
    #16
    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. Matthew Superstar Swass

    Opinions on college grads in this field

    Matthew Superstar Swass, Jul 28, 2004, in forum: HTML
    Replies:
    4
    Views:
    363
    Shailesh Humbad
    Jul 28, 2004
  2. diehard4.0

    Dads & Grads Superstore

    diehard4.0, May 21, 2009, in forum: C Programming
    Replies:
    0
    Views:
    265
    diehard4.0
    May 21, 2009
  3. diehard4.0

    Dads & Grads Superstore

    diehard4.0, May 21, 2009, in forum: Python
    Replies:
    0
    Views:
    235
    diehard4.0
    May 21, 2009
  4. fighter

    Dads & Grads Superstore

    fighter, May 21, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    fighter
    May 21, 2009
  5. diehard4.0

    Dads & Grads Superstore

    diehard4.0, May 22, 2009, in forum: HTML
    Replies:
    0
    Views:
    309
    diehard4.0
    May 22, 2009
Loading...

Share This Page