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

    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
          • 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
            Michael Davey 1
            wrote on last edited by
            #25

            omg.. what else is there to say ?? :)

            Michael Davey biproject.com rss and blog news in a more palatable format mobile edition now available!

            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
              Xagyg
              wrote on last edited by
              #26

              - Pascal - wrote:

              We found it very funny

              I find it quite useful, actually. This is the sort of thing that sits in code that is prone to change. Why destroy the entire structure when a month or two from now, someone has a need to add in an index of 2 and 3? Though, admittedly, I'd comment out everything but the 'do something' code when it got this low. :) But I'd still leave it THERE. :)

              1 Reply Last reply
              0
              • M Marc Clifton

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

                Thyme In The Country
                Interacx
                My Blog

                C Offline
                C Offline
                codemunkeh
                wrote on last edited by
                #27

                Only a real programmer who codes in raw hex, or someone very daft, or someone with the editor font set to one with massive differences between 1 and l would ever call a variable "l". i, on the other hand...


                Ninja (the Nerd)
                Confused? You will be...

                1 Reply Last reply
                0
                • T Tony Wesley

                  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 Offline
                  C Offline
                  CurtD
                  wrote on last edited by
                  #28

                  Tony Wesley wrote:

                  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.

                  I'm talking about the case where coders like to put a block of cleanup code at the bottom of a method and "goto" it when something goes wrong or fall into it in a non-error situation. If you are depending on blocks of cleanup code, an exception will bypass it completely. This worked back in C, but not in OO design. That's why I use objects that clean themselves up when destroyed -- automatically. I am far too lazy to try to figure out every possible error situation, catch exceptions from anything that might throw them, and ensure that there is an appropriate goto. I disagree completely with Joel Spolsky. He suggests using return codes instead of exceptions. Return codes and exceptions are completely different things. Exceptions are not meant to be return codes. They are "exceptional" situations where usually the app cannot continue. And exceptions have the major advantage of unwinding the stack, which he fails to mention. They are not "an abrupt jump from one point of code to another." It seems like OO programming has really failed to catch on with a lot of developers.

                  modified on Sunday, December 16, 2007 12:10:46 AM

                  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