11 Replies - 17620 Views - Last Post: 29 November 2008 - 10:49 AM Rate Topic: -----

#1 kiranc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 08

Why using a goto statement is bad programming practice?

Post icon  Posted 29 November 2008 - 03:21 AM

What r technical resons for not using goto statement in any language..[i]
Is This A Good Question/Topic? 0
  • +

Replies To: Why using a goto statement is bad programming practice?

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 04:14 AM

Most programmers do not use goto-statements because it makes the code very hard to read. This can be an annoyance when trying to understand code written by other people. A while-loop with break-statements withing is considered to be better coding-practise.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 04:19 AM

There aren't any technical reasons for not using goto - its a perfectly legitimate keyword. At assembly level, all control statements are implemented using jumps (goto) - it would be impossible to write anything more than an extremely trivial program without such control statements.

I suggest you research a famous article written by Edgar Dijkstra on his personal opinion on why 'goto' is considered harmful, and open to abuse by anyone who doesn't fully understand the implications of what they're doing. This article is the generally regarded as the reason why 'goto' is frowned upon by most programmers. - I believe its better to read around for yourself and to make up your own mind than to take somebody else's word for it. its equally bad practise for programmers to run around preaching the evils of goto without actually understanding the real reasons why, or any recognition of the occasional situations when goto is actually OK

Bear in mind that goto is not the only "evil" keyword or programming practise which is widely open to abuse.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 04:35 AM

Quote

I suggest you research a famous article written by Edgar Dijkstra on his personal opinion on why 'goto' is considered harmful, and open to abuse by anyone who doesn't fully understand the implications of what they're doing. This article is the generally regarded as the reason why 'goto' is frowned upon by most programmers. .


Here is the Edsger W. Dijkstra article referred to above for those who want to look at a bit of CompSci history.
http://www.ifi.uzh.c...jkstra_Goto.pdf
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 04:43 AM

Maybe a picture will help.

10 goto 40
20 print "and"
30 goto 60
40 print "To infinity"
50 goto 20
60 print "beyond."
70 goto 10



Looks unlikely, I know. However, put a whole bunch of meat in between those little gotos and it will look perfectly reasonable. And will take days and hair loss to debug.

A goto has two fundamental dangers. One is spaghetti code from hell, like the mess above. The other is unintended infinite loops, also above.

It is interesting to note that at the machine code level there are gotos. Every higher level language tries to protect the programmer from them. When a program "locks up" it usually means it's started down an unintended execution path. The reason it's locked is usually because it finds its way into a maze of gotos. Strangely, this is a good thing, because it could find it's way to a format hard drive command. ;)
Was This Post Helpful? 0
  • +
  • -

#6 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 06:37 AM

I was under the impression that gotos, especially inside functions, break you out of that function without the proper cleanup, leaving orphaned memory segments i.e. memory leaks. That's not true?
Was This Post Helpful? 0
  • +
  • -

#7 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 07:00 AM

Is GOTO X similar to JMP LABEL in ASM?
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 07:31 AM

View PostPwn, on 29 Nov, 2008 - 01:37 PM, said:

I was under the impression that gotos, especially inside functions, break you out of that function without the proper cleanup, leaving orphaned memory segments i.e. memory leaks. That's not true?

At least in C and C++, referencing a label which exists beyond the scope of a function will cause a compile error, so this isn't a problem. Other languages which have more relaxed scoping rules might suffer from it, however.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 09:49 AM

View PostBujarM, on 29 Nov, 2008 - 08:00 AM, said:

Is GOTO X similar to JMP LABEL in ASM?


Yes. That's exactly what I was thinking of when I described random ops getting looped. However, not all ASM implementation call it JMP, so being inclusive it tricky.

For that matter, not all languages call a goto a goto. Basically, anything that allows code execution to jump to an arbitrary location probably meets the definition of goto. Higher level loops are different, because they pre define a scope and are more constrained.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 10:27 AM

The only place I've ever really used gotos, outside of AppleBASIC 20 some-odd years ago, is when you've got a lot of individual memory allocations in a C program and if something fails in there, you end up with a lot of duplicate cleanup code. Like this contrived example:
char *blah()
{
    char *var1 = NULL;
    char *var2 = NULL;
    char *var3 = NULL;
    char *result = NULL;
    if ((var1 = malloc(25)) == NULL)
    {
        return NULL;
    }
    if ((var2 = malloc(75)) == NULL)
    {
        free(var1);
        return NULL;
    }
    if ((var3 = malloc(75)) == NULL)
    {
        free(var2);
        free(var1);
        return NULL;
    }
    if ((result = malloc(strlen(var3) + strlen(var2) + strlen(var1) + sizeof(*result)) == NULL)
    {
        free(var3);
        free(var2);
        free(var1);
        return NULL;
    }

    sprintf(result, "%s%s%s", var3, var2, var1);
    free(var3);
    free(var2);
    free(var1);

    return result;
}


You can tighten this up a couple of ways, first with a cleanup label and gotos:
char *blah()
{
    char *var1 = NULL;
    char *var2 = NULL;
    char *var3 = NULL;
    char *result = NULL;
    if ((var1 = malloc(25)) == NULL)
    {
        goto cleanup;
    }
    if ((var2 = malloc(75)) == NULL)
    {
        goto cleanup;
    }
    if ((var3 = malloc(75)) == NULL)
    {
        goto cleanup;
    }
    if ((result = malloc(strlen(var3) + strlen(var2) + strlen(var1) + sizeof(*result)) == NULL)
    {
        goto cleanup;
    }

    sprintf(result, "%s%s%s", var3, var2, var1);

cleanup:
    free(var3);
    free(var2);
    free(var1);

    return result;
}


Or through the use of a do...while(FALSE) loop and breaks:
char *blah()
{
    char *var1 = NULL;
    char *var2 = NULL;
    char *var3 = NULL;
    char *result = NULL;
    do
    {
        if ((var1 = malloc(25)) == NULL)
        {
            break;
        }
        if ((var2 = malloc(75)) == NULL)
        {
            break;
        }
        if ((var3 = malloc(75)) == NULL)
        {
            break;
        }
        if ((result = malloc(strlen(var3) + strlen(var2) + strlen(var1) + sizeof(*result)) == NULL)
        {
            break;
        }

        sprintf(result, "%s%s%s", var3, var2, var1);
    } while (0);

    free(var3);
    free(var2);
    free(var1);

    return result;
}


These uses require the proper initialization of the local variables, or you're going to have a mess on your hands.

This post has been edited by JackOfAllTrades: 29 November 2008 - 10:29 AM

Was This Post Helpful? 1

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 10:47 AM

Sorry, I've still seen no reasonable excuse for a goto. Nor have I ever seen code that required it, much less used it as a more elegant solution.

From the admittedly slapped together example:

char *blah() {
	char *var1 = NULL;
	char *var2 = NULL;
	char *var3 = NULL;
	char *result = NULL;
	if ( ((var1 = malloc(25)) != NULL) && ((var2 = malloc(75)) != NULL) && ((var3 = malloc(75)) != NULL) ) {
		result = malloc(strlen(var3) + strlen(var2) + strlen(var1) + sizeof(*result));
	}

	if (result!=NULL) { sprintf(result, "%s%s%s", var3, var2, var1); }
	
	free(var3);
	free(var2);
	free(var1);
	return result;
}


Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Why using a goto statement is bad programming practice?

Posted 29 November 2008 - 10:49 AM

Great article. It's wonderful how things like that still apply even though it was written in 1968. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1