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. Do it only once please

Do it only once please

Scheduled Pinned Locked Moved The Weird and The Wonderful
28 Posts 19 Posters 0 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.
  • P Pascal 0

    Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

    T Offline
    T Offline
    Tony Wesley
    wrote on last edited by
    #5

    The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:

    while (true)   // loop 1 time, exit at bottom
    {
        if (set (ifrFragment))
        {
            result = true;
            ifrTerm = ifrFragment;
            break;            
        }
        if (errorCode)
            break;
    
        if (keyword (TokenSubtype::Not) && term (ifrFragment))
        {
            result = true;
            ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment;
            break;
        }
        if (errorCode)
            break;
      // more lines snipped
       break;
    }
    

    But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.

    R P C 3 Replies Last reply
    0
    • P Pascal 0

      Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #6

      If it ain't broke...

      1 Reply Last reply
      0
      • T Tony Wesley

        The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:

        while (true)   // loop 1 time, exit at bottom
        {
            if (set (ifrFragment))
            {
                result = true;
                ifrTerm = ifrFragment;
                break;            
            }
            if (errorCode)
                break;
        
            if (keyword (TokenSubtype::Not) && term (ifrFragment))
            {
                result = true;
                ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment;
                break;
            }
            if (errorCode)
                break;
          // more lines snipped
           break;
        }
        

        But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.

        R Offline
        R Offline
        Robert Surtees
        wrote on last edited by
        #7

        lol. Haven't seen that done in a long time. We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

        T 1 Reply Last reply
        0
        • R Robert Surtees

          lol. Haven't seen that done in a long time. We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

          T Offline
          T Offline
          Tony Wesley
          wrote on last edited by
          #8

          Robert Surtees wrote:

          We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

          Robert, I use goto's for exactly the same reason.

          if (keyword (TokenSubtype::Group))
          {
              if (!group\_label (groupName))
              {
                  errorMessage = myName + ": Missing GROUP label\\n" + errorMessage;
                  errorCode = DL\_ERROR;
                  **goto exit\_;**
              }
          
              if (!separator (TokenSubtype::Colon))
              {
                  errorMessage = myName + ": Missing colon following GROUP label";
                  errorCode = DL\_ERROR;
                  **goto exit\_;**
              }
          

          // etc...

          C L C 3 Replies Last reply
          0
          • T Tony Wesley

            The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:

            while (true)   // loop 1 time, exit at bottom
            {
                if (set (ifrFragment))
                {
                    result = true;
                    ifrTerm = ifrFragment;
                    break;            
                }
                if (errorCode)
                    break;
            
                if (keyword (TokenSubtype::Not) && term (ifrFragment))
                {
                    result = true;
                    ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment;
                    break;
                }
                if (errorCode)
                    break;
              // more lines snipped
               break;
            }
            

            But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #9

            But then why not

            do
            {
            ...
            } while ( false ) ;

            T 1 Reply Last reply
            0
            • P PIEBALDconsult

              But then why not

              do
              {
              ...
              } while ( false ) ;

              T Offline
              T Offline
              Tony Wesley
              wrote on last edited by
              #10

              Your way is better.

              1 Reply Last reply
              0
              • T Tony Wesley

                The loop one times construct can be useful. Where I've used it is when I've had to work with a long sequence of if else/if's, and used break to exit. For instance:

                while (true)   // loop 1 time, exit at bottom
                {
                    if (set (ifrFragment))
                    {
                        result = true;
                        ifrTerm = ifrFragment;
                        break;            
                    }
                    if (errorCode)
                        break;
                
                    if (keyword (TokenSubtype::Not) && term (ifrFragment))
                    {
                        result = true;
                        ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment;
                        break;
                    }
                    if (errorCode)
                        break;
                  // more lines snipped
                   break;
                }
                

                But the switch/case in your example would lead me to believe that lnIndice had other values at some point, perhaps for debugging.

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #11

                heh. of course that's just a coding-standards-compliant way of writing:

                    if (set (ifrFragment))
                    {
                        result = true;
                        ifrTerm = ifrFragment;
                        goto done;            
                    }
                    if (errorCode)
                        goto done;
                
                    if (keyword (TokenSubtype::Not) && term (ifrFragment))
                    {
                        result = true;
                        ifrTerm = TokenSubtype::Not.asString() + " " + ifrFragment;
                        goto done;
                    }
                    if (errorCode)
                        goto done;
                  // more lines snipped
                

                done:

                :laugh:

                image processing toolkits | batch image processing

                1 Reply Last reply
                0
                • P Pascal 0

                  Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #12

                  You're sure you're not mistaking that "one" in lnIndice <= 1 for an "ell"? Marc

                  Thyme In The Country
                  Interacx
                  My Blog

                  C 1 Reply Last reply
                  0
                  • T Tony Wesley

                    Robert Surtees wrote:

                    We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

                    Robert, I use goto's for exactly the same reason.

                    if (keyword (TokenSubtype::Group))
                    {
                        if (!group\_label (groupName))
                        {
                            errorMessage = myName + ": Missing GROUP label\\n" + errorMessage;
                            errorCode = DL\_ERROR;
                            **goto exit\_;**
                        }
                    
                        if (!separator (TokenSubtype::Colon))
                        {
                            errorMessage = myName + ": Missing colon following GROUP label";
                            errorCode = DL\_ERROR;
                            **goto exit\_;**
                        }
                    

                    // etc...

                    C Offline
                    C Offline
                    chmod2222
                    wrote on last edited by
                    #13

                    I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...

                    -- www.domagoj.net

                    C T 2 Replies Last reply
                    0
                    • C chmod2222

                      I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...

                      -- www.domagoj.net

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #14

                      The rollback from a list of initialization steps maybe another valid usage. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                      1 Reply Last reply
                      0
                      • T Tony Wesley

                        Robert Surtees wrote:

                        We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

                        Robert, I use goto's for exactly the same reason.

                        if (keyword (TokenSubtype::Group))
                        {
                            if (!group\_label (groupName))
                            {
                                errorMessage = myName + ": Missing GROUP label\\n" + errorMessage;
                                errorCode = DL\_ERROR;
                                **goto exit\_;**
                            }
                        
                            if (!separator (TokenSubtype::Colon))
                            {
                                errorMessage = myName + ": Missing colon following GROUP label";
                                errorCode = DL\_ERROR;
                                **goto exit\_;**
                            }
                        

                        // etc...

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #15

                        Tony Wesley wrote:

                        I use goto's

                        It's OK, so do I, in C#! :)

                        xacc.ide
                        IronScheme a R5RS-compliant Scheme on the DLR
                        The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                        1 Reply Last reply
                        0
                        • P Pascal 0

                          Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

                          X Offline
                          X Offline
                          Xiangyang Liu
                          wrote on last edited by
                          #16

                          That's what I call extendable coding. ;P

                          My .NET Business Application Framework My Home Page

                          1 Reply Last reply
                          0
                          • P Pascal 0

                            Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

                            D Offline
                            D Offline
                            Dalek Dave
                            wrote on last edited by
                            #17

                            FINALLY!!!! My first entry on a coding horror! Is this a sign I am starting to learn, possibly, but even in goood old fashioned ms basic circa 1981 I would have spotted this load of old codswallop! So pleased to say that I would never have done this!:-D

                            ------------------------------------ Happy Primes Lead to Happy Memories. Don't Google FGI

                            S 1 Reply Last reply
                            0
                            • C chmod2222

                              I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...

                              -- www.domagoj.net

                              T Offline
                              T Offline
                              Tony Wesley
                              wrote on last edited by
                              #18

                              chmod2222 wrote:

                              I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...

                              Real Programmers aren't afraid to use GOTO's[^]

                              C 1 Reply Last reply
                              0
                              • T Tony Wesley

                                chmod2222 wrote:

                                I use goto only to break out of >1 nested loops... Don't see the need for it anywhere else...

                                Real Programmers aren't afraid to use GOTO's[^]

                                C Offline
                                C Offline
                                chmod2222
                                wrote on last edited by
                                #19

                                I aint affraid to use it bubba joe :) I just don't see the need for it... At least in C#...

                                -- www.domagoj.net

                                T 1 Reply Last reply
                                0
                                • D Dalek Dave

                                  FINALLY!!!! My first entry on a coding horror! Is this a sign I am starting to learn, possibly, but even in goood old fashioned ms basic circa 1981 I would have spotted this load of old codswallop! So pleased to say that I would never have done this!:-D

                                  ------------------------------------ Happy Primes Lead to Happy Memories. Don't Google FGI

                                  S Offline
                                  S Offline
                                  Secrets
                                  wrote on last edited by
                                  #20

                                  heheheh :D. Cool thing....i myself as developer can understand that sort of things can happen when code passes through certain hands without proper comments.

                                  1 Reply Last reply
                                  0
                                  • P Pascal 0

                                    Today we had to edit very old code and we ran into this great piece of codefor (int lnIndice = 1; lnIndice <= 1; lnIndice++) { switch (lnIndice) { case 1: // do something here break; } } We found it very funny :-D

                                    K Offline
                                    K Offline
                                    KarstenK
                                    wrote on last edited by
                                    #21

                                    Whether it was the basement of great code or the ruin of an unlucky project. Who knows... :~ A good optimized cempiler would clean this up. :-O

                                    Greetings from Germany

                                    1 Reply Last reply
                                    0
                                    • C chmod2222

                                      I aint affraid to use it bubba joe :) I just don't see the need for it... At least in C#...

                                      -- www.domagoj.net

                                      T Offline
                                      T Offline
                                      Tony Wesley
                                      wrote on last edited by
                                      #22

                                      C#? Sounds like a language for quiche eaters. :) Seriously, there are times when nested if/else constructs get unweildy. In some cases, albeit rarely, I'd rather use a goto. I only use them to transfer control either to a error handling section or a single exit point.

                                      1 Reply Last reply
                                      0
                                      • T Tony Wesley

                                        Robert Surtees wrote:

                                        We used to do it to piss off the "never use goto" zealots years ago when forbidden to use 'goto xit' for handling error conditions.

                                        Robert, I use goto's for exactly the same reason.

                                        if (keyword (TokenSubtype::Group))
                                        {
                                            if (!group\_label (groupName))
                                            {
                                                errorMessage = myName + ": Missing GROUP label\\n" + errorMessage;
                                                errorCode = DL\_ERROR;
                                                **goto exit\_;**
                                            }
                                        
                                            if (!separator (TokenSubtype::Colon))
                                            {
                                                errorMessage = myName + ": Missing colon following GROUP label";
                                                errorCode = DL\_ERROR;
                                                **goto exit\_;**
                                            }
                                        

                                        // etc...

                                        C Offline
                                        C Offline
                                        CurtD
                                        wrote on last edited by
                                        #23

                                        So what happens to your "goto" cleanup routine when an exception is thrown? I'll give you a hint -- it doesn't get called.

                                        T 1 Reply Last reply
                                        0
                                        • C CurtD

                                          So what happens to your "goto" cleanup routine when an exception is thrown? I'll give you a hint -- it doesn't get called.

                                          T Offline
                                          T Offline
                                          Tony Wesley
                                          wrote on last edited by
                                          #24

                                          CurtD wrote:

                                          So what happens to your "goto" cleanup routine when an exception is thrown? I'll give you a hint -- it doesn't get called.

                                          The goto doesn't figure into that. Just above it in the thread is another snippet of my code that doesn't use a goto. Same issue. What happens to the code after the while loop when an exception is thrown? It doesn't get executed. Once could argue that the problem is with exception. As I pointed out elsewhere, Joel Spolsky argues that exceptions [...] create an abrupt jump from one point of code to another. In fact they are significantly worse than goto's: (emphasis added) I don't quite agree with Joel. But there was a time, when I was debugging an app that another programmer had written, that I understood that completely. It was spaghetti exception handling. I never knew where I had come from. I'm not saying you shouldn't use exceptions. But they can be abused as badly as gotos.

                                          C 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