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

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Advertisements

Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Recursion CS 367 – Introduction to Data Structures.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
Statements and Control Issues By Chris Bradney. Overview of topics Typical Control Statements in Programming Issues with Return statements Recursion and.
Programming Creating programs that run on your PC
ISBN Chapter 8 Statement-Level Control Structures.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Introduction to a Programming Environment
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
PRE-PROGRAMMING PHASE
Python quick start guide
Programming Languages CPS120: Introduction to Computer Science Lecture 5.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
High-Level Programming Languages: C++
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
CHAPTER 1 Overview of Programming and Problem Solving.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Department of CSE, MIT, Manipal SOFTWARE ENGINEERING By Prakash Kalingrao Aithal.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
 Thursday: › Team Presentations › Risk Assessment and project plan due 11:55 pm  Friday: › Help on coding/testing  Monday: › HW 5 due, 11:55 pm.
CPS120 Introduction to Computer Science Iteration (Looping)
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
CPS120: Introduction to Computer Science Decision Making in Programs.
ISBN Chapter 8 Statement-Level Control Structures.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
I Power Higher Computing Software Development High Level Language Constructs.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
CPS120 Introduction to Computer Science Iteration (Looping)
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
High-level language programming paradigms. Programming languages come in many forms or 'paradigms'. Each form of language offers advantages over other.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Structured Programming The Basics
Def: A control structure is a control statement and
Software Engineering (LECT 1)
Quick Test What do you mean by pre-test and post-test loops in C?
Agenda Control Flow Statements Purpose test statement
Control Structures.
Flow of Control.
Flow of Control.
Control statements Simple statements Basic structured statements
Structured Programming Taken from notes by Dr. Neil Moore
Flow of Control.
Coding Constructs considered Violations of Structured Programming
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

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. Topic debated till the late 1970s, when the debate died down

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. 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. It is the fundamental operation which can be used for transfer of control from one part of a program to another.

GOTO is found in FORTRAN, Algol, COBOL, SNOBOL, BASIC, C, C++, Pascal, Perl and many other languages, particularly assembly languages GOTO is found in FORTRAN, Algol, COBOL, SNOBOL, BASIC, C, C++, Pascal, Perl and many other languages, particularly assembly languagesFORTRANAlgol COBOLSNOBOLBASICCC++ PascalPerlFORTRANAlgol COBOLSNOBOLBASICCC++ PascalPerl 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. 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.

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

One of E.W. Dijkstra’s arguments in his paper was that the use of goto statements produces incomprehensible spaghetti code 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. 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.GOTOsspaghettiGOTOsspaghetti

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

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

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

Unreadable code: 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.

Will lead to spaghetti code really fast 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

no reusability and object orientation 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.

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

Proper use of goto The problem with using goto isn’t the construct itself 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 It’s the use of goto in the appropriate places. The goto statement can be a useful tool in structuring program flow

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

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

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

The key to writing solid code is to write code that reads naturally. 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. 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 second piece of code reverses your natural way of thinking about the problem

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

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

Gotos and better Memory management int method1(){ char *ptr = malloc(1024); char *ptr1 = malloc(1024); char *ptr2 = malloc(1024); if ( method2(ptr) != 0 ){ free(ptr); free(ptr);free(ptr1); free(ptr2); free(ptr2); return 0; } if ( method3(ptr1) != 0) { free(ptr); free(ptr);free(ptr1);free(ptr2); return 0; } if ( method4(ptr2) != 1) {free(ptr); free(ptr1); 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 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

Better memory management with goto int method1(){ char *ptr = malloc(1024) char *ptr1 = alloc(1024); char *ptr2 = alloc(1024); int ret = 0; if ( method2(ptr) != 0 ) if ( method2(ptr) != 0 ){ ret=0; ret=0; goto done; } if ( method3(ptr1) != 0) {ret=0; goto done; } if ( method4(ptr2) != 1) { ret=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 This is cleaner, lot less prone to memory leaks, as there is only one exit point and code is much cleaner to understand

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

THE END THE END