GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement...

24
GOTO GOTO CONSIDERED HARMFUL CONSIDERED HARMFUL Tarana Yar Tarana Yar

Transcript of GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement...

Page 1: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

GOTO GOTO

CONSIDERED HARMFULCONSIDERED HARMFUL

Tarana YarTarana Yar

Page 2: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

The publishing of the paper entitled The publishing of the paper entitled “Go To Statement Considered “Go To Statement Considered Harmful”, written by Edsger W. Harmful”, written by Edsger W. Dijkstra in 1968, marked the Dijkstra in 1968, marked the beginning of the debate on whether it beginning of the debate on whether it is appropriate to use the is appropriate to use the gotogoto statement in a well-structured statement in a well-structured program. program.

Topic debated till the late 1970s, when Topic debated till the late 1970s, when the debate died down the debate died down

Page 3: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

What is goto?What is goto?

GOTOGOTO is a command found in many is a command found in many programming languages which instructs programming languages which instructs the computer to jump to another point in the computer to jump to another point in the computer program.the computer program.

It is the fundamental operation which can It is the fundamental operation which can be used for transfer of control from one be used for transfer of control from one part of a program to another.part of a program to another.

Page 4: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

GOTO is found in GOTO is found in FORTRANFORTRAN, , AlgolAlgol, , COBOLCOBOL, , SNOBOLSNOBOL, BASIC, C, C++, , BASIC, C, C++, Pascal, Perl and many other Pascal, Perl and many other languages, particularly assembly languages, particularly assembly languages languages

In the assembly languages, the GOTO In the assembly languages, the GOTO command is usually called BRA (from command is usually called BRA (from "branch"), JMP or JUMP, and is often "branch"), JMP or JUMP, and is often the only way of organizing program the only way of organizing program flow. flow.

Page 5: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Simple Example of GOTOSimple Example of GOTO

If ( a == 5)If ( a == 5)goto aLabelgoto aLabel

Else{Else{a = a -5a = a -5b++b++

}}

aLabel: a = b+1aLabel: a = b+1

Page 6: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

One of E.W. Dijkstra’s arguments in his One of E.W. Dijkstra’s arguments in his paper was that the use of goto statements paper was that the use of goto statements produces incomprehensible spaghetti codeproduces incomprehensible spaghetti code

Spaghetti codeSpaghetti code is a term for code with a is a term for code with a complex and tangled control structure, complex and tangled control structure, especially one using many GOTOs, especially one using many GOTOs, exceptions, or other "unstructured" exceptions, or other "unstructured" branching constructs. It is named such branching constructs. It is named such because program flow tends to look like a because program flow tends to look like a bowl of spaghetti.bowl of spaghetti.

Page 7: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Example of spaghetti codeExample of spaghetti code

$test = 1; $test = 1; loop: loop:

if($test == 0) if($test == 0) goto loopend; goto loopend;

if($var == 1) if($var == 1) { {

$var = 3; $var = 3; goto preloop;goto preloop;

} } If($var == 2)If($var == 2)

goto var_7; goto var_7; if($var == 7) if($var == 7) { {

var_7: var_7: $var = 9; $var = 9; goto preloop; goto preloop;

} } $var = $var + 5; $var = $var + 5; $test = 0; $test = 0; preloop: preloop: $var = $var - 1; $var = $var - 1; goto loop; goto loop; loopend: loopend:

Page 8: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Same code without gotoSame code without goto$test = true; $test = true; while($test) while($test) { {

switch($var) switch($var) { { case 1: case 1:

$var = 3;$var = 3; break; break;

case 2:case 2: case 7: case 7:

$var = 9;$var = 9; break; break;

default: default: $var += 5; $var += 5; $test = false; $test = false; break; break;

}} $var -= 1;$var -= 1; } }

Page 9: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Main arguments against GOTOMain arguments against GOTO

Makes your code unreadable to the Makes your code unreadable to the others others

Will lead to spaghetti code really fast Will lead to spaghetti code really fast GOTOs are in strong opposition to GOTOs are in strong opposition to

code reusability and object code reusability and object orientation orientation

Its a pain to debug GOTOs Its a pain to debug GOTOs

Page 10: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Unreadable code:Unreadable code:If you write code as in previous If you write code as in previous

“spaghetti” code example then it will be “spaghetti” code example then it will be unreadable. You must stick to using it unreadable. You must stick to using it infrequently or only when it makes the infrequently or only when it makes the program easier to write.program easier to write.

Page 11: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Will lead to spaghetti code really fastWill lead to spaghetti code really fast

You must be a responsible You must be a responsible programmer and not use gotos programmer and not use gotos everwhere in your program so that it everwhere in your program so that it leads to spaghetti code.leads to spaghetti code.

Metaphor of driving a porche vs. Metaphor of driving a porche vs. driving a hondadriving a honda

Page 12: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

no reusability and object orientationno reusability and object orientation

Object orientation is a way to organize your Object orientation is a way to organize your program as a whole, at a completely other program as a whole, at a completely other level than the implementation itself level than the implementation itself

You musnt confuse object oriented programs You musnt confuse object oriented programs with ‘high-level’ programs and goto with ‘high-level’ programs and goto statements with “low-level” programming.statements with “low-level” programming.

Page 13: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

ReusabilityReusability

Same argument as previous slide. Same argument as previous slide. Object orientation does not create Object orientation does not create reusable code, programmers doreusable code, programmers do

cannot debugcannot debug

Justified use of GOTO statements Justified use of GOTO statements does not hinder debugging does not hinder debugging

Page 14: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Proper use of gotoProper use of goto

The problem with using The problem with using goto goto isn’t the isn’t the construct itself construct itself

It’s the use of It’s the use of gotogoto in the in the appropriate places. The appropriate places. The gotogoto statement can be a useful tool in statement can be a useful tool in structuring program flow structuring program flow

Page 15: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Example use of goto statmentExample use of goto statment

while (true)while (true){{

read in a valueread in a valueif (value == xl)if (value == xl)thenthengoto value_foundgoto value_foundelseelseprocess the valueprocess the value

}}Value_found:Value_found://...//...

Page 16: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Same code without gotoSame code without goto

read in a valueread in a value

while (value != sentinel)while (value != sentinel)

{{

process the valueprocess the value

read in a valueread in a value

}}

//...//...

Page 17: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

the second approach has two major the second approach has two major drawbacks: drawbacks:

First, it duplicates the statement to First, it duplicates the statement to read in a value. Duplication of code read in a value. Duplication of code leads to maintenance problems:leads to maintenance problems:

Modification of the code at one place Modification of the code at one place must be repeated at every other must be repeated at every other place where it has been duplicatedplace where it has been duplicated

Page 18: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

The key to writing solid code is to write The key to writing solid code is to write code that reads naturally. code that reads naturally.

What you’re trying to do is: You read in a What you’re trying to do is: You read in a value. If the value is a sentinel – you stop. value. If the value is a sentinel – you stop. Otherwise, you process the value and Otherwise, you process the value and continue to read the next value. continue to read the next value.

second piece of code reverses your natural second piece of code reverses your natural way of thinking about the problem way of thinking about the problem

Page 19: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

When GOTO is necessaryWhen GOTO is necessary

Sometimes certain code is very hard Sometimes certain code is very hard to write without the use of a GOTOto write without the use of a GOTO

Page 20: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

int first_zero_row = -1; /* none */int first_zero_row = -1; /* none */int i, j;int i, j;

for (i = 0; i < n; i++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) for (j = 0; j < n; j++)

{ { if (A[i][j]) goto next; if (A[i][j]) goto next; } }

first_zero_row = i; first_zero_row = i; break; break; next: ; next: ; } }

Page 21: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Gotos and better Memory managementGotos and better Memory management

int method1(){ int method1(){ char *ptr = malloc(1024); char *ptr = malloc(1024); char *ptr1 = malloc(1024);char *ptr1 = malloc(1024);char *ptr2 = malloc(1024); char *ptr2 = malloc(1024);

if ( method2(ptr) != 0 ){if ( method2(ptr) != 0 ){ free(ptr); free(ptr);

free(ptr1);free(ptr1); free(ptr2); free(ptr2); return 0; } return 0; }

if ( method3(ptr1) != 0)if ( method3(ptr1) != 0){{

free(ptr); free(ptr); free(ptr1); free(ptr1); free(ptr2); free(ptr2); return 0; return 0;

} } if ( method4(ptr2) != 1)if ( method4(ptr2) != 1){ {

free(ptr);free(ptr); free(ptr1); free(ptr1);

free(ptr2); free(ptr2); return 1; return 1;

} } free(ptr);free(ptr);free(ptr1);free(ptr1);free(ptr2); free(ptr2); } }

This is a lot of extra This is a lot of extra code and its all messy code and its all messy and when this method and when this method grows, in each return grows, in each return we have to free any we have to free any allocated memory. If allocated memory. If programmer forgets, programmer forgets, then method1 is run then method1 is run many times, it may many times, it may result in a core dump result in a core dump or segmentation fault or segmentation fault

Page 22: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

Better memory management with gotoBetter memory management with goto

int method1(){ int method1(){ char *ptr = malloc(1024)char *ptr = malloc(1024)char *ptr1 = alloc(1024);char *ptr1 = alloc(1024);char *ptr2 = alloc(1024);char *ptr2 = alloc(1024);int ret = 0;int ret = 0;

if ( method2(ptr) != 0 )if ( method2(ptr) != 0 ){{ ret=0; ret=0;

goto done; goto done; } } if ( method3(ptr1) != 0)if ( method3(ptr1) != 0){ {

ret=0; ret=0; goto done; goto done;

} } if ( method4(ptr2) != 1)if ( method4(ptr2) != 1){{ ret=1; ret=1; goto done; goto done; } }

done: done: free(ptr); free(ptr); free(ptr1); free(ptr1); free(ptr2); free(ptr2); return ret; } return ret; }

This is This is cleaner, lot cleaner, lot less prone to less prone to memory memory leaks, as leaks, as there is only there is only one exit point one exit point and code is and code is much cleaner much cleaner to understand to understand

Page 23: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

ConclusionConclusion

Goto statements are harmful when Goto statements are harmful when they are used carelesslythey are used carelessly

If responsible programming is used If responsible programming is used then goto statement can be of a then goto statement can be of a great benefit.great benefit.

Page 24: GOTO CONSIDERED HARMFUL Tarana Yar. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968,

THE ENDTHE END