11 Replies - 11983 Views - Last Post: 29 November 2008 - 10:49 AM
#1
Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 03:21 AM
Replies To: Why using a goto statement is bad programming practice?
#2
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 04:14 AM
#3
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 04:19 AM
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.
#4
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 04:35 AM
Quote
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
#5
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 04:43 AM
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.
#6
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 06:37 AM
#7
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 07:00 AM
#8
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 07:31 AM
Pwn, on 29 Nov, 2008 - 01:37 PM, said:
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.
#9
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 09:49 AM
BujarM, on 29 Nov, 2008 - 08:00 AM, said:
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.
#10
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 10:27 AM
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
#11
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 10:47 AM
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;
}
#12
Re: Why using a goto statement is bad programming practice?
Posted 29 November 2008 - 10:49 AM
|
|

New Topic/Question
Reply




MultiQuote







|