1 / 24

GOTO

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

kellam
Download Presentation

GOTO

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GOTO CONSIDERED HARMFUL Tarana Yar

  2. The publishing of the paper entitled “Go To Statement Considered Harmful”, written by Edsger W. Dijkstra in 1968, marked the beginning of the debate on whether it is appropriate to use the goto statement in a well-structured program. Topic debated till the late 1970s, when the debate died down

  3. What is goto? • GOTO is a command found in many programming languages which instructs the computer to jump to another point in the computer program. • It is the fundamental operation which can be used for transfer of control from one part of a program to another.

  4. GOTO is found in FORTRAN, Algol, COBOL, SNOBOL, BASIC, C, C++, Pascal, Perl and many other languages, particularly assembly languages • In the assembly languages, the GOTO command is usually called BRA (from "branch"), JMP or JUMP, and is often the only way of organizing program flow.

  5. Simple Example of GOTO • If ( a == 5) goto aLabel Else{ a = a -5 b++ } aLabel: a = b+1

  6. One of E.W. Dijkstra’s arguments in his paper was that the use of goto statements produces incomprehensible spaghetti code • Spaghetti code is a term for code with a complex and tangled control structure, especially one using many GOTOs, exceptions, or other "unstructured" branching constructs. It is named such because program flow tends to look like a bowl of spaghetti.

  7. $test = 1; loop: if($test == 0) goto loopend; if($var == 1) { $var = 3; goto preloop; } If($var == 2) goto var_7; if($var == 7) { var_7: $var = 9; goto preloop; } $var = $var + 5; $test = 0; preloop: $var = $var - 1; goto loop; loopend: Example of spaghetti code

  8. Same code without goto $test = true; while($test) { switch($var) { case 1: $var = 3; break; case 2: case 7: $var = 9; break; default: $var += 5; $test = false; break; } $var -= 1; }

  9. Main arguments against GOTO • Makes your code unreadable to the others • Will lead to spaghetti code really fast • GOTOs are in strong opposition to code reusability and object orientation • Its a pain to debug GOTOs

  10. Unreadable code: If you write code as in previous “spaghetti” code example then it will be unreadable. You must stick to using it infrequently or only when it makes the program easier to write.

  11. Will lead to spaghetti code really fast You must be a responsible programmer and not use gotos everwhere in your program so that it leads to spaghetti code. Metaphor of driving a porche vs. driving a honda

  12. no reusability and object orientation Object orientation is a way to organize your program as a whole, at a completely other level than the implementation itself You musnt confuse object oriented programs with ‘high-level’ programs and goto statements with “low-level” programming.

  13. Reusability Same argument as previous slide. Object orientation does not create reusable code, programmers do • cannot debug Justified use of GOTO statements does not hinder debugging

  14. Proper use of goto • The problem with using goto isn’t the construct itself • It’s the use of goto in the appropriate places. The goto statement can be a useful tool in structuring program flow

  15. Example use of goto statment while (true) { read in a value if (value == xl) then goto value_found else process the value } Value_found: //...

  16. Same code without goto read in a value while (value != sentinel) { process the value read in a value } //...

  17. the second approach has two major drawbacks: First, it duplicates the statement to read in a value. Duplication of code leads to maintenance problems: Modification of the code at one place must be repeated at every other place where it has been duplicated

  18. The key to writing solid code is to write code that reads naturally. • What you’re trying to do is: You read in a value. If the value is a sentinel – you stop. Otherwise, you process the value and continue to read the next value. • second piece of code reverses your natural way of thinking about the problem

  19. When GOTO is necessary • Sometimes certain code is very hard to write without the use of a GOTO

  20. int first_zero_row = -1; /* none */ int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (A[i][j]) goto next; } first_zero_row = i; break; next: ; }

  21. int method1(){ char *ptr = malloc(1024); char *ptr1 = malloc(1024); char *ptr2 = malloc(1024); if ( method2(ptr) != 0 ){ free(ptr); free(ptr1); free(ptr2); return 0; } if ( method3(ptr1) != 0) { free(ptr); free(ptr1); free(ptr2); return 0; } if ( method4(ptr2) != 1) { free(ptr); free(ptr1); free(ptr2); return 1; } free(ptr); free(ptr1); free(ptr2); } This is a lot of extra code and its all messy and when this method grows, in each return we have to free any allocated memory. If programmer forgets, then method1 is run many times, it may result in a core dump or segmentation fault Gotos and better Memory management

  22. int method1(){ char *ptr = malloc(1024) char *ptr1 = alloc(1024); char *ptr2 = alloc(1024); int ret = 0; if ( method2(ptr) != 0 ) { ret=0; goto done; } if ( method3(ptr1) != 0) { ret=0; goto done; } if ( method4(ptr2) != 1) { ret=1; goto done; } done: free(ptr); free(ptr1); free(ptr2); return ret; } This is cleaner, lot less prone to memory leaks, as there is only one exit point and code is much cleaner to understand Better memory management with goto

  23. Conclusion • Goto statements are harmful when they are used carelessly • If responsible programming is used then goto statement can be of a great benefit.

  24. THE END

More Related