Zuverlässige Software mit GoTo Ludwig Suski 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 1
Gliederung Historischer Hintergrund Dijkstra: Go To Statement Considered Harmful [1] Notwendigkeit? - GoTo-Elimination Warum GoTo in höheren Sprachen? Basis für neue Kontrollstrukturen Effizienz Abschlussbemerkungen Quellen 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 2
Historischer Hintergrund 1 Dijkstra (1968): [...] the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. [1] Problem: Programmierer eine neue Spezies fehlende Erfahrung bzw. Disziplin GoTo als direktes Ausdrucksmittel sehr intuitiv exzessive Nutzung verringert Lesbarkeit 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 3
Historischer Hintergrund 2 /* find Element x in A, insert if not * present yet. Increase counter at * found index in B. A[m+1] := x; i:=1; go to test; loop: i := i + 2; test: if A[i]=x then go to found fi; if A[i+1] x then go to loop fi; i := i + 1; found: if i>m then m := i; B[i] := 1; else B[i] := B[i]+1 fi; Struktur nicht intuitiv erkennbar dennoch sehr einfaches Beispiel Bsp 1: Suchalgorithmus von Knuth ([2] S. 268) 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 4
GoTo-Elimination 1 streng mechanische Lösungen kein Problem: Befehlszähler in SW aber: kompletter Strukturverlust y a := 1 a=0 n a=1 n a=2 n a=n n y y y 1 2 (n-1) n Aus: Wolf, A Case against the GOTO [5] 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 5
GoTo-Elimination 2 weitere Methoden existieren [3] Defizite in Laufzeit, Speicherverbrauch oder Textgröße GoTos nachträglich eliminieren wenig sinnvoll GoTo-frei (strukturiert) entwerfen 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 6
Warum GoTo in höheren Sprachen? Kontrollstrukturen: individuelle Lösungen Lesbarkeit in manchen Fällen besser Effizienz: detaillierte Optimierung kritischer Schleifen Fein-Tuning Vermeidung von Rekursion 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 7
Warum GoTo? - Kontrollstrukturen 1 Programmiersprachen nicht a priori ausgereift fehlende Kontrollstrukturen mittels GoTo nachempfinden /* find and print index * of x in array a for (i=0; i<max; i++) { if(a[i] == x) break; } if (i!=max) print( i:,i); else print( not found. ); /* find and print index * of x in array a for (i=0; i<max; i++) { if(a[i] == x) goto next; } next: if (i!=max) print( i:,i); else print( not found. ); Bsp 2a: for-schleife mit break verlassen Bsp 2b: for-schleife mit goto verlassen 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 8
Warum GoTo? - Kontrollstrukturen 2 zwei weitere Ansatze links: illegaler Feldzugriff möglich rechts: zusätzliche Variable nötig /* find and print index * of x in array a while (i<max && a[i]!=x) i++; if (i!=max) print( i:,i); else print( not found. ); /* find and print index * of x in array a bool found = false; while (i<max &&!found) { if (a[i++]==x) found = true; } if (found) print( i:,i 1); else print( not found. ); Bsp 2c: konjugierte Bedingung Bsp 2d: Flag zur Ablaufkontrolle 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 9
Warum GoTo? - Kontrollstrukturen 3 Weiteres Beispiel: Universalstruktur für Iteration deckt while-do und do-while ab for-schleife direkt umsetzbar /* universal loop structure * (as thought by Dahl) loop: S; while B: // leave if false T; repeat; // from loop /* universal loop structure * (simulated with goto) repeat: S; if not B then goto next; T; forever; next: Bsp 3a: universelle Schleife nach Ole-Johan Dahl ([2] S. 279) Bsp 3b: universelle Schleife Synthese mit GoTo 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 10
Warum GoTo? - Kontrollstrukturen 4 Weitere strukturierte Sprünge: Ausnahmebehandlung ( C) Beliebige Rücksprünge aus Funktionen ( Pascal) Event-Indicators (nach C. T. Zahn in [2], S. 275): loop until <event>1 or <event>2 or...or <event>n: <statement list>0; repeat; then <event>1 => <statement list>1; <event>2 => <statement list>2;... <event>n => <statement list>n; fi; 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 11
Warum GoTo? Effizienz 1 kritische Schleifen (~3%) dominieren die Programmgeschwindigkeit (Knuth, [2], S. 268) Optimierung dieser Bereiche lohnt sich! VORSICHT: Optimierung nur auf ausgereifte Schleifen anwenden! strukturiert entworfen, analysiert, debuggt, auf funktionale Korrektheit geprüft premature optimization is the root of all evil. (Knuth [2], S. 268) 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 12
Warum GoTo? - Effizienz 2 /* A[m+1] := x; i:= 1; * while A[i] x do i := i+1; * if i>m then m := i; B[i]:=1; * else B[i] := B[i]+1 fi; A[m+1] := x; i:=1; go to test; loop: i := i + 2; test: if A[i]=x then go to found fi; if A[i+1] x then go to loop fi; i := i + 1; found: if i>m then m := i; B[i] := 1; else B[i] := B[i]+1 fi; Optimierung als Folge erprobter Transformationen Bsp.: Schleifenkörper ausrollen Laufzeit: 3,5n zu 4n ([2], S. 267f) Gewinn: 12,5% betrachten wieder: Bsp 1: Suchalgorithmus von Knuth ([2] S. 267f) ^ 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 13
Warum GoTo? - Effizienz 3 Elimination von Rekursion ([2], S. 280-282) /* binary tree traversal * (print infix) procedure treeprint(t); [..] if t 0 then treeprint(l[t]); print(a[t]); treeprint(r[t]); fi; /* binary tree traversal * (print infix) procedure treeprint(t); [..] L:if t 0 then treeprint(l[t]); print(a[t]); t:=r[t]; go to L; fi; /* binary tree traversal * (print infix) procedure treeprint(t); [..] begin integer stack S;[..] L1: loop while t 0: S<=t; t:=l[t]; go to L1; L2: t<=s; print(a[t]); t:=r[t]; repeat; if nonempty(s) then go to L2 fi; end; Bsp 4a Bsp 4b Bsp 4c 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 14
Abschlussbemerkungen Dem Programmierer sein Werkzeug! er muss es verantwortungsvoll einsetzen; erst Strukturieren, dann optimieren nur spezielle Restriktionen sinnvoll in C: keine interprozeduralen Sprünge GoTo-Freiheit kein indikator für gute SW GoTo nur ein Auslöser für Debatten über Programmierstil und -disziplin? chaotische Programme auch ohne GoTo möglich! 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 15
Zuverlässige Software mit GoTo Vielen Dank für Ihre Aufmerksamkeit! 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 16
Quellen [1] Dijkstra: Go To Statement Considered Harmful Communications of the ACM, Vol. 11, No. 3, March 1968 [2] Knuth: Structured Programming with go to Statements Computing Surveys, Vol. 6, No. 4, December 1974 [3] Johnson: The Elimination of Goto-Patches? SIGPLAN Notices, Vol. 24, No. 3, 1989 [4] Hopkins: A Case for the GOTO Classics in Software Engineering, YOURDON, 1979, S. 101-112 [5] Wulf: A Case against the GOTO Classics in Software Engineering, YOURDON, 1979, S. 83-98 12.06.2009 Zuverlässige Software mit GoTo - Ludwig Suski 17