Live Hacking - Durch Refactoring zu sauberem Code

19
Live Hacking Durch Refactoring zu sauberem Code

Transcript of Live Hacking - Durch Refactoring zu sauberem Code

Live Hacking Durch Refactoring zu sauberem Code

Roland Mast Sybit GmbH Software-Entwickler [email protected]

Roland Mast Sybit GmbH Software-Entwickler [email protected]

0

5

10

15

20

25

30

35

40

19

27

37

6

1

Refactorings in IDEs

0

5

10

15

20

25

30

35

40

NetBeans7.1

Eclipse 4.4Luna

IDEA 14 VisualStudio

2013 C#

MSVC 6

19

27

37

6

1

Clean Code

Print Primes

Refactoring

Clean Code

Print Primes

Refactoring

• Keep it simple, stupid (KISS)

• Don‘t Repeat Yourself (DRY)

• Single Responsibility Principle (SRP)

• Single Level of Abstractions (SLA)

• Principle of Least Astonishment

• Refactoring

• …

Clean Code Prinzipien

www.clean-code-developer.de

www.clean-code-developer.de

Clean Code

Print Primes

Refactoring

• Berechnet 1000 Primzahlen und gibt diese formatiert aus

• 1983 generiert mit dem WEB Tool von Donald E. Knuth

„I chose the name WEB partly because it was one of the few three-letter

words of English that hadn’t already been applied to computers.” http://www.literateprogramming.com/knuthweb.pdf

Quelle: Robert C. Martin – Clean Code

Print Primes

@* Printing primes: An example of \WEB.

The following program is essentially the same as Edsger Dijkstra's @^Dijkstra, Edsger@> ''first example of step-wise program composition,'' found on pages 26—39 of his {\sl Notes on Structured Programming},$^\Dijk$ but it has been translated into the \WEB\ language. @.WEB@>

\[Double brackets will be used in what follows to enclose comments relating to \WEB\

...

an informal top-level description.\]

@p @<Program to print the first thousand prime numbers@>

PRIMES.WEB

@<Program to print the first thousand prime numbers@>=

program print_primes(output);

const @!m=1000;

@<Other constants of the program@>@;

var @<Variables of the program@>@;

begin @<Print the first |m| prime numbers@>;

end.

PRIMES.WEB

{1:}{2:}PROGRAM PRINTPRIMES(OUTPUT);

CONST M=1000;{5:}RR=50;CC=4;WW=10;{:5}{19:}

ORDMAX=30;{:19}VAR{4:}

P:ARRAY[1..M]OF INTEGER;{:4}{7:}

PAGENUMBER:INTEGER;PAGEOFFSET:INTEGER;

ROWOFFSET:INTEGER;C:0..CC;{:7}{12:}J:INTEGER;

K:0..M;{:12}{15:}JPRIME:BOOLEAN;{:15}{17:}

ORD:2..ORDMAX;SQUARE:INTEGER;{:17}{23:}

N:2..ORDMAX;{:23}{24:}

MULT:ARRAY[2..ORDMAX]OF INTEGER;{:24}

BEGIN{3:}{11:}{16:}J:=1;K:=1;P[1]:=2;{:16}

{18:}ORD:=2;SQUARE:=9;{:18};

WHILE K<M DO BEGIN{14:}REPEAT J:=J+2;{20:}

IF J=SQUARE THEN BEGIN ORD:=ORD+1;{21:}

SQUARE:=P[ORD]*P[ORD];{:21}{25:}

MULT[ORD-1]:=J;{:25};END{:20};{22:}N:=2;

JPRIME:=TRUE;

WHILE(N<ORD)AND JPRIME DO BEGIN{26:}

WHILE MULT[N]<J DO MULT[N]:=MULT[N]+P[N]+P[N]

;IF MULT[N]=J THEN JPRIME:=FALSE{:26};N:=N+1;

END{:22};UNTIL JPRIME{:14};K:=K+1;P[K]:=J;

END{:11};{8:}BEGIN PAGENUMBER:=1;

PAGEOFFSET:=1;

WHILE PAGEOFFSET<=M DO BEGIN{9:}

BEGIN WRITE('The First ');WRITE(M:1);

WRITE(' Prime Numbers --- Page ');

WRITE(PAGENUMBER:1);WRITELN;WRITELN;

FOR ROWOFFSET:=PAGEOFFSET TO PAGEOFFSET+RR-1

DO{10:}

BEGIN FOR C:=0 TO CC-1 DO IF ROWOFFSET+C*RR<=

M THEN WRITE(P[ROWOFFSET+C*RR]:WW);WRITELN;

END{:10};PAGE;END{:9};

PAGENUMBER:=PAGENUMBER+1;

PAGEOFFSET:=PAGEOFFSET+RR*CC;END;END{:8}{:3};

END.{:2}{:1}

Generierter Pascal-Code

Clean Code

Print Primes

Refactoring

• Einfache Refactorings • Extract Variable, Parameter, Field, Constant

• Extract Method,

• Inline

• Refactoring von Legacy Code • Statische Methoden umwandeln: ConvertToInstanceMethod

• Konvertierung Array -> List: TypeConversion

• Extract Interface and replace usage

• ReplaceConstructorWithBuilder

Refactorings

Als Mathe-Lehrer möchte ich Primzahlen tabellarisch in verschiedenen Ansichten erstellen, um sie den Schülern im Unterricht als Handout verteilen zu können. Verifiziere, dass • die Anzahl von Spalten und Zeilen einstellbar ist • die Seitenüberschrift einstellbar ist • die Parameter in main() als Konstanten angegeben werden

können (Argumentübergabe an die main-Methode ist nicht Bestandteil dieser Übung und soll weggelassen werden)

• der vorhandene Primzahlen-Algorithmus weiter verwendet wird • die fixe Anzahl von 1000 Primzahlen generiert wird

Neue Anforderung

Let‘s refactor!

Yop

• Keinen Clean Code ohne Refactoring

• IDE-Unterstützung für Java sehr gut, aber nicht perfekt

• Kompliziertere Refactorings in Einzelschritte zerlegen

• Training notwendig (Coding Katas)

• Unit Tests sind ein Muss und geben notwendige Sicherheit

Fazit