-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
482 lines (425 loc) · 19.1 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Object Calisthenics</title>
<link rel="stylesheet" href="reveal.js/css/reveal.min.css">
<link rel="stylesheet" href="reveal.js/css/theme/solarized.css" id="theme">
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="css/custom.css">
<script>
document.write('<link rel="stylesheet" href="reveal.js/css/print/' +
(window.location.search.match(/print-pdf/gi) ? 'pdf' : 'paper') +
'.css" type="text/css" media="print">'
);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Object Calisthenics</h1>
<em>Your code sucks, let's fix it!</em>
</section>
<section>
<section data-markdown>
<script type="text/template">
## Object
Related to **O**bject **O**riented **P**rogramming.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Calisthenics
Term derived from greek, _exercise_,<br>under the context of gymnastics.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Object Calisthenics
**Jeff Bay**, in **The ThoughtWorks Anthology**, lists
**9 rules** to writing better Object Oriented code.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Motivation
Readable Code, Comprehensible, Testable, Maintainable.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 1. Only One Level Of<br>Indentation Per Method
</script>
</section>
<section>
<h2>Yay!</h2>
<pre><code class="java highlight-comments">class Board {
public String board() {
StringBuilder buf = new StringBuilder();
// 0
for (int i = 0; i < 10; i++) {
// 1
for (int j = 0; j < 10; j++) {
// 2
buf.append(data[i][j]);
}
buf.append("\n");
}
return buf.toString();
}
}</code></pre>
</section>
<section>
<h2>Extract Method</h2>
<pre><code class="java">public String board() {
StringBuilder buf = new StringBuilder();
collectRows(buf);
return buf.toString();
}</code></pre>
<pre><code class="java">private void collectRows(StringBuilder buf) {
for (int i = 0; i < 10; i++) {
collectRow(buf, i);
}
}
private void collectRow(StringBuilder buf, int row) {
for (int i = 0; i < 10; i++) {
buf.append(data[row][i]);
}
buf.append("\n");
}
</code></pre>
<small>
<a href="http://refactoring.com/catalog/extractMethod.html">http://refactoring.com/catalog/extractMethod.html</a>
</small>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 2. Don't Use The ELSE Keyword
</script>
</section>
<section>
<h2>Example</h2>
<pre><code class="java">public void login(String username, String password) {
if (userRepository.isValid(username, password)) {
redirect('homepage');
} else {
addFlash('error', 'Bad credentials');
redirect('login');
}
}</code></pre>
</section>
<section>
<h2>Early Return</h2>
<pre><code class="java">public void login(String username, String password) {
if (!userRepository.isValid(username, password)) {
addFlash('error', 'Bad credentials');
return redirect('login');
}
redirect('homepage');
}</code></pre>
</section>
<section>
<h2>Variable</h2>
<pre><code class="java">public void login(String username, String password) {
String redirectRoute = 'homepage';
if (!userRepository.isValid(username, password)) {
addFlash('error', 'Bad credentials');
redirectRoute = 'login';
}
redirect(redirectRoute);
}</code></pre>
</section>
<section data-markdown>
<script type="text/template">
## But Also
* Polymorphism
* Null Object Pattern
* Strategy Pattern
* State Pattern
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 3. Wrap All Primitives And Strings
</script>
</section>
<section data-markdown>
<script type="text/template">
Encapsulate all the primitives within objects.
<small>
<a href="http://c2.com/cgi/wiki?PrimitiveObsession">http://c2.com/cgi/wiki?PrimitiveObsession</a>
</small>
</script>
</section>
<section data-markdown>
<script type="text/template">
If the variable of primitive type has a **behavior**,
<br>it **MUST** be encapsulated.
</script>
</section>
<section data-markdown>
<script type="text/template">
It is especially _true_ for **Domain Driven Design**.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 4. First Class Collections
</script>
</section>
<section data-markdown>
<script type="text/template">
Any class that contains a collection should contain
<br>no other member variables.
</script>
</section>
<section data-markdown>
<script type="text/template">
Each **collection** gets wrapped **in its own class**, so now behaviors
related to the collection have a home.
<br>
(e.g. filter methods, applying a rule to each element)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 5. One Dot Per Line
</script>
</section>
<section data-markdown>
<script type="text/template">
## Law of Demeter
Only talk to your **immediate friends**, don't talk to strangers.
</script>
</section>
<section>
<h2>Example</h2>
<pre><code class="java">class Location { class Piece {
public Piece current; public String representation;
} }
class Board {
public String boardRepresentation() {
StringBuilder buf = new StringBuilder();
for (Location loc : squares()) {
buf.append(loc.current.representation.substring(0, 1));
}
return buf.toString();
}
}</code></pre>
</section>
<section>
<h2>Talk To Your Friends!</h2>
<pre><code class="java">class Location {
private Piece current;
public void addTo(StringBuilder buf) { current.addTo(buf); }
}
class Piece {
private String representation;
public String character() {
return representation.substring(0, 1);
}
public void addTo(StringBuilder buf) {
buf.append(character());
}
}</code></pre>
</section>
<section>
<h2>Talk To Your Friends!</h2>
<pre><code class="java">// Before:
// buf.append(loc.current.representation.substring(0, 1));
// After:
class Board {
public String boardRepresentation() {
StringBuilder buf = new StringBuilder();
for (Location location : squares()) {
location.addTo(buf);
}
return buf.toString();
}
}</code></pre>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 6. Don't Abbreviate
</script>
</section>
<section data-markdown>
<script type="text/template">
### Why Do You Want To Abbreviate?
</script>
</section>
<section>
<h3>Write The Same Name Over And Over Again?</h3>
<p class="fragment">Then, your method is reused multiple times.</p>
<p class="fragment">Looks like <strong>code duplication</strong>.</p>
</section>
<section>
<h3>Method Name Too Long?</h3>
<p class="fragment">Maybe your class has multiple responsabilities.</p>
<p class="fragment">Violation of the <strong>Single Responsibility Principle</strong>.</p>
</section>
<section data-markdown>
<script type="text/template">
> If you can't find a decent name for a class or a method,
> something is probably wrong with your conception.
> Rethink!
<em><small>Me — 2 hours ago</small></em>
<br>
<small>
<a href="http://williamdurand.fr/2012/01/24/designing-a-software-by-naming-things/">
http://williamdurand.fr/2012/01/24/designing-a-software-by-naming-things/
</a>
</small>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 7. Keep All Entities Small
</script>
</section>
<section data-markdown>
<script type="text/template">
No class over **50 lines** and no package over 10 files.
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 8. No Classes With More Than Two Instance Variables
</script>
</section>
<section data-markdown>
<script type="text/template">
## Yes, I know!
</script>
</section>
<section data-markdown>
<script type="text/template">
## Why?
Low cohesion, better encapsulation.
</script>
</section>
<section>
<h2>How?</h2>
<img src="images/2-instance-variables.png" />
<small>
<a href="https://github.com/TheLadders/object-calisthenics#rule-8-no-classes-with-more-than-two-instance-variables">
https://github.com/TheLadders/object-calisthenics#rule-8-no-classes-with-more...
</a>
</small>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## 9. No Getters/Setters/Properties
</script>
</section>
<section data-markdown>
<script type="text/template">
Tell, don't ask.
<small>
<a href="http://pragprog.com/articles/tell-dont-ask">http://pragprog.com/articles/tell-dont-ask</a>
<br>
<a href="http://c2.com/cgi/wiki?TellDontAsk">http://c2.com/cgi/wiki?TellDontAsk</a>
</small>
</script>
</section>
<section data-markdown>
<script type="text/template">
Getters/Setters violate the **Open/Closed Principle**.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Getters/Setters Are Evil
// Game
private int score;
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
// Usage
game.setScore(game.getScore() + ENEMY_DESTROYED_SCORE);
<span></span>
// Game
public void addScore(int delta) {
score += delta;
}
// Usage
game.addScore(ENEMY_DESTROYED_SCORE);
<small>
<a href="http://stackoverflow.com/questions/565095/are-getters-and-setters-evil">http://stackoverflow.com/questions/565095/are-getters-and-setters-evil</a>
<br>
<a href="http://whitewashing.de/2012/08/22/building_an_object_model__no_setters_allowed.html">http://whitewashing.de/2012/08/22/building_an_object_model__no_setters_allowed.html</a>
</small>
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
## Recap'
1. Only One Level Of Indentation Per Method
2. Don't Use The ELSE Keyword
3. Wrap All Primitives And Strings
4. First Class Collections
5. One Dot Per Line
6. Don't Abbreviate
7. Keep All Entities Small
8. No Classes With More Than Two Instance Variables
9. No Getters/Setters/Properties
</script>
</section>
<section data-markdown>
<script type="text/template">
## Thank You, Questions?
<br>
<ul class="me">
<li><a href="http://williamdurand.fr" title="website">williamdurand.fr</a></li>
<li><a href="https://github/willdurand" title="github">github.com/willdurand</a></li>
<li><a href="https://twitter.com/couac" title="twitter">twitter.com/couac</a></li>
</ul>
</script>
</section>
</div>
</div>
<script type="text/javascript" src="reveal.js/lib/js/head.min.js"></script>
<script type="text/javascript" src="reveal.js/js/reveal.min.js"></script>
<script type="text/javascript">
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme,
transition: Reveal.getQueryHash().transition || 'concave',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
//{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
//{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>