-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactoring.html
632 lines (571 loc) · 26.3 KB
/
refactoring.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Refactoring – Oder wie wir das Design von bestehender Software verbessern um anfälle geistigerr Umnachtung zu verhindern</title>
<meta name="description" content="Refactoring – Oder wie wir das Design von bestehender Software verbessern um anfälle geistigerr Umnachtung zu verhindern">
<meta name="author" content="Jasper Bartscher">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/monokai.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Intro -->
<section>
<h1>Refactoring</h1>
<h3>Wie Sie das Design vorhandener Software verbessern</h3>
<h6>Oder: The mind boggling positive effects of refactoring on sanity and happiness</h6>
<p>
<small>Created by <a href="http://bartscher.io">Jasper Bartscher</a></small>
</p>
</section>
<!-- Was ist dieser Vortrag und was Nicht? -->
<section>
<h2>Was ist dieser Vortrag und was Nicht?</h2>
<h4><font color="red">Dieser Vortrag ist nicht</font></h4>
<span style="display: inline-block;" class="fragment fade-right">dazu gedacht eine Anleitung zum Refactoring zu sein</span>
<h4><font color="green">Dieser Vortrag ist </font></h4>
<span style="display: inline-block;" class="fragment fade-right">ein Appetitanreger um Refactoring in Ihren Arbeitsprozess einfließen zu lassen</span>
</section>
<!-- Definition -->
<section>
<h2>kurze Defintion</h2>
<blockquote cite="http://searchservervirtualization.techtarget.com/definition/Our-Favorite-Technology-Quotations">
“Refactoring ist der Vorgang, ein funktionsfähiges Programm zu nehmen und dessen Wert zu erhöhen, indem wir sein Verhalten nicht ändern, aber ihm mehr Eigenschaften geben, die es uns ermöglichen, es mit hohem Tempo weiterzuentwickeln.“ - Kent
Beck
</blockquote>
</section>
<!-- Motivation -->
<section>
<h2>kurze Motivation</h2>
<blockquote cite="http://searchservervirtualization.techtarget.com/definition/Our-Favorite-Technology-Quotations">
“Ich bin immer bereit hart zu arbeiten, um möglichst wenig zu arbeiten.“ - Ich
</blockquote>
</section>
<!-- Vorraussetzungen -->
<section>
<h2>Vorraussetzungen zum Refactoring</h2>
<p class="fragment grow highlight-green">der bestehende Code muss funktionieren!</p>
<p class="fragment highlight-red">kaputten Code muss man reparieren oder neuschreiben!</p>
<p class="fragment highlight-blue">Code muss "allen" gehören!</p>
<span style="display: inline-block;" class="fragment fade-right">Tests, erstmal nur <span class="fragment highlight-blue">funktionale Tests</span>, noch keine <span class="fragment highlight-current-blue">Unit Tests</span></span>
<p class="fragment highlight-red">Eine Versionsverwaltung! (git, svn ...)</span>
</section>
<!-- Zeichen das Refactoriert werden muss -->
<section>
<h2>Zeichen, dass Refactoriert werden muss</h2>
<ul>
<li>Ständiger Zeitdruck nach neuen Features</li>
<li>Abhetzen von Deadline zu Deadline</li>
<li>Unzufriede/gestresste Mitarbeiter</li>
<li class="fragment highlight-red">Code Smells und Antipatterns</li>
<li>Schwer lesbarer/verständlicher Code</li>
<li>Änderungen am Code erzeugen Seiteneffekte</li>
<li>Komplexe Bedingungen und/oder Entscheidungsbäume</li>
</ul>
</section>
<!-- Code Smells -->
<section>
<h2>Code Smells</h2>
<ul>
<li>Lange Methoden</li>
<li>Lange Klassen</li>
<li>Lange Parameterlisten</li>
<li>Neid</li>
<li>Redundanter/ duplizierter Code</li>
<li>Divergente Änderungen</li>
<li>Schrotkugeln</li>
<li>switch-case</li>
<li>Viele elementare Typen</li>
<li>Viele Kommentare<span style="display: inline-block;" class="fragment fade-left highlight-red">?</span></li>
<li><a href="https://de.wikipedia.org/wiki/Code-Smell">und viele weitere ...</a></li>
</ul>
</section>
<!--Antipatterns-->
<section>
<h4><br/>Antipatterns</h4>
<ul>
<li>Big Ball of Mud ⇒ Design Patterns, Software Architecture</li>
<li>Gottobject ⇒ aufteilen in Objekte</li>
<li>Sumo-Hochzeit⇒ DB-Abstraktionsschicht</li>
<li>Lavafluss ⇒ Code Coverage Test</li>
<li>Zwiebel ⇒ Refakturierung des bestehenden Codes anstatt "ranklatschen" von Features</li>
<li>Magic Numbes ⇒ Enumerationen oder Polymorphie</li>
</ul>
</section>
<!-- Vorteile & Nachteile -->
<section data-background-transition="zoom" data-background="#4d7e65">
<h2>Vorteile des Refactorings</h2>
<ul>
<li>Zeitgewinn beim Einarbeiten von neuen Mitarbeitern in bestehende Projekte</li>
<li>Genereller Zeitgewinn beim Programmieren</li>
<li>Schafft tieferes Verständnis für den vorliegen Code</li>
<li>Zunahme der Arbeitsqualität</li>
<li>Schnelle Anpassungen am Code möglich</li>
</ul>
</section>
<section data-background-transition="zoom" data-background="#b5533c">
<h2>Nachteile des Refactorings</h2>
<ul>
<li>Falsch ausgeführt schaufelt sich der Entwickler sein eigenes Grab</li>
<li>Unfertiges Refactoring ist wie Schulden machen</li>
<li>Besser zuerst die Architektur planen als wild drauflos programmieren</li>
<li>Zu starke Indirektion</li>
</ul>
</section>
<!-- Code Beispiele Refactoring -->
<!-- Extract Method - Methoden verkürzen -->
<section>
<!-- Extract Method - Methoden verkürzen -->
<section>
<h2>Methode Extrahieren</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1,6-10">
void printOwing(double previousAmount) {
Enumeration e = _orders.elements();
double outstanding = previousAmount * 1.2;
printBanner();
// calculate outstanding
while (e.hasMoreElements()) {
Order each = (Order) e.nextElement();
outstanding += each.getAmount();
}
printDetails(outstanding);
}
</code>
</pre>
</section>
<section>
<pre>
<code class="hljs" data-trim data-line-numbers="3,5-14">
void printOwing(double previousAmount) {
printBanner();
double outstanding = getOutstanding(previousAmount * 1.2);
printDetails(outstanding);
}
double getOutstanding(double initialValue) {
double result = initialValue;
Enumeration e = _orders.elements();
while (e.hasMoreElements()) {
Order each = (Order) e.nextElement();
result += each.getAmount();
}
return result;
}
</code>
</pre>
<ul>
<li>höhere Chance das Methoden wiederverwendet werden</li>
<li>higher-level Methoden lesen sich wie "Prosa"</li>
</ul>
</section>
</section>
<!-- Replace Conditional with Polymorphism -->
<section>
<!-- Replace Conditional with Polymorphism -->
<section>
<h2>Replace Conditional with Polymorphism</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="4-10">
class Swallow {
// ...
double getSpeed() {
switch (type) {
case EUROPEAN:
return getBaseSpeed();
case AFRICAN:
return getBaseSpeed() - getLoadFactor() * numberOfCoconuts;
case NORWEGIAN_BLUE:
return (isNailed) ? 0 : getBaseSpeed(voltage);
}
throw new RuntimeException("Should be unreachable");
}
}
</code>
</pre>
</section>
<section>
<pre>
<code class="hljs" data-trim data-line-numbers="1-4,23">
abstract class Swallow {
abstract double getSpeed();
}
class European extends Swallow {
double getSpeed() {
return getBaseSpeed();
}
}
class African extends Swallow {
double getSpeed() {
return getBaseSpeed() - getLoadFactor() * numberOfCoconuts;
}
}
class NorwegianBlue extends Swallow {
double getSpeed() {
return (isNailed) ? 0 : getBaseSpeed(voltage);
}
}
// Somewhere in client code
speed = bird.getSpeed();
</code>
</pre>
<ul>
<li>Tell-Don’t-Ask principle: Nicht nach dem Objekt-state fragen sondern Auftrag erteilen und das Objekt das "wie" entscheiden lassen</li>
<li>Extrem einfache Integration von neuen "Conditionals"</li>
</ul>
</section>
</section>
<!-- Nullobjekte -->
<section>
<!-- Nullobjekte -->
<section>
<h2>Nullobjekte</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="2">
// ...
if( customer == null){
plan = BillingPlan.basic();
}
else{
customer.getPlan();
}
</code>
</pre>
</section>
<section>
<img src="Null-pattern.jpg">
<ul>
<li>Referenz auf die Variable verweist zu jedem Zeitpunkt auf ein gültiges Objekt, was Behandlungen von Sonderfällen (Nichtvorhandensein) erübrigt.</li>
<li>Als Konstant und Singleton implementiert</li>
<li>Code Redundanz sinkt, Fehler bei keiner Überprüfung vermieden</li>
</ul>
</section>
</section>
<!-- Fehlercodes durch Exceptions ersetzen -->
<section>
<!-- Fehlercodes durch Exceptions ersetzen -->
<section>
<h2>Fehlercode durch Ausnahme ersetzten</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="3,6">
int withdraw(int amount){
if(amount > _balance)
return -1;
else{
_balance -= amount;
return 0;
}
}
</code>
</pre>
</section>
<section>
<pre>
<code class="hljs" data-trim data-line-numbers="1,2">
void withdraw(int amount) throws BalanceException{
if(amount > _balance) throw new BalanceException();
_balance -= amount;
}
</code>
</pre>
<ul>
<li>Trennung von Verarbeitung und Fehlerbehandlung</li>
<li>Erweiterbare Objekte, keine Basisdatentypen mehr</li>
<li>Weiterreichen an die Stelle, die diese behandeln soll (Voraussetztung OOP)</li>
<li>Nicht übertreiben!</li>
</ul>
</section>
</section>
<!-- Klasse extrahieren -->
<section>
<!-- Klasse extrahieren -->
<section>
<h2>Klasse extrahieren</h2>
<pre>
<code class="hljs" data-trim data-line-numbers="1">
class Person {
name,
officeAreaCode,
officeNumber,
getTelephoneNumber()
}
</code>
</pre>
</section>
<section>
<pre>
<code class="hljs" data-trim data-line-numbers="1,6">
class Person {
name,
getTelephoneNumber()
}
class TelephoneNumber {
areaCode,
number,
getTelephoneNumber()
}
</code>
</pre>
<ul>
<li>Single-Responsibility-Prinzip</li>
<li>Verbesserung der Testbarkeit</li>
<li>Stärkere Abstraktion und Aufgabentrennung</li>
<li>Einfachere Änderung und Erweiterung der Klassen</li>
</ul>
</section>
</section>
<!-- Code den wir dann am Laptop Refacturieren -->
<!--
<section>
<section>
<h4>Refactoring - eine Filmausleihe</h4>
<pre><code class="hljs" data-trim data-line-numbers="3-8,10">
public class Movie{
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
private String _title;
private String _priceCode;
public Movie(String title, int priceCode){
_title = title;
_priceCode = priceCodce
}
/*
* ... setter und getter für priceCode und title
*/
} </code></pre>
</section>
<section>
<pre><code class="hljs" data-trim data-line-numbers="3-4,6">
public class Rental{
private Movie _movie;
private int _daysRented;
public Rental(Movie movie, int _daysRented){
_movie = movie;
_daysRented = daysRented;
}
public int getDaysRented(){
return _daysRented;
}
public int getMovie(){
return _movie;
}
} </code></pre>
</section>
<section>
<pre><code class="hljs" data-trim data-line-numbers="1-60">
public class Customer{
private String _name;
private int _rentals = new ArrayList<Rentals>;
public Customer(String name){
_name = name;
}
public int addRental(Rental arg){
_rentals.add(arg);
}
public int getName(){
return _name;
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints= 0;
String result = "Rental Record for " + getName() + "\n";
while (_rentals.hasMoreElements()){
double thisAmount = 0;
Rental each = (Rental) _rentals.nextElement();
//Beträge pro Zeile ermitteln
switch (each.getMovie().getPriceCode()){
case Movie.REGULAR:
thisAmount += 2;
if(each.getDaysRented() > 2)
thisAmount += (each.getDaysRented() - 2) * 1.5;
case Movie.NEW_RELEASE:
thisAmount += each.getDaysRented()) * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if(each.getDaysRented() > 3)
thisAmount += (each.getDaysRented() - 3) * 1.5;
break;
}
//Bonuspunkte aufaddieren
frequentRenterPoints ++;
//Bonuspunkte für zweittägige Ausleihe einer Neuerscheinung
if(each.getMovie().getPriceCode() == Movie.NEW_RELEASE && each.getDaysRented() > 1)
frequentRenterPoints ++;
//Zahlen für die Aussleihe ausgeben
result = += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;
}
//Fusszeile ausgeben
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "Youeve earned" + String.valueOf(frequentRenterPoints)+ " frequent penter points";
return result;
}
} </code></pre>
</section>
</section>
<section> -->
<section>
<h4><br/>Alles sehr schön informativ aber wo fange ich jetzt an?</h4>
<ul>
<li>Was ist eigentlich mein Client-Code und was ist mein Libary-Code?</li>
<li>Wo finde ich offensichtliche Codesmells und Anti-Patterns?</li>
<li>Verstehe ich den <span class="fragment highlight-red">Roten Faden</span> einer Applikation oder zerfasert er?</li>
<li>Kommentare sagen <span class="fragment highlight-red">WAS</span> aber nicht <span class="fragment highlight-green">WARUM</span> Dinge so sind wie sie sind</li>
</ul>
</section>
</section>
<!-- Empfehlung & Rausschmeißer -->
<section>
<h2>Abschließende Worte</h2>
<ul>
<span style="display: inline-block;" class="fragment fade-right"><li>Refactorieren ist wie Sport, wir sollten Ihn machen weil er gut für uns ist, können uns aber manchmal nur schwer motivieren.</li></span>
<span style="display: inline-block;" class="fragment fade-right"><li>Knabbern statt schlingen</li></span>
<span style="display: inline-block;" class="fragment fade-right"><li>Pfadfinderprinzip anwenden</li></span>
<span style="display: inline-block;" class="fragment fade-right"><li>Schlau (toolgestützt) arbeiten</li></span>
<span style="display: inline-block;" class="fragment fade-right"><li>Fehler liegen lassen und erst im Anschluss beheben</li></span>
<span style="display: inline-block;" class="fragment fade-right"><li>Es erscheint anfangs mühselig, geht aber schnell ins Blut über</li></span>
</ul>
</section>
<section>
<h2><br/>Empfehlung</h2>
<img src="https://images-na.ssl-images-amazon.com/images/I/51K-M5hR8qL._SX392_BO1,204,203,200_.jpg" style="width:50px;height:100px;">
<h4><a href="https://www.amazon.de/Refactoring-Improving-Design-Existing-Technology/dp/0201485672/">Deep Dive</a></h4>
<h4> <a href="https://refactoring.com/catalog/">schnelle schematische Erklärung</a><h4>
<h4> <a href="https://github.com/HugoMatilla/Refactoring-Summary">git Repo mit allen Refactoing Techniken und Code Beispielen</a></h4>
<h4> <a href="https://refactoring.guru/">gut aufbereitete Webseite zum Thema Refactoring</a></h4>
</section>
<section data-background="https://media.giphy.com/media/YV3vOINigsezsVO7Xu/giphy.gif">
<h2>Ich hoffe ich konnte euch etwas für Refactoring begeistern</h2>
</section>
</div>
</div>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
center: true,
hash: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [{
src: 'plugin/markdown/marked.js',
condition: function() {
return !!document.querySelector('[data-markdown]');
}
}, {
src: 'plugin/markdown/markdown.js',
condition: function() {
return !!document.querySelector('[data-markdown]');
}
}, {
src: 'plugin/highlight/highlight.js',
async: true
}, {
src: 'plugin/search/search.js',
async: true
}, {
src: 'plugin/zoom-js/zoom.js',
async: true
}, {
src: 'plugin/notes/notes.js',
async: true
}, ]
});
</script>
<!-- Paste the following into your Reveal.js index.html to add +/- buttons to every <code> section -->
<style>
.reveal .fullscreen {
width: 1000px;
height: 600px;
position: fixed;
top: 0;
left: 0;
z-index: 100;
max-height: none;
margin-top: -20px;
}
.reveal .plus,
.reveal .minus {
color: #d3d3d3;
z-index: 200;
font-size: 32px;
position: absolute;
top: 0px;
}
.reveal .plus {
left: 880px;
}
.reveal .minus {
left: 990px;
display: none;
}
.reveal .plus:hover,
.reveal .minus:hover {
cursor: pointer;
color: #000;
}
@media print {
.reveal .minus,
.reveal .plus {
display: none;
}
}
</style>
<script>
// Add maximize/minimize "buttons" to parents of hljs divs
var removeClass = function(classList, className) {
return classList.split(' ').filter(function(c) {
return c !== className
}).join(' ');
}
hs = [].slice.call(document.getElementsByClassName('hljs'))
hs.forEach(function(el) {
var plus = document.createElement('span'),
minus = document.createElement('span');
plus.appendChild(document.createTextNode('+'));
el.parentElement.appendChild(plus);
plus.addEventListener('click', function() {
// undo vertical centering on current slide:
[].slice.call(document.getElementsByClassName('present')).forEach(function(p) {
p.style.top = 0
});
el.className = el.className + ' fullscreen';
minus.style.display = 'block';
this.style.display = 'none';
});
plus.className = 'plus';
minus.appendChild(document.createTextNode('-'));
el.parentElement.appendChild(minus);
minus.addEventListener('click', function() {
el.className = removeClass(el.className, 'fullscreen');
plus.style.display = 'block';
this.style.display = 'none';
});
minus.className = 'minus';
});
</script>
</body>
</html>