Thread: goto, why not

  1. #31
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Suppose this happens, you will most definitely need to use a goto.
    If this ever happens, it's bad design in the first place. If you solution is applied, you code is guaranteed to break after the first two or three changes.

    For nested loops: In C you might be right. In C++ you can use exception handling. After all, something interrupting your nested loops should be highly exceptional. Or it should exit the function at this time. I don't see a need with proper function design to place a goto statement.

    I have never used a goto statement in C/C++. Last time I used it was Atari Basic when I was too inexperienced to know what a loop is. ( And VB, but there it's really just another method of exception handling ).

    Actually, using goto is not that bad. It's just that from my experience, people using it made mistakes in their concepts and tried to remedy them this way. And goto should not be used to make dirty code clean up a hopeless concept.

    We are always talking about how a goto might be useful. Can anyone here give a real life example ? Can anyone give me a portion of code that isn't solvable by proper planning alone ?
    No assumptions, not guesses, a real world example ?



    Edited to add:

    At my current employer, use of the goto statement would violate the installation standards and hence fail the quality departments tests.
    Right, the use of goto would violate our coding standards and my code would be considered wrong no matter how fast it runs.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  2. #32
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The linux kernel uses them.
    microsoft also does.

    Here's some msdn code

    int ProcessFiles(const char* pszFiles, FILECALLBACK pFunc, void* pArgs)
    {
    HRESULT hr = S_OK;
    int i;

    WCHAR* wszArg = WideString(pszFiles);
    WIN32_FIND_DATA FindFileData;
    HANDLE handle = ::FindFirstFile(wszArg, &FindFileData);
    int failed = 0;

    if (handle == INVALID_HANDLE_VALUE)
    {
    // Then maybe it's a URL.
    hr = (*pFunc)(wszArg, pArgs);
    if (hr != 0) failed++;
    goto CleanUp;
    }
    for ( i = wcslen(wszArg)-1; i >= 0 && wszArg[i] != '\\'; i--)
    {
    wszArg[i] = 0;
    }

    while (handle != INVALID_HANDLE_VALUE)
    {
    ULONG len1 = wcslen(wszArg);
    ULONG len2 = wcslen(FindFileData.cFileName);
    WCHAR* buffer = new WCHAR[len1 + len2 + 1];
    if (buffer == NULL)
    {
    hr = E_OUTOFMEMORY;
    goto CleanUp;
    }
    memcpy(buffer, wszArg, sizeof(WCHAR) * len1);
    memcpy(&buffer[len1], FindFileData.cFileName, sizeof(WCHAR) * len2);
    buffer[len1+len2] = '\0';

    hr = (*pFunc)(buffer, pArgs);

    if (hr != 0) failed++;

    delete[] buffer;

    if (! ::FindNextFile(handle, &FindFileData))
    break;
    }
    if (handle != INVALID_HANDLE_VALUE)
    ::FindClose(handle);

    CleanUp:
    delete wszArg;

    return failed;
    }

  3. #33
    Registered User
    Join Date
    Oct 2001
    Posts
    22
    Why would you have nested loops 4 deep?

    you could just do this

    int done = 0;
    for (int a = 0; a < 100, done < 1; a++)
    {
    for (int b = 0; b < 100, done < 1; b++)
    {
    for (int c = 0; c < 100, done < 1; c++)
    {
    for (int d = 0; d < 100, done < 1; d++)
    {
    if (condition)
    done = 2;
    }
    }
    }
    }
    }

    omg... no goto.... no 4 conditional break....

    -Vulcan

  4. #34
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    hr = E_OUTOFMEMORY;
    goto CleanUp;
    Yes... this comes from the source that uses void main. Just because it's MSDN doesn't mean it's good. So this is a higly exceptional state of the function: out of memory. How about exception handling ? try-catch ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #35
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Just as a side note and to say something a bit more constructive, I don't use goto's at all. The last time I used a goto was coding in GW-BASIC back in the early 80's. Even QBasic has been designed specifically so you do not have to use goto's. VB, which is designed from QBasic and the BASICs, also does not make use of a goto (only in error handling do you 'have' to use a goto). It is clear to me that Microsoft went to modules and no line numbers in their new line of BASICs specifically to remove the ability to jump around (goto's and gosub's) all over the place in code since it is not good programming practice.

    I was taught that there should be only one entry into and one exit from a function or procedure and that the procedure should only do one thing, but do it very well. So, I try to follow this example as much as possible and do not exit out of my functions until the end of the function. It was hard to break the habit at first, but now I cringe at code that jumps all over the place. Besides, when you jump around, are you not slowing the entire program down? According to Randall Hyde's AoA and a lot of books, websites, and teachers I've had, jumping around a lot is not a good practice.

    With a goto, I could exit as many times as I please and the code would be very ugly, hard to debug, and probably would not be any faster than if I had not used a goto - probably much slower. I've never used a goto in C/C++. The closest thing that I've used to a goto is an on error statement in VB which jumps to a label on an error. But I don't even like using those cuz your subs and functions get all cluttered up with error handlers and you have to explicity exit the sub or function prior to reaching the label. Very ugly. You can have this big ugly global error handler, but again error handling in QBasic and VB is just ugly.

    I even cringe when I have to use a jmp or jx instruction in assembly, even though I must at times. To avoid this I try to structure asm program logic to 'fall through' to labels on certain conditions instead of jumping to them, even though a near jump is not that costly.


    I said this before and I'll say it again.

    A high level program that looks as though it has no choice but to use a goto is poorly designed. When it comes to goto's and higher level languages it seems to me that there is always a better way.

  6. #36
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    goto mess up your code "pipeline code"
    Yoshi

  7. #37
    Scourfish
    Guest
    Bah! You hippocrites. Just code comfortably. After all, that shoe commercial says "Just do it!"

  8. #38
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Scourfish: >>> Bah! You hippocrites.

    Who is being hypocritical and why?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #39
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    All you have to do when programming with goto is to keep it in a respectable manner. Don't have code like:
    Code:
    label1:    printf("This is label1\n");
    label2:    printf("This is label2\n");
    label3:    printf("This is label3\n");
    
    if (x == 5)
    goto label1;
    elseif (x == 87)
    goto label3;
    else
    goto label2;
    That's no good. Keep it in perspective, that's all.
    1978 Silver Anniversary Corvette

  10. #40
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    yeah, it is like a RPG. You cannot go back.

    --------------------
    engineer223
    Yoshi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM