"Continue" usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdh

    "Continue" usage

    May I ask the group this somewhat non-focused question....hav ing now
    seen "continue" used in some of the solutions I have worked on. ( Ex
    7-4 solution by Tondo and Gimpel comes to mind)
    Is there a good principle/s for the good usage of "continue"...or ...do
    any of the regular/irregular contributors have a "favorite" way in
    which it is particularly useful. Thanks as usual.
  • Richard Heathfield

    #2
    Re: "Continue& quot; usage

    mdh said:
    May I ask the group this somewhat non-focused question....hav ing now
    seen "continue" used in some of the solutions I have worked on. ( Ex
    7-4 solution by Tondo and Gimpel comes to mind)
    Is there a good principle/s for the good usage of "continue"...or ...do
    any of the regular/irregular contributors have a "favorite" way in
    which it is particularly useful. Thanks as usual.
    Personally, I prefer to reserve it as an indicator that an otherwise empty
    loop is intended to be empty:

    for(t = s; *s != '\0'; (*s != c) ? *t++ = *s++ : *s++)
    {
    continue;
    }
    *t = '\0';

    Another reasonable view is that it should be used for quickly dispensing
    with exceptional situations encountered in a loop where you don't want to
    do any processing on this iteration (but don't want to stop the loop):

    while(line_read ing_function(&l ine, &len, fp) != EOF)
    {
    skipwhite(&line );
    if(line[0] == '\0')
    {
    continue; /* empty line */
    }
    if(line[0] == '#')
    {
    continue; /* comment encountered */
    }

    /* now we can get on with processing "real" lines */

    Comment

    • mdh

      #3
      Re: "Continue& quot; usage

      On Sep 21, 8:39 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      mdh said:
      >
      May I ask the group
      .....having now
      seen "continue" used
      Is there a good principle/s for the good usage of "continue"...or ...do
      any of the regular/irregular contributors have a "favorite" way in
      which it is particularly useful.  
      >
      Personally, I prefer to reserve it as an indicator that an otherwise empty
      loop is intended to be empty:
      >
      for(t = s; *s != '\0'; (*s != c) ? *t++ = *s++ : *s++)
      {
        continue;}
      >
      *t = '\0';
      >
      Another reasonable view is that it should be used for quickly dispensing
      with exceptional situations encountered in a loop where you don't want to
      do any processing on this iteration (but don't want to stop the loop):
      >
      while(line_read ing_function(&l ine, &len, fp) != EOF)
      {
        skipwhite(&line );
        if(line[0] == '\0')
        {
          continue; /* empty line */
        }
        if(line[0] == '#')
        {
          continue; /* comment encountered */
        }
      >
        /* now we can get on with processing "real" lines */
        .
       
      thanks Richard for those "hooks" to hang that idea on.

      Comment

      • CBFalconer

        #4
        Re: &quot;Continue& quot; usage

        mdh wrote:
        >
        May I ask the group this somewhat non-focused question....hav ing
        now seen "continue" used in some of the solutions I have worked
        on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind). Is
        there a good principle/s for the good usage of "continue" or do
        any of the regular/irregular contributors have a "favorite" way
        in which it is particularly useful. Thanks as usual.
        I primarily use it to indicate an empty statement. It is not
        limited to that however. For example:

        while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.

        Comment

        • Richard Heathfield

          #5
          Re: &quot;Continue& quot; usage

          CBFalconer said:
          mdh wrote:
          >>
          >May I ask the group this somewhat non-focused question....hav ing
          >now seen "continue" used in some of the solutions I have worked
          >on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind). Is
          >there a good principle/s for the good usage of "continue" or do
          >any of the regular/irregular contributors have a "favorite" way
          >in which it is particularly useful. Thanks as usual.
          >
          I primarily use it to indicate an empty statement. It is not
          limited to that however. For example:
          >
          while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
          Your example is fine, but your description is not, because you can't always
          use continue to indicate an empty statement - *only* in loop contexts and
          *only* where your intent is for the loop to continue from the
          loop-continuation portion of the smallest enclosing iteration statement.
          For example:

          int main(void)
          {
          continue;
          return 0;
          }

          is a constraint violation requiring a diagnostic message. Furthermore, the
          following example illustrates *legal* C where the use of continue to
          indicate a null statement gives the wrong code for the job:

          #include <stdio.h>

          int main(void)
          {
          int ch;
          while((ch = getchar()) != EOF)
          {
          if(isalpha(ch))
          {
          if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
          {
          continue; /* bad idea */
          }
          else
          {
          /* consonant-specific processing - non-aeiou */
          }
          /* letter-specific processing - for all a-z */
          }
          }
          return 0;
          }

          In this code, vowels will *not* receive letter-specific processing.

          So no, using it to indicate an empty statement is not a good idea. Using it
          to indicate an empty loop body is obviously fine, however.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Chris Dollin

            #6
            Re: &quot;Continue& quot; usage

            mdh wrote:
            May I ask the group this somewhat non-focused question....hav ing now
            seen "continue" used in some of the solutions I have worked on. ( Ex
            7-4 solution by Tondo and Gimpel comes to mind)
            Is there a good principle/s for the good usage of "continue"...or ...do
            any of the regular/irregular contributors have a "favorite" way in
            which it is particularly useful. Thanks as usual.
            I have seen `continue` used sensibly [1] so rarely
            that I treat any use of it
            as a code smell
            suggesting
            that there is an opportunity
            to fix the design
            in a way that eliminates any need
            to consider continue for that code.

            To indicate an empty loop body
            I find `{}` perfectly adequate
            on those odd occasions
            when it is needed.

            Mind the gap.

            [1] From my POV, natch.

            --
            'It changed the future .. and it changed us.' /Babylon 5/

            Hewlett-Packard Limited registered office: Cain Road, Bracknell,
            registered no: 690597 England Berks RG12 1HN

            Comment

            • Richard Tobin

              #7
              Re: &quot;Continue& quot; usage

              In article <f4udnSP9FqvLxU rVnZ2dnUVZ8qPin Z2d@bt.com>,
              Richard Heathfield <rjh@see.sig.in validwrote:
              >Your example is fine, but your description is not, because you can't always
              >use continue to indicate an empty statement - *only* in loop contexts and
              >*only* where your intent is for the loop to continue from the
              >loop-continuation portion of the smallest enclosing iteration statement.
              Unlike Fortran, where CONTINUE was a no-op, and was often used at the
              end of loops because it could reliably be used to hang the line number
              on.

              -- Richard
              --
              Please remember to mention me / in tapes you leave behind.

              Comment

              • Richard

                #8
                Re: &quot;Continue& quot; usage

                Chris Dollin <chris.dollin@h p.comwrites:
                mdh wrote:
                >
                >May I ask the group this somewhat non-focused question....hav ing now
                >seen "continue" used in some of the solutions I have worked on. ( Ex
                >7-4 solution by Tondo and Gimpel comes to mind)
                >Is there a good principle/s for the good usage of "continue"...or ...do
                >any of the regular/irregular contributors have a "favorite" way in
                >which it is particularly useful. Thanks as usual.
                >
                I have seen `continue` used sensibly [1] so rarely
                that I treat any use of it
                as a code smell
                suggesting
                that there is an opportunity
                to fix the design
                in a way that eliminates any need
                to consider continue for that code.
                >
                To indicate an empty loop body
                I find `{}` perfectly adequate
                on those odd occasions
                when it is needed.
                >
                Mind the gap.
                >
                [1] From my POV, natch.
                Actually it makes perfect sense when you have your gate condition at the
                top of the section - far easier to read, understand and maintain in many
                cases (not all of course).

                while(n--){

                if(complexCondi tionNotMatched( n))
                continue; /* not interested. Check the next if there is one. */

                /*do processing of things we are interested in*/

                }

                Now we could invert the test but then we need to scroll down to the end
                of the function to be see what else happens after the block. Here we see
                that nothing happens at all - we must move to the next element.

                Seems clear, maintainable and decent design to me. I fail to see how it
                could be construed as "bad" in any shape or form.

                Comment

                • Eric Sosman

                  #9
                  Re: &quot;Continue& quot; usage

                  mdh wrote:
                  May I ask the group this somewhat non-focused question....hav ing now
                  seen "continue" used in some of the solutions I have worked on. ( Ex
                  7-4 solution by Tondo and Gimpel comes to mind)
                  Is there a good principle/s for the good usage of "continue"...or ...do
                  any of the regular/irregular contributors have a "favorite" way in
                  which it is particularly useful. Thanks as usual.
                  Hi, my name is Eric, and I'm a `continue' abuser. I use
                  it as a disguised `goto', simply to avoid excessive nesting:

                  for (...) {
                  ... prepare for a test ...
                  if (not_applicable )
                  continue;
                  ... prepare another test ...
                  if (not_applicable _2)
                  continue;
                  ... payload ...
                  }

                  Inverting the sense of the tests would eliminate my abusive
                  `continue' statements:

                  for (...) {
                  ... prepare for a test ...
                  if (! not_applicable) {
                  ... prepare another test ...
                  if (! not_applicable_ 2) {
                  ... payload ...
                  }
                  }
                  }

                  .... but if there are three or four such tests, as often
                  happens when you're converting and validating input from
                  an uncontrolled source, the "payload" -- the real purpose
                  of the function --
                  tends to get squee-
                  zed into short
                  little lines all
                  crowded at the right-
                  hand side and larded
                  with ugly line bre-
                  aks.

                  So although I know in my heart of hearts that I overuse
                  `continue' and really shouldn't, the way I really shouldn't
                  drink and smoke and gamble on cockfights, I, er, continue in
                  my disgusting habit. There, I've admitted my guilty secret.
                  They say it's the first step to recovery, but only time will
                  tell.

                  --
                  Eric Sosman
                  esosman@ieee-dot-org.invalid

                  Comment

                  • Ben Bacarisse

                    #10
                    Re: &quot;Continue& quot; usage

                    Richard<rgrdev@ gmail.comwrites :
                    Chris Dollin <chris.dollin@h p.comwrites:
                    >
                    >mdh wrote:
                    >>
                    >>May I ask the group this somewhat non-focused question....hav ing now
                    >>seen "continue" used in some of the solutions I have worked on.
                    <snip>
                    >I have seen `continue` used sensibly [1] so rarely
                    >that I treat any use of it
                    >as a code smell
                    >suggesting
                    >that there is an opportunity
                    >to fix the design
                    <snip>
                    >[1] From my POV, natch.
                    >
                    Actually it makes perfect sense when you have your gate condition at the
                    top of the section - far easier to read, understand and maintain in many
                    cases (not all of course).
                    >
                    while(n--){
                    >
                    if(complexCondi tionNotMatched( n))
                    continue; /* not interested. Check the next if there is one. */
                    >
                    /*do processing of things we are interested in*/
                    >
                    }
                    >
                    Now we could invert the test but then we need to scroll down to the end
                    of the function
                    You mean "loop" presumably here.
                    to be see what else happens after the block. Here we see
                    that nothing happens at all - we must move to the next element.
                    There is, of course, no need to invert the test though many people
                    really seem to object to an empty if branch -- an empty else is fine
                    but an empty if seems to be OTT for most people.

                    It is unfortunate that your example is already negative because, to my
                    mind, there is slightly more clarity in:

                    while (n--) {
                    if (complexConditi onMatched(n)) {
                    }
                    }

                    but that is just an accident of choosing a negative condition to start
                    with.
                    Seems clear, maintainable and decent design to me. I fail to see how it
                    could be construed as "bad" in any shape or form.
                    Clear and decent yes, but not 100% maintainable because you loose a
                    place to put code executed at the end of every loop. I honestly don't
                    know if this matters (I have no metric for maintainability ) but people
                    site less complex matters are being an issue for maintainability so I
                    will suggest this one.

                    I almost never use continue but I don't think this is because I want
                    to write super maintainable code, just that to me the "if in a loop"
                    form seems to say what I mean more clearly in all the cases that have
                    come up.

                    --
                    Ben.

                    Comment

                    • mdh

                      #11
                      Re: &quot;Continue& quot; usage

                      On Sep 22, 5:11 am, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
                      >
                           Hi, my name is Eric, and I'm a `continue' abuser.

                      LOL...

                      Thanks Eric and all who replied. More hooks to hang my ideas on. Much
                      Appreciated.

                      Comment

                      • Richard

                        #12
                        Re: &quot;Continue& quot; usage

                        Richard<rgrdev@ gmail.comwrites :
                        Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                        >
                        >Richard<rgrdev @gmail.comwrite s:
                        >>
                        >>Chris Dollin <chris.dollin@h p.comwrites:
                        >>>
                        >>>mdh wrote:
                        >>>>
                        >>>>May I ask the group this somewhat non-focused question....hav ing now
                        >>>>seen "continue" used in some of the solutions I have worked on.
                        ><snip>
                        >>>I have seen `continue` used sensibly [1] so rarely
                        >>>that I treat any use of it
                        >>>as a code smell
                        >>>suggesting
                        >>>that there is an opportunity
                        >>>to fix the design
                        ><snip>
                        >>>[1] From my POV, natch.
                        >>>
                        >>Actually it makes perfect sense when you have your gate condition at the
                        >>top of the section - far easier to read, understand and maintain in many
                        >>cases (not all of course).
                        >>>
                        >>while(n--){
                        >>>
                        >> if(complexCondi tionNotMatched( n))
                        >> continue; /* not interested. Check the next if there is one. */
                        >>>
                        >> /*do processing of things we are interested in*/
                        >>>
                        >>}
                        >>>
                        >>Now we could invert the test but then we need to scroll down to the end
                        >>of the function
                        >>
                        >You mean "loop" presumably here.
                        >
                        Of course. thanks.
                        Actually I was too hasty to accept your correction. No. I did not mean
                        loop. I meant the scope of the if block.

                        e.g

                        if(match(n){
                        something();
                        }else{
                        somethingelse() ;
                        }

                        here without the continue "in your face" one must check for the else at
                        the end when reading the code.

                        This

                        if(complexCondi tionNotMatched( n))
                        continue; /* not interested. Check the next if there is one. */

                        something();

                        is much more explicit and clearer.

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: &quot;Continue& quot; usage

                          Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
                          mdh wrote:
                          >May I ask the group this somewhat non-focused question....hav ing now
                          >seen "continue" used in some of the solutions I have worked on.
                          <snip>
                          Hi, my name is Eric, and I'm a `continue' abuser. I use
                          it as a disguised `goto', simply to avoid excessive nesting:
                          <fx:stands up>
                          Hello, my name is Ben, and *I* abuse break. Ah, it feels so good to
                          say it out loud!

                          I never get tempted by continue (see elsethread) but I can't keep my
                          hands off that lovely break. It is sooooo tempting, when you are all
                          hot and bothered, tightly bound by a strict loop invariant, just to
                          slip in little break and be free.

                          [Serious point: in some ways, break is much worse than continue
                          because of what happens to the way you think about your loops, but I
                          find myself using break much more than continue and paying the extra
                          thinking costs later because I do try to verify the logic of all my
                          loops.]

                          --
                          Ben.

                          Comment

                          • Chris Dollin

                            #14
                            Re: &quot;Continue& quot; usage

                            Richard wrote:
                            Chris Dollin <chris.dollin@h p.comwrites:
                            >
                            >mdh wrote:
                            >>
                            >>May I ask the group this somewhat non-focused question....hav ing now
                            >>seen "continue" used in some of the solutions I have worked on. ( Ex
                            >>7-4 solution by Tondo and Gimpel comes to mind)
                            >>Is there a good principle/s for the good usage of "continue"...or ...do
                            >>any of the regular/irregular contributors have a "favorite" way in
                            >>which it is particularly useful. Thanks as usual.
                            >>
                            >I have seen `continue` used sensibly [1] so rarely
                            >that I treat any use of it
                            >as a code smell
                            >suggesting
                            >that there is an opportunity
                            >to fix the design
                            >in a way that eliminates any need
                            >to consider continue for that code.
                            >>
                            >To indicate an empty loop body
                            >I find `{}` perfectly adequate
                            >on those odd occasions
                            >when it is needed.
                            >>
                            >Mind the gap.
                            >>
                            >[1] From my POV, natch.
                            >
                            Actually it makes perfect sense when you have your gate condition at the
                            top of the section - far easier to read, understand and maintain in many
                            cases (not all of course).
                            >
                            while(n--){
                            >
                            if(complexCondi tionNotMatched( n))
                            continue; /* not interested. Check the next if there is one. */
                            >
                            /*do processing of things we are interested in*/
                            >
                            }
                            This is exactly the kind of example that I find smelly. (Weakly
                            smelly, but still smelly.)

                            I would write the utterly obvious:

                            while (n--)
                            if (complexConditi onMatched(n))
                            doInterestingTh ings(n);

                            (Add layout and braces and inlined function body to taste.)
                            Now we could invert the test but then we need to scroll down to the end
                            of the function to be see what else happens after the block.
                            No, we don't. We just look; functions are not /that/ big, especially
                            nowadays, and editors are not that feeble, so I'm told.
                            Seems clear, maintainable and decent design to me. I fail to see how it
                            could be construed as "bad" in any shape or form.
                            It's not a clear statement of intent; or, more exactly, it's not as
                            clear a statement as one could write with similar effort.

                            --
                            'It changed the future .. and it changed us.' /Babylon 5/

                            Hewlett-Packard Limited registered no:
                            registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

                            Comment

                            • Nick Keighley

                              #15
                              Re: &quot;Continue& quot; usage

                              On 22 Sep, 14:02, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
                              Eric Sosman <esos...@ieee-dot-org.invalidwrit es:
                              mdh wrote:
                              May I ask the group this somewhat non-focused question....hav ing now
                              seen "continue" used in some of the solutions I have worked on.
                              <snip>
                              Hi, my name is Eric, and I'm a `continue' abuser. I use
                              it as a disguised `goto', simply to avoid excessive nesting:
                              >
                              <fx:stands up>
                              Hello, my name is Ben, and *I* abuse break. Ah, it feels so good to
                              say it out loud!
                              >
                              I never get tempted by continue (see elsethread) but I can't keep my
                              hands off that lovely break. It is sooooo tempting, when you are all
                              hot and bothered, tightly bound by a strict loop invariant, just to
                              slip in little break and be free.
                              >
                              [Serious point: in some ways, break is much worse than continue
                              because of what happens to the way you think about your loops, but I
                              find myself using break much more than continue and paying the extra
                              thinking costs later because I do try to verify the logic of all my
                              loops.]
                              I too am an inveterate break user and hardly ever use continue.
                              I think my continue is about the same as my goto usage.

                              As you say though break is potentially much worse. I think the
                              temptation to use it is because it it actually provides a useful
                              construct. continue seems to add confuscation without any particular
                              gain.

                              while (next_item() != end)
                              {
                              if (!match())
                              continue;

                              process_item();
                              }

                              seems less clear than

                              while (next_item() != end)
                              {
                              if (match())
                              process_item();
                              }


                              whilst I sometimes stuctures like this handy (and clear)

                              for (;;)
                              {
                              pre_process();
                              if (!more())
                              break;
                              post_process();
                              }


                              --
                              Nick Keighley

                              Comment

                              Working...