1 / 19

F h

F h. Fachhochschule Flensburg. Refactoring Überarbeiten von Programmcode Hans Werner Lang. Software ist Literatur. Software wird von Menschen geschrieben und von Menschen gelesen. Software.

haracha
Télécharger la présentation

F h

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. Fh Fachhochschule Flensburg Refactoring Überarbeiten von Programmcode Hans Werner Lang

  2. Software ist Literatur Software wird von Menschen geschrieben und von Menschen gelesen. Software Für mich ist die Literatur eine Form der Freude. Wenn wir etwas mit Mühe lesen, so ist der Autor gescheitert. Jorge Luis Borges (argentinischer Schriftsteller, 1899-1986) H. W. Lang (deutscher Informatiker)

  3. Prinzipien der Softwarekonstruktion Einfachheit Rapid Prototyping Refactoring "Weniger Teile" Schild im Büro eines japanischen Unternehmensgründers, der in Deutschland studiert hatte "Make it work first before you make it work fast." "Essentially, three: Revise, revise, revise." C.A.R. Hoare auf die Frage, ob er Tipps habe, wie man einen guten wissenschaftlichen Artikel schreibt

  4. Einfachheit "Ich bin ein Berliner." (J.F. Kennedy) "Wir sind Papst." (Bild-Zeitung) "Ja, wir sind ein Paar." (Anne Will)

  5. Programme sollen sein ... einfach knapp lesbar elegant schön korrekt

  6. Lesbarkeit von Programmen Benennung von Klassen, Variablen, Funktionen Formatierung (in Python kein Thema) Kommentare

  7. Benennung von Klassen, Variablen, Funktionen Sprache: Englisch Klassen: Position, Placement, Client Variablen: i, p, pos, lastmove, startfields Funktionen: Rückgabetyp boolean: isValid(), hasNext(), comesDigit() void: findMove(), setPosition() andere: evaluationOf(), bestMove(), getPosition() Konstanten: G, MAX_SIZE, ALPHANUMERIC

  8. Benennung von Variablen Lokale Variablen, Parameter: kurz if array_to_be_sorted[currentIndex1-1]>array_to_be_sorted[currentIndex1]: temporaryValue=array_to_be_sorted[currentIndex1-1] array_to_be_sorted[currentIndex1-1]=array_to_be_sorted[currentIndex1] array_to_be_sorted[currentIndex1]=temporaryValue Besser so: if a[i-1]>a[i]: t=a[i-1] a[i-1]=a[i] a[i]=t Globale Variablen, Objektvariablen: länger lastmove, startfieldsusw.

  9. Kommentare vor Funktionen: # prueft, ob alle vorhandenen Nachbarfelder # von Position p frei sind def hasOnlyFreeNeighbours(self, p): hinter Anweisungen: self.hand.clear() # alle Steine entfernen nicht: i=i+1 # i um 1 erhoehen

  10. Einfachheit Sonderfälle vermeiden Berechnung von n! if (n==0 || n==1) f=1; else { f=n; for (i=n-1; i>1; i=i-1) f=f*i; } Besser so: f=1; for (i=1; i<=n; i=i+1) f=f*i;

  11. Eleganz Quicksort void quicksort (int lo, int hi) { int i=lo, j=hi, x=a[(lo+hi)/2]; while (i<=j) { while (a[i]<x) i++; while (a[j]>x) j--; if (i<=j) exchange(i++, j--); } if (lo<j) quicksort(lo, j); if (i<hi) quicksort(i, hi); }

  12. Refactoring code

  13. Introduce symbolic constant double potentialEnergy() { returnmass*9.81*height; } static final double G=9.81; double potentialEnergy() { return mass*G*height; }

  14. Introduce parameter object double amountReceived(Date start, Date end) { ... } double amountDelivered(Date start, Date end) { ... } class DateRange { Date start, end; } double amountReceived(DateRange range) { ... } double amountDelivered(DateRange range) { ... }

  15. Encapsulate field class Complex { public double re, im; } class Complex { private double re, im; public getRe() { return re; } public getRe() { return re; } }

  16. Extract method void printOwing() { printBanner(); // print details System.out.println("Name: "+name); System.out.println("Amount: "+amount); } void printOwing() { printBanner(); printDetails(); } void printDetails() { System.out.println("Name: "+name); System.out.println("Amount: "+amount); }

  17. Replace method with object class Order { double getPrice() { double baseprice, secondaryprice; // long computation of price .... } } class Order { double getPrice() { return new PriceCalculator.compute(); } } class PriceCalculator { private double baseprice, secondaryprice; public double compute() { // computation of price } }

  18. Replace inheritance with delegation class Stack extends Vector { public void push(Object element) { insertElement(element, 0); } } class Stack { private Vector v; public void push(Object element) { v.insertElement(element, 0); } }

More Related