Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26
  1. #11
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    1,627
    the reason is, that it makes it a lot harder to read the code cuz of all the BS like - goto this label down, goto this label up, and that way a lot of code in the middle doesn't even get read by the compiler, ever. so its very hard to debug a code where goto is used. so use it until you are absolulety sure u know what you're doin' otherwise just don't touch, there are a lot better alternatives.

    it was finally concluded by the ANSI C professionals as the 'Sphagetti Code' just as a sphagetti is all jumbled up and disorganised with one on top of other with no particular order, thats how goto is to the language C. so it was deemed to be a bad programmin' technique and was hence avoided by all programmers from there on.

    hope this helps!!

    Cheers,
    SD
    FX Techincal Director

  2. #12
    Join Date
    Jan 2004
    Location
    South Pole
    Posts
    1,519
    I use gotos in my code.. It doesn't really make things too difficult to understand, as long as I don't have more than 3-4 of them in one place. I use them because often it's more convenient and faster (both to write and both to execute) than setting/reading a flag. Very useful when you want to get out of several nested loops in one command, instead of having to check a flag in each of 5 loops.

    Example:
    Code:
    for (int i=0;i<10;i++)
        for (int j=0;j<10;j++)
            for (int k=0;k<10;k++)
                while (whatever) {
                    if (something) goto stop;
                    //...
                }
    
    stop:;
    It's also an elegant solution for when you want to jump inside a pair of for loops.. example:
    Code:
    void backtrack(int initialX, int initialY) {
        int x=initialX, y=initialY;
        goto start;
        for (x=0;x<width;x++) {
            for (y=0;y<height;y++) {
                start:;
                //Do whatever...
            }
        }
    }
    Show me a cleaner way to do that without using a goto, and I'll consider not bashing people who bash my precious goto!

  3. #13
    Join Date
    Oct 2002
    Location
    Leicester, UK
    Posts
    3,335
    About your second example, that goto seems unnecessary, since that is the default flow.
    Forget the username, call me Keith

  4. #14
    Join Date
    Mar 2003
    Location
    Colorful Colorado
    Posts
    1,096
    second example first - cleaner code without goto:


    Code:
    void backtrack(int initialX, int initialY) {
        int x=initialX, y=initialY;
    
        for (x=initialX ;x<width;x++) {
            for (y=initialY;y<height;y++) {
    
                //Do whatever...
            }
        }
    }
    The first case is the one case where goto's can have a place, but even then I would think about if I had the logic right (does it really require three nested loops, etc. ) before I'd use the goto.

    I use them because often it's more convenient and faster (both to write and both to execute)
    This is fine if the program is small and only you maintain it. If it is a program that someone else may ever have to maintain then Readability if far more important the ease of writing.
    Chuck
    Digital Spectra
    (rendering with pixels)

  5. #15
    Join Date
    Dec 2003
    Posts
    60
    i have actualy newer used goto in c++. kinda got an habbit.
    how mutch faster is an goto loop?

  6. #16
    Join Date
    Oct 2003
    Location
    My little corner of the world
    Posts
    1,460
    Quote Originally Posted by halma
    I use gotos in my code.. It doesn't really make things too difficult to understand, as long as I don't have more than 3-4 of them in one place. I use them because often it's more convenient and faster (both to write and both to execute) than setting/reading a flag. Very useful when you want to get out of several nested loops in one command, instead of having to check a flag in each of 5 loops.

    Example:
    Code:
    
    
    It's also an elegant solution for when you want to jump inside a pair of for loops.. example:
    Code:
    
    
    Show me a cleaner way to do that without using a goto, and I'll consider not bashing people who bash my precious goto!
    now thats second code is REALLY nasty
    I mean... its completly the oposite from elegant

    The first example is one of the few cases where i would think goto's are ok.
    But as said earlier using then once can led to using them in innapropriate cases (as your second example). Maybe an alternative solution for it could be

    Code:
    for (int i=0;i<10 && !something;i++)
        for (int j=0;j<10 && !something;j++)
            for (int k=0;k<10 && !something;k++)
                while (whatever && !something) {
                    //...
                }
    now thats look a lot better for me
    some repeting code yeah, but you can easely see when your for loop is suposed to stop.

    And also the articles refered by Fluxium are VERY pertinent, especially the first being from Dijkstra and the second leading to notes from Knuth to eliminate goto's.

    PS.:Took out your original code just to make the post smaller
    Last edited by Mkhaos7; 03-29-2005 at 09:28 AM.
    "Things are like they are because thats how they are suposed to be"

  7. #17
    Join Date
    Jan 2004
    Location
    South Pole
    Posts
    1,519
    Quote Originally Posted by capt chuck
    second example first - cleaner code without goto:

    Code:
    void backtrack(int initialX, int initialY) {
        int x=initialX, y=initialY;
    
        for (x=initialX ;x<width;x++) {
            for (y=initialY;y<height;y++) {
    
                //Do whatever...
            }
        }
    }
    Actually, that does something quite different from what I wrote! Yes, in my code, x starts at initialX. But y starts at initialY the first time, but starts at 0 every time after that!

    So if you wanted to do that without using a goto, you'd have to do something like:
    Code:
    void backtrack(int initialX, int initialY) {
        int x=initialX, y=initialY;
    
        for (x=initialX;x<width;x++) {
            for (y=((x==initialX)?initalY:0);y<height;y++) {
                //Do whatever...
            }
        }
    }
    Or maybe:
    Code:
    void backtrack(int initialX, int initialY) {
        int x=initialX, y=initialY;
    
        bool first=true;
        for (x=initialX;x<width;x++) {
            int y;
            if (first) {
                y=initialY;
                first=false;
            } else
                y=0;
            for (;y<height;y++) {
                //Do whatever...
            }
        }
    }
    I think the goto is prettier than either of these alternatives!
    Last edited by halma; 03-31-2005 at 11:05 AM.

  8. #18
    Join Date
    Oct 2003
    Location
    My little corner of the world
    Posts
    1,460
    it might be prettier but its VERY harder to read
    you can see this since both me and capt chuck (and others, i'm sure) thought the code was suposed to do one thing, when it was actually doing other
    Last edited by KhaoticMind; 04-01-2005 at 01:55 AM.
    "Things are like they are because thats how they are suposed to be"

  9. #19
    Join Date
    Jan 2004
    Location
    South Pole
    Posts
    1,519
    Well, you know, if it was hard to write, it should be hard to read!

    Just kidding!

    But you know, we programmers need to have a little fun every once in a while too...

    Oh, and by the way, about
    how mutch faster is an goto loop?
    If you mean using goto's instead of writing a for loop or while loop, it's not faster at all! Don't do it!

  10. #20
    Join Date
    Dec 2002
    Posts
    73
    I must admit to having used goto quite a lot - 1975 in BASIC.
    We've moved on a bit since then. It comes down to readable code, not just the person that wrote it but the poor mugs that have to maintain it. I found out very quickly that I prefer maintaining simple code, so that's what I write.

    Ryan

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •