-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
858 lines (829 loc) · 26.9 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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Object Calisthenics</title>
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/custom.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 ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section>
<h3 class="top-2">Make your code great again with the</h3>
<h2>Object Calisthenics</h2>
<p class="me">Maxence POUTORD</p>
<p class="me-link">
<a href="https://twitter.com/_maxpou"><i class="fa fa-twitter"></i> @_maxpou</a> |
<a href="http://www.maxpou.fr/"><i class="fa fa-globe"></i> maxpou.fr</a> |
<a href="mailto:[email protected]"><i class="fa fa-envelope-o"></i> [email protected]</a>
</p>
</section>
</section>
<section>
<section>
<h2 class="top-1">Object Calisthenics?</h2>
<blockquote>
Cal • is • then • ics - /ˌkaləsˈTHeniks/
</blockquote>
<ul>
<li class="fragment" data-fragment-index="0">Object: OOP context</li>
<li class="fragment" data-fragment-index="1">Calisthenics: gymnastics context</li>
</ul>
<br>
<img data-src="img/gym.gif" class="fragment" data-fragment-index="1" style="border:none;width: 25%;">
</section>
<section>
<h2 class="top-1">Object Calisthenics</h2>
<p>In the ThoughtWorks Anthology, Jeff Bay, list 9 rules for writing <b>better Object Oriented code</b>.</p>
<img data-src="img/thoughtWorks-anthology.jpg" style="border:none;width: 25%;">
</section>
<section>
<h2 class="top-1">Motivation</h2>
<p>readability, maintainability, testability, and comprehensibility</p>
<img data-src="img/super_elk.gif" style="border:none;">
<p>...make your OO code great again!</p>
</section>
<aside class="notes">
and velocity
</aside>
</section>
<!-- STEP 1 -->
<section>
<section>
<h2 class="top-3">#1: One level of indentation<br> per method</h2>
</section>
<section>
<img data-src="img/indent-power.jpeg" style="border:none;width: 80%;">
</section>
<section>
<h2>Code (re)organisation:<br> Early return</h2>
<pre class="fragment push" data-fragment-index="0"><code class="php" data-trim>
public someFunction(someParameter)
{
if (firstCondition) {
if (secondCondition) {
// Do Something
// Do Something
// Do Something
}
}
}
</code></pre>
<pre class="fragment push" data-fragment-index="1"><code class="php" data-trim>
public someFunction(someParameter)
{
if (!firstCondition) {
return;
}
if (secondCondition) {
// Do Something
// Do Something
// Do Something
}
}
</code></pre>
<pre class="fragment push" data-fragment-index="2"><code class="php" data-trim>
public someFunction(someParameter)
{
if (!firstCondition) {
return;
}
if (!secondCondition) {
return;
}
// Do Something
// Do Something
// Do Something
}
</code></pre>
<br>
<p class="fragment" data-fragment-index="2">
"Single Entry, Single Exit"...?
</p>
<p class="fragment" data-fragment-index="3">
"Single Entry, Single Exit" <i class="fa fa-arrow-right" aria-hidden="true"></i> was written for assembly languages like Fortran/COBOL
</p>
<aside class="notes">
<a href="http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from">More reading</a>
</aside>
</section>
<section>
<h2>Too much indentation level with loop?</h2>
<p class="fragment" data-fragment-index="0">
You may need to split your code into multiple functions.<br> This is the <a href="http://refactoring.com/catalog/extractMethod.html">Extract Method</a> (cf. Martin Fowler).
</p>
<pre class="fragment push" data-fragment-index="0"><code class="php" data-trim>
//Class Command
public function validateProductList(array $products): array
{
$validProducts[];
foreach ($products as $product)
{
if (!$product->price > 0 && length($product->name)) {
$validProducts[] = $product;
}
}
return $validProducts;
}
</code></pre>
<pre class="fragment push" data-fragment-index="1"><code class="php" data-trim>
//Class Command
public function validateProductList(array $products): array
{
return array_filter($products, 'isValidProduct');
}
public function isValidProduct(Product $product): boolean
{
if (!$product->price > 0 && length($product->name)) {
return true;
}
return false;
}
</code></pre>
</section>
<section>
<h2 class="top-2">Remove useless checks</h2>
<pre class="fragment push" data-fragment-index="0"><code class="php" data-noescape data-trim>
function foo($products)
{
if (isset($products) && count($products)) {
foreach ($products as $product) {
# code
}
}
}
</code></pre>
<pre class="fragment push" data-fragment-index="1"><code class="php" data-noescape data-trim>
function foo($products)
{
<mark>if (isset($products) && count($products)) {</mark>
foreach ($products as $product) {
# code
}
<mark>}</mark>
}
</code></pre>
<pre class="fragment push" data-fragment-index="2"><code class="php" data-noescape data-trim>
function foo($products)
{
foreach ($products as $product) {
# code
}
}
</code></pre>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Easier to read</li>
<li>Reduce cyclomatic complexity</li>
<li>Respect KISS principle</li>
<li>Single Responsability Principle ("S" in SOLID)</li>
</ul>
</section>
</section>
<!-- STEP 2 -->
<section>
<section>
<h2 class="top-3">#2: Don’t use the ELSE keyword</h2>
</section>
<section>
<h2 class="top-1">Code (re)organisation</h2>
<pre class="fragment push" data-fragment-index="0"><code class="php" data-trim>
public function login($username, $password)
{
if ($this->isValid($username, $password)) {
redirect("homepage");
} else {
addFlash("error", "Bad credentials");
redirect("login");
}
}
</code></pre>
<pre class="fragment push" data-fragment-index="1"><code class="php" data-trim>
public function login($username, $password)
{
if ($this->isValid($username, $password)) {
return redirect("homepage");
} else {
addFlash("error", "Bad credentials");
return redirect("login");
}
}
</code></pre>
<pre class="fragment push" data-fragment-index="2"><code class="php" data-trim>
public function login($username, $password)
{
// defensive approach
if ($this->isValid($username, $password)) {
return redirect("homepage");
}
addFlash("error", "Bad credentials");
return redirect("login");
}
</code></pre>
<pre class="fragment push" data-fragment-index="3"><code class="php" data-trim>
public function login($username, $password)
{
// optimistic approach
if (!$this->isValid($username, $password)) {
addFlash("error", "Bad credentials");
return redirect("login");
}
return redirect("homepage");
}
</code></pre>
</section>
<section>
<h2 class="top-2">Not enough?</h2>
<ul>
<li>State Pattern</li>
<li>Strategy Pattern</li>
<li>NullObject Pattern</li>
<li>...</li>
</ul>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Easier to read</li>
<li>Reduce cyclomatic complexity</li>
</ul>
</section>
</section>
<!-- STEP 3 -->
<section>
<section>
<h2 class="top-3">#3: Wrap all primitives and Strings</h2>
</section>
<section>
<h2>Example</h2>
<pre class="sticky"><code class="php" data-trim>
class User
{
/** @var string */
private $name;
/** @var string */
private $email;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
}
</code><code class="php fragment" data-trim>
//this is ok
$myUser = new User("John", "[email protected]");
</code><code class="php fragment" data-trim>
//this is ok
$anotherUser = new User("Max", "0811223344");
</code><code class="php fragment" data-trim>
//this is ok
$whoeverUser = new User("Louise", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
</code><code class="php fragment" data-trim>
//this is ok
$whoeverUserBis = new User("Lucie", "打才我醫集老電了快洋,西足並良步細主!");
</code></pre>
</section>
<section>
<h2>Proposition - Value Object</h2>
<pre class="sticky"><code class="php" data-trim>
class Email
{
/** @var string */
private $email;
public function __construct(string $email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL) {
throw new InvalidArgumentException("Invalid format!");
}
$this->email = $email;
}
}
</code><code class="php fragment" data-trim>
// This is ok
$myUser = new User("John", new Email("[email protected]"));
</code><code class="php fragment" data-trim>
// This is NOT ok
$anotherUser = new User("Max", new Email("0811223344"));
</code><code class="php fragment" data-trim>
// This is NOT ok
$whoeverUser = new User("Louise", new Email("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."));
</code><code class="php fragment" data-trim>
// This is NOT ok
$whoeverUserBis = new User("Lucie", new Email("打才我醫集老電了快洋,西足並良步細主!"));
</code></pre>
</section>
<section>
<h2 class="top-2">Should I wrap all primitives and strings?</h2>
<br>
<h2 class="fragment" style="color: red">NO!</h3>
</section>
<section>
<h2 class="top-1">Be pragmatic!</h2>
<h4>only wrap primitive with behaviour:</h4>
<ul>
<li>UUID</li>
<li>BIC, IBAN</li>
<li>email, URL</li>
<li>IP address</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Stop abusing arrays!</h2>
<pre class="sticky fragment push" data-fragment-index="0"><code class="php" data-trim>
$command = array(
array(
"user" => array(
"id" => 42,
"name" => "Maxence",
"address" => "Dublin"
),
"date" => "1989-04-11 07:28:47",
"beers" => array(
array(
"ID" => "76b44e1a-a8d6-11e6-80f5-76304dec7eb7",
"name" => "Tripel Karmeliet",
"quantity" => 4,
),
array(
"ID" => "76b45220-a8d6-11e6-80f5-76304dec7eb7",
"name" => "Journeyman's Pale Ale",
"quantity" => 8,
),
array(
"ID" => "76b45356-a8d6-11e6-80f5-76304dec7eb7",
"name" => "Duvel tripel hop",
"quantity" => 15,
)
)
)
);
</code></pre>
<div class="fragment push" data-fragment-index="1">
<ul>
<li><a href="https://en.wikipedia.org/wiki/Big_ball_of_mud">Big ball of mud anti-pattern</a></li>
<li>No defined structure</li>
<li>Comprehension is time consuming</li>
<li>Needless complexity</li>
<li>Procedural programming is hard to scale</li>
</ul>
<pre><code class="php" data-trim>
foreach ($command["beers"] as $beer) {
if (isset($beer["quantity"])) {
$itemsCounter += $beer["quantity"];
}
}
// ...or
$itemsCounter = $command->countItems();
</code></pre>
</div>
<aside class="notes">
<a href="http://nomad.so/2014/06/stop-abusing-arrays-in-php/">stop abusing arrays in php</a>
</aside>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Type hinting</li>
<li>Easier to reuse</li>
<li>Prevent code duplication</li>
</ul>
</section>
</section>
<!-- STEP 4 -->
<section>
<section>
<h2 class="top-3">#4: First class collections</h2>
</section>
<section>
<h2>Why?</h2>
<pre><code class="php" data-trim>
class Company
{
private $name;
private $employes;
# code
}
</code></pre>
<p class="fragment">Where can I put filters/contains/... methods?</p>
<pre class="fragment"><code class="php" data-trim>
class Team
{
private $name;
private $employes;
# code
}
</code></pre>
<p class="fragment"><i class="fa fa-arrow-right" aria-hidden="true"></i> 'employes' collection need to be wrapped in its own class</p>
</section>
<section>
<h2 class="top-1">Example</h2>
<pre><code class="php" data-trim>
class EmployeesCollection
{
private $employes;
public function add(Employe $employe) {/** ... **/};
public function remove(Employe $employe) {/** ... **/};
public function count() {/** ... **/};
public function filter(Closure $p): {/** ... **/};
//...
}
</code></pre>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Readability</li>
<li>Easier to merge collection and not worry about member behaviour in them</li>
</ul>
</section>
</section>
<!-- STEP 5 -->
<section>
<section>
<h2 class="top-3">#5: One dot per line</h2>
<i>...or an arrow (->) if you use PHP!</i>
</section>
<section>
<h2 class="top-2">A.K.A. Law of Demeter (LoD)</h2>
<blockquote>
"Don't talk to strangers. Only talk to your immediate friends"
</blockquote>
</section>
<section>
<h2>What are strangers?</h2>
<pre><code class="php" data-trim>
$objectA->getObjectB()->getObjectC();
</code></pre>
<img src="img/lod.png" style="background-color:white;">
</section>
<section>
<h2 class="top-2">Example</h2>
<pre><code class="php" data-trim>
$objectA->getThis()->getThat()->getSomethingElse()->doSomething();
</code></pre>
<p><i class="fa fa-question-circle" aria-hidden="true"></i> What happen if getThat() return NULL?</p>
</section>
<!-- <section>
<blockquote style="margin-top: 20%;">
"you can play with your toys, with toys that you make, and with toys that someone gives you. You don’t ever, ever play with your toy’s toys."
<br> — Jeff BAY
</blockquote>
</section> -->
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Law of Demeter</li>
<li>Readability</li>
<li>Easier to debug</li>
</ul>
</section>
</section>
<!-- STEP 6 -->
<section>
<section>
<h2 class="top-3">#6: Don’t abbreviate</h2>
</section>
<section>
<h2 class="top-2">Why do you abbreviate?</h2>
<ul>
<li class="fragment">
It's repeated many times<br>
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Code duplication
</li>
<li class="fragment">
The method name is too long <br>
<i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Violates the Single Responsibility Principle
</li>
</ul>
</section>
<section>
<h2>1st SOLID Principle</h2>
<p><b>S</b> mean Single Responsibility Principle (SRP)</p>
<video src="img/fancy-beer.webm" id="main-video" autoplay="" loop=""></video>
</section>
<section>
<h2>Tips</h2>
<ul>
<li>Class and method names with 1-2 words</li>
<li>Be explicit! <br>
<i><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Wrong/non explicit names often leads to errors.</i>
</li>
<li>Naming is too hard? Ask yourself if this class/method/... make sense!</li>
</ul>
<blockquote>
There are only two hard things in Computer Science:<br> cache invalidation and naming things.
<br> — Phil Karlton
</blockquote>
</section>
<section>
<h2 class="top-2">It's all about reading!</h2>
<pre><code class="php" data-trim>
foreach ($users as $u) {
// code
// code
// code
// code
// code
$u->sendMail($aMail); // "$u": what does it stand for?
}
</code></pre>
</section>
<section>
<h2 class="top-2">Avoid tautologies</h2>
<pre><code class="php" data-trim>
class Manager
{
function perform()
{
// ...
}
}
</code></pre>
<p>You can also replace perform by <code>process()</code>...</p>
</section>
<section>
<h2>Ubiquitous Language<br>to the rescue!</h2>
<img data-src="img/stairs.gif" style="border:none;width: 80%;background-color: #FFF">
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Readability</li>
<li>Communication</li>
<li>Help to reveal underlying problems</li>
</ul>
</section>
</section>
<!-- STEP 7 -->
<section>
<section>
<h2 class="top-3">#7: Keep all entities small</h2>
</section>
<section>
<h2 class="top-2">Avoid God Object</h2>
<p><i> An object that knows too much or does too much</i></p>
</section>
<section>
<h2 class="top-3">Rule #7: Keep <u>everything</u> small</h2>
<br>
<blockquote cite="http://">
"Simplicity Comes from Reduction"
<br> — Paul W. Homer
</blockquote>
<aside class="notes">
simplicity is the ultimate sophistication
</aside>
</section>
<section>
<h2 class="top-1">Why?</h2>
<p>Long files are hard to:</p>
<ul>
<li>read</li>
<li>understand</li>
<li>maintain</li>
<li>reuse</li>
</ul>
</section>
<section>
<h2 class="top-2"><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> Code Gravity <i class="fa fa-exclamation-triangle" aria-hidden="true"></i></h2>
<p><i>"big stuff attracts even more stuff and gets bigger"</i></p>
<p><i>≈ theory of the broken window</i></p>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Easier to reuse</li>
<li>Better code segregation</li>
<li>Clear methods and their objectives</li>
<li>Single Responsability Principle ("S" in SOLID)</li>
</ul>
</section>
</section>
<!-- STEP 8 -->
<section>
<section>
<h2 class="top-3">#8: No classes with several instance variables</h2>
</section>
<section>
<h2 class="top-1">High cohesion, and better encapsulation</h2>
<p>Try to keep <s style="color:#FF5733">2</s> 5 instance variables (max)</p>
<pre><code class="php" data-trim>
class MyWonderfulService
{
private $anotherService;
private $logger;
private $translator;
private $entityService;
// code
}
</code></pre>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Loose coupling / Easier to reuse</li>
<li>Better encapsulation</li>
<li>Short dependency list</li>
<li>Single Responsability Principle ("S" in SOLID)</li>
</ul>
</section>
</section>
<!-- STEP 9 -->
<section>
<section>
<h2 class="top-3">#9: No getters/setters/properties</h2>
<aside class="notes">
<ul>
<li>http://whitewashing.de/2012/08/22/building_an_object_model__no_setters_allowed.html</li>
</ul>
</aside>
</section>
<section>
<h2 class="top-2">Why do we use GETTER/SETTER so much?</h2>
<ul>
<li>Frameworks <i>(i.e. Symfony's entities)</i></li>
<li>IDE generate them automatically</li>
<li>We can change every properties we need<br> and also... we are lazy!</li>
</ul>
</section>
<section>
<h2 class="top-2">Tell, don’t ask principle</h2>
<blockquote cite="http://">
"Don't ask for the information you need to do the work; ask the object that has the information to do the work for you"
<br> — Allen Holub
</blockquote>
</section>
<section>
<h2>What is better?</h2>
<pre><code class="php" data-trim>
// Game
private $score;
public function setScore(int $score): void {
$this->score = $score;
}
public function getScore(): int {
return score;
}
// Usage
$game->setScore($game->getScore() + ENEMY_DESTROYED_SCORE);
</code></pre>
<pre class="fragment"><code class="php" data-trim>
// Game
public function addScore(int $delta): void {
this->score += $delta;
}
// Usage
$game->addScore(ENEMY_DESTROYED_SCORE);
</code></pre>
</section>
<section>
<h2>What is the difference between...</h2>
<div class="row">
<div class="col-md-6">
<pre><code class="php" data-trim>
class Employe
{
private $name;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
}
</code></pre>
</div>
<div class="col-md-6 fragment">
<pre><code class="php" data-trim>
class Employe
{
public $name;
}
</code></pre>
</div>
</div>
</section>
<section>
<h2 class="top-2">Benefits</h2>
<ul>
<li>Avoid to violate the <abbr title="open for extension, but closed for modification">Open/Closed principle</abbr> (the <b>O</b> in SOLID)</li>
<li>Easier to read</li>
</ul>
</section>
</section>
<section>
<img data-src="img/object-calisthenics.png" style="border:none;width: 80%;">
</section>
<section>
<section>
<h2 class="top-1">Reading</h2>
<ul>
<li><a href="http://code.joejag.com/2016/anti-if-the-missing-patterns.html">Anti-If: The missing patterns</a> by Joe Wright</li>
<li><a href="http://www.maxpou.fr/else-unnecessary-expression/">"Else": the unnecessary expression</a> by me :)</li>
<li><a href="http://www.yegor256.com/2014/09/16/getters-and-setters-are-evil.html">Getters/Setters. Evil. Period.</a> by Yegor Bugayenko</li>
<li><a href="http://williamdurand.fr/2013/06/03/object-calisthenics/">Object Calisthenics</a> by William Durand</li>
</ul>
</section>
<section>
<h2>Thank you</h2>
<h3>Questions?</h3>
<img data-src="img/question.gif" style="border:none;width: 40%;">
<br>
<a href="https://twitter.com/_maxpou"><i class="fa fa-twitter"></i> @_maxpou</a>
</section>
</section>
<section>
<section>
<h2 class="top-3">Backup slides</h2>
</section>
<section>
<h2 class="top-1">No classes with more than two instance variable</h2>
<img src="img/instance-variables.png" alt="">
</section>
<section>
<h2>Using / Personal POV</h2>
<table>
<tr>
<td>#1: One level of indentation</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#2: Don’t use the ELSE keyword</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#3: Wrap all primitives and Strings</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#4: First class collections</td><td><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#5: One dot per line</td><td><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#6: Don’t abbreviate</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#7: Keep all entities small</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#8: No classes with several instance variables</td><td><i class="fa fa-thumbs-o-up"></i><i class="fa fa-thumbs-o-up"></i></td>
</tr>
<tr>
<td>#9: No getters/setters/properties</td><td><i class="fa fa-thumbs-o-up"></i></td>
</tr>
</table>
</section>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
slideNumber: true,
history: true,
center: false,
width: 1000,
height: 700,
transition: 'convex', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
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, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: 'reveal.js/plugin/notes/notes.js', async: true }
]
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-67868977-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>