Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Infinite for loops

Infinite for loops

Scheduled Pinned Locked Moved The Weird and The Wonderful
helpc++
6 Posts 5 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jesse Krebs
    wrote on last edited by
    #1

    Hello, love the show, long time listener, first time caller.

    One of the company's c++ coding guidelines is that goto's are forbidden. For some developers who use goto statements as error handling this could present a problem. For others, ... for ( ;; ) { // Some code for ( ;; ) { bool someError = func(); if (someError) { break; } // ... if (anotherError) { break; } // ... break; } // ... for ( ;; ) { // A few more hundred lines of code with more for( ;; ) loops. break; } // ... break; } After stumbling upon this beauty I searched and found over 200 infinite loops used as an error handling technique. Not one of these loops ever actually looped.

    M 1 Reply Last reply
    0
    • J Jesse Krebs

      Hello, love the show, long time listener, first time caller.

      One of the company's c++ coding guidelines is that goto's are forbidden. For some developers who use goto statements as error handling this could present a problem. For others, ... for ( ;; ) { // Some code for ( ;; ) { bool someError = func(); if (someError) { break; } // ... if (anotherError) { break; } // ... break; } // ... for ( ;; ) { // A few more hundred lines of code with more for( ;; ) loops. break; } // ... break; } After stumbling upon this beauty I searched and found over 200 infinite loops used as an error handling technique. Not one of these loops ever actually looped.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      Yep, seen it. I hate this irrational ban on goto. It's out of date - it dates from a time when decent control structures weren't common. A judicious, careful, occasional use of a goto is fine. Subverting a control structure to give it a different meaning is definitely not. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^] [PDF, 17k].

      Stability. What an interesting concept. -- Chris Maunder

      R J 2 Replies Last reply
      0
      • M Mike Dimmick

        Yep, seen it. I hate this irrational ban on goto. It's out of date - it dates from a time when decent control structures weren't common. A judicious, careful, occasional use of a goto is fine. Subverting a control structure to give it a different meaning is definitely not. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^] [PDF, 17k].

        Stability. What an interesting concept. -- Chris Maunder

        R Offline
        R Offline
        RodgerB
        wrote on last edited by
        #3

        Goto's are really really bad and should be super rarely used. They are not needed for error handling. The only good use I've seen for goto's is machine generated code with tools like YACC and LEX. Generally speaking, break, continue, and multiple returns can cause problems too. Readability and maintainability are very important. Imagine if you will a "come from" statement. You put a label X somewhere then in a totally different part of the method you place a "come from" label X. Whenever control reaches the label you goto the "come from". Imagine how hard it would be to read that code. What's wrong with using exceptions for error handling?

        J 1 Reply Last reply
        0
        • R RodgerB

          Goto's are really really bad and should be super rarely used. They are not needed for error handling. The only good use I've seen for goto's is machine generated code with tools like YACC and LEX. Generally speaking, break, continue, and multiple returns can cause problems too. Readability and maintainability are very important. Imagine if you will a "come from" statement. You put a label X somewhere then in a totally different part of the method you place a "come from" label X. Whenever control reaches the label you goto the "come from". Imagine how hard it would be to read that code. What's wrong with using exceptions for error handling?

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          RodgerB wrote:

          Goto's are really really bad and should be super rarely used. They are not needed for error handling.

          This

          if (spin_trylock(&tty_lock.lock))
          goto got_lock;
          if (tsk == tty_lock.lock_owner) {
          WARN_ON(!tty_lock.lock_count);
          tty_lock.lock_count++;
          return flags;
          }
          spin_lock(&tty_lock.lock);
          got_lock:
          WARN_ON(tty_lock.lock_owner);

          seems pretty well justified for me. You could do this with nested ifs, but you would neither get more clarity nor faster code. Only code indenting out at your right screen margin.


          Failure is not an option - it's built right in.

          R 1 Reply Last reply
          0
          • J jhwurmbach

            RodgerB wrote:

            Goto's are really really bad and should be super rarely used. They are not needed for error handling.

            This

            if (spin_trylock(&tty_lock.lock))
            goto got_lock;
            if (tsk == tty_lock.lock_owner) {
            WARN_ON(!tty_lock.lock_count);
            tty_lock.lock_count++;
            return flags;
            }
            spin_lock(&tty_lock.lock);
            got_lock:
            WARN_ON(tty_lock.lock_owner);

            seems pretty well justified for me. You could do this with nested ifs, but you would neither get more clarity nor faster code. Only code indenting out at your right screen margin.


            Failure is not an option - it's built right in.

            R Offline
            R Offline
            RodgerB
            wrote on last edited by
            #5

            I guess we could argue this back and forth. It comes down to avoiding mistakes and doing things as a matter of policy. E.g. I never do this: int* y, z; I always do this: int* y; int z; Why you ask. Too easy to read it wrong at a glance, too easy to miss something. Some logic as to why I never use goto's and why I don't put returns anywhere but at the very bottom or very top of a function. I believe this to be more than just a matter of style.

            1 Reply Last reply
            0
            • M Mike Dimmick

              Yep, seen it. I hate this irrational ban on goto. It's out of date - it dates from a time when decent control structures weren't common. A judicious, careful, occasional use of a goto is fine. Subverting a control structure to give it a different meaning is definitely not. For more, see Gotos Considered Harmful and Other Programmers' Taboos[^] [PDF, 17k].

              Stability. What an interesting concept. -- Chris Maunder

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              That is an excellent paper! I rarely use goto, but sometimes it is the best solution to a problem. (Example: escaping from multiple nested loops.) I once read someone’s general rule that you always go forward, but I found one exception years ago:

              NEXT_BYTE:
              if( x>xmin ) {
              Byte = *(--vptr);
              /* Skip whole bytes */
              if( Byte == 0xFF ) {
              x -= 8;
              goto NEXT_BYTE;
              }
              }

              Sure the above code could be written using a loop and originally was, but this increased the speed (graphics code) enough to justify its use at the time.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups