-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
753 lines (657 loc) · 34.5 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Be SOLID | Antoine Froger</title>
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/white.css">
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="css/custom.css">
<link rel="icon" type="image/png" href="images/favicon.png">
<!-- 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">
<a href="https://newcraftgroup.com" class="logo" target="_blank">NEWCRAFT</a>
<section class="main">
<h1>BE <strong>SOLID</strong></h1>
<h5>Five basic programming principles</h5>
<small>A <a href="https://www.newcraftgroup.com/" target="_blank">Newcraft</a> tech meetup</small>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow"><strong>S.O.L.I.D.</strong></h2>
<p>
SOLID is an acronym defining<br />
<strong>five basic programming principles</strong>
</p>
<p>
SOLID principles were defined by<br />
</strong>Robert <em>"Uncle Bob"</em> Martin</strong><br />
in the early 2000s
</p>
</section>
<section>
<h3>What does <strong>SOLID</strong> means?</h3>
<ul class="no-bullet">
<li><strong>S</strong>ingle Responsability Principle</li>
<li><strong>O</strong>pen/Closed Principle</li>
<li><strong>L</strong>iskov Substitution Principle</li>
<li><strong>I</strong>nterface Segregation Principle</li>
<li><strong>D</strong>ependency Inversion Principle</li>
</ul>
</section>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow"><strong>S</strong>OLID</h2>
</section>
<section>
<h3><strong>S</strong>ingle Responsability Principle*</h3>
<p>There should <strong>never</strong> be more than <strong>one reason</strong><br />
for a class to change.</p>
<small>
* also known as <a href="docs/srp.pdf" target="_blank">SRP</a>
</small>
</section>
<section data-markdown data-transition="slide-in fade-out"><script type="text/template">
### **Don't** do <a href="http://wiki.c2.com/?GodClass" target="_blank">GOD classes</a>
```php
class OrderEmailHandler
{
public function __construct(array $data) { /* ... */ }
public function createMessage() { /* ... */ }
public function send($email) { /* ... */ }
public function displayWebVersion() { /* ... */ }
private function validateEmail() { /* ... */ }
private function checkUserPermission() { /* ... */ }
}
```
<img src="images/man-orchestra.jpg" alt="Man orchestra" class="orchestra">
</script></section>
<section data-markdown data-transition="fade-in fade-out"><script type="text/template">
### but rather **small** classes <small>(which work together)</small>
![](images/orchestra.jpg)
</script></section>
<section data-markdown data-transition="fade-in slide-out"><script type="text/template">
```php
// namespace MyApp\Controller\Order;
class OrderMessageController {}
// namespace MyApp\Email\Order;
class OrderMessageCreator {}
class OrderMessageSender {}
// namespace MyApp\Validator
class EmailValidator {}
```
</script></section>
<section>
<h3>One responsability per class</h3>
<ul>
<li>One reason to change</li>
<li>Stay focused on a single concern</li>
<li>Simplify maintenance</li>
<li>Simplify testing</li>
<li>Can be reused (DRY <i class="fa fa-heart"></i>)</li>
</ul>
</section>
<section>
<h3>One class per responsability</h3>
<ul>
<li>Always try to give every class its <strong>own</strong> responsibility</li>
<li>Define for every responsibility a class</li>
<li>When the responsability changes, <strong>only one class</strong> has to be modified</li>
</ul>
</section>
<section>
<h3><strong>Demo</strong></h3>
<h4>EmailAndPdfHtmlProcessor</h4>
<p class="fragment">Responsible for:</p>
<ul>
<li class="fragment">creating the HTML used to render the PDF</li>
<li class="fragment">creating the HTML used for the email body</li>
<li class="fragment">creating the Swift_Message object</li>
</ul>
<span class="fragment message type-alert">Too many responsabilities</span>
</section>
<section data-markdown><script type="text/template">
<h3>Tips</h3>
<p class="fragment">
If your class name contains a separator like <strong>And</strong> or <strong>Or</strong><br />
<em>The class has probably more than one goal</em>
</p>
<p class="fragment">
Write straightforward code and comments<br />
Conditions like `if / elseif / else` and `switch` are an indicator.<br />
<em>If you start using many conditions you are going on the wrong direction</em>
</p>
</script></section>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow">S<strong>O</strong>LID</h2>
</section>
<section data-markdown><script type="text/template">
### **O**pen/Closed Principle*
Software entities should be **open for extension**,
but **closed for modification**.
<small>
\* also known as <a href="docs/ocp.pdf" target="_blank">OCP</a>
</small>
</script></section>
<section data-markdown><script type="text/template">
### In other words
When a new functionality is needed,
we should **not modify our existing code**
but rather **write new code** that will be **used by existing code**.
</script></section>
<section data-markdown><script type="text/template">
### Example
Calculating price
</script></section>
<section data-markdown><script type="text/template">
### Context
We sell subscriptions for a big phone operator
```php
public class Subscription
{
private $price;
private $discount;
private $optionPrice;
// getters...
}
```
```php
class PriceCalculator
{
public function calculate(array $subscriptions)
{
$price = 0;
foreach ($subscriptions as $subscription) {
$price += $subscription->getPrice() -
$subscription->getDiscount() +
$subscription->optionPrice;
}
return $price;
}
}
```
</script></section>
<section data-markdown><script type="text/template">
### New feature
A new product `Phone` comes into play
... and its price is calculated differently
```php
public class Phone
{
private $quantity;
private $price;
private $discount;
// getters...
}
```
</script></section>
<section data-markdown><script type="text/template">
### How to calculate the new price?
```php
class PriceCalculator
{
public function calculate(array $products)
{
$price = 0;
foreach ($products as $product) {
if ($product instanceof Subscription) {
$price += $product->getPrice() -
$product->getDiscount() +
$product->getOptionPrice();
} else {
$price += $quantity *
($product->getPrice() - $product->getDiscount());
}
}
return $price;
}
}
```
</script></section>
<section data-markdown><script type="text/template">
<p>It works <strong>but</strong>...</p>
<ul>
<li class="fragment">`PriceCalculator` is <strong>not closed for modification</strong><br />
as we need to change it in order to extend it.</li>
<li class="fragment">We'll quickly have an <strong>ugly</strong> and <strong>unmaintenable</strong> peace of code</li>
</ul>
<span class="fragment message type-alert">Not a good solution</span>
</script></section>
<section data-markdown data-transition="slide-in fade-out"><script type="text/template">
### How to be **OCP** compliant?
One solution among others
<pre class="fragment"><code>
interface Orderable
{
public function calculatePrice();
}
</code></pre>
</script></section>
<section data-markdown data-transition="fade-in fade-out"><script type="text/template">
```php
public class Subscription implements Orderable
{
public function calculatePrice()
{
return $this->price - $this->discount + $product->optionPrice;
}
}
```
```php
public class Phone implements Orderable
{
public function calculatePrice()
{
return $quantity * ($this->getPrice() - $this->getDiscount());
}
}
```
</script></section>
<section data-markdown data-transition="fade-in slide-out"><script type="text/template">
```php
class PriceCalculator
{
/**
* @param Orderable[] $products
*/
public function calculate(array $products)
{
$price = 0;
foreach ($products as $product) {
$price += $product->calculatePrice();
}
return $price;
}
}
```
<p class="fragment">
We'll never modify `PriceCalculator` again,<br />
<strong>it's closed for modification.</strong>
</p>
<p class="fragment">
When we need a new class, it has to implement `Orderable`<br />
and calculate its price alone.<br />
</p>
<p class="fragment">
We reduce the risk of introducing bugs in an old functionality, as the `PriceCalculator::calculate()` method is already unit tested
</p>
</script></section>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow">SO<strong>L</strong>ID</h2>
</section>
<section data-markdown><script type="text/template">
### **L**iskov Substitution Principle*
Functions that use pointers or references to base classes
must be able **to use objects of derivated classes without knowing it**
<small>
\* also known as <a href="docs/lsp.pdf" target="_blank">LSP</a>
</small>
</script></section>
<section data-markdown><script type="text/template">
### In other words
Objects in a program **should be replaceable with instances of their subtypes** without altering the correctness of the program.
</script></section>
<section data-markdown><script type="text/template">
### Example
That is **NOT** related to rectangles and squares
<small>
*But you can find the famous rectangle example <a href="docs/lsp.pdf" target="_blank">here</a>*
</small>
</script></section>
<section data-markdown><script type="text/template">
```php
class DeviceCollection
{
protected $collection;
public function add(Device $device)
{
$this->collection[$device->getId()] = $device;
}
public function getAll()
{
return $this->collection;
}
}
```
</script></section>
<section data-markdown><script type="text/template">
```php
class OrderedDeviceCollection extends DeviceCollection
{
public function add(Device $device)
{
parent::add($device);
usort($this->collection, function($a, $b) {
if ($a->getPrice() === $b->getPrice()) {
return 0;
}
return ($a->getPrice() < $b->getPrice()) ? -1 : 1;
});
}
}
```
</script></section>
<section data-markdown><script type="text/template">
### Why is the LSP violated?
<pre class="fragment"><code>
public function testAddToCollection()
{
$collection = new DeviceCollection();
$collection->add(new Device(1, 9.99));
$collection->add(new Device(2, 199));
$collection->add(new Device(3, 59));
$this->assertEqual(
[new Device(1, 9.99), new Device(2, 199), new Device(3, 59)],
$collection->getAll()
);
// true
}
</code></pre>
<p class="fragment">
<span>**Works!**</span>
<span class="fragment">*But wait a minute...*</span>
</p>
<p class="fragment">
What happens if we use<br />
`$collection = new OrderedDeviceCollection();`?
</p>
<span class="fragment message type-alert">The test will fail!</span>
<p class="fragment">
We can't replace `DeviceCollection` by a class of its subtypes<br />
<strong>LSP is violated</strong>
</p>
</script></section>
<section data-markdown><script type="text/template">
### A LSP violation in our code
```php
class CellSubscription extends Product
{
public function setMinutes($minutes)
{
$this->minutes = $minutes;
}
}
```
```php
class ConfiguredCellSubscription extends CellSubscription
{
public function setMinutes($minutes)
{
throw new \BadMethodCallException('Cannot set minutes for configured cell subscription');
}
}
```
<p>In that case, using <a href="https://en.wikipedia.org/wiki/Object_composition" target="_blank">composition</a>
would have been<br />a better solution than <a href="https://en.wikipedia.org/wiki/Composition_over_inheritance" target="_blank">inheritance</a></p>
</script></section>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow">SOL<strong>I</strong>D</h2>
</section>
<section data-markdown><script type="text/template">
### **I**nterface Segregation Principle*
Clients **should not** be forced to depend upon interfaces
that they do not use
<small>
\* also known as <a href="docs/isp.pdf" target="_blank">ISP</a>
</small>
</script></section>
<section data-markdown><script type="text/template">
### In other words
**Many client specific interfaces** are better than
one general-purpose interface
<p class="fragment">The bigger the interface is, the more likely it is that all its features
won't be used by the classes that implement it</p>
</script></section>
<section data-markdown><script type="text/template">
### Example: Product
```php
interface ProductInterface {
public function getTitle();
public function getDescription();
public function getPrice();
public function getDeliveryMethods();
public function getEstimatedDeliveryDate();
}
```
```php
class Product implements ProductInterface {
public function getTitle() {
//...
}
public function getDescription {
//...
}
public function getPrice() {
//...
}
public function getDeliveryMethods() {
//...
}
public function getEstimatedDeliveryDate() {
//...
}
}
```
</script></section>
<section data-markdown><script type="text/template">
### Example: Digital products are introduced
We don't need to deliver digital products
but the `ProductInterface` forces us to
implement delivery methods
```php
class DigitalProduct implements ProductInterface {
// ...
public function getDeliveryMethods() {
throw new NotImplementedException();
}
public function getEstimatedDeliveryDate() {
throw new NotImplementedException();
}
}
```
<span class="fragment message type-alert">Not a good solution</span>
</script></section>
<section data-markdown><script type="text/template">
### How to improve that?
<p class="fragment">Split the interface</p>
<pre class="fragment"><code>
interface ProductInterface {
public function getTitle();
public function getDescription();
public function getPrice();
}
interface DeliverableInterface {
public function getDeliveryMethods();
public function getEstimatedDeliveryDate();
}
</code></pre>
</script></section>
<section data-markdown><script type="text/template">
`Product` implements
`ProductInterface` **AND** `DeliverableInterface`
```php
class Product implements ProductInterface, DeliverableInterface {
public function getTitle() {
//...
}
public function getDescription {
//...
}
public function getPrice() {
//...
}
public function getDeliveryMethods() {
//...
}
public function getEstimatedDeliveryDate() {
//...
}
}
```
</script></section>
<section data-markdown><script type="text/template">
While `DigitalProduct`
**ONLY** implements `ProductInterface`
Unneeded methods are not implemented
```php
class DigitalProduct implements ProductInterface {
public function getTitle() {
//...
}
public function getDescription {
//...
}
public function getPrice() {
//...
}
}
```
</script></section>
</section>
<section>
<section>
<h2 class="has-shape type-rectangle color-yellow">SOLI<strong>D</strong></h2>
</section>
<section data-markdown><script type="text/template">
### **D**ependency Inversion Principle*
1. High level modules **should not** depend upon low level modules.
Both should depend upon abstractions.
2. Abstractions **should not** depend upon details.
Details should depend upon abstractions.
<p class="fragment">
So,
**Details** should depend upon **Abstration**<br />
**Abstractions** should depend upon **Abstration**
</p>
<small>
\* also known as <a href="docs/dip.pdf" target="_blank">DIP</a>
</small>
</script></section>
<section>
<h3>
<a href="https://lostechies.com/derickbailey/2011/09/22/dependency-injection-is-not-the-same-as-the-dependency-inversion-principle/" target="_blank">
Dependency Injection is NOT the same as the<br />
Dependency Inversion Principle
</a>
</h3>
</section>
<section data-markdown><script type="text/template">
## What is the benefit?
<ul class="no-bullet">
<li>**Remove tight** <a href="https://en.wikipedia.org/wiki/Coupling_(computer_programming)" target="_blank">coupling</a></li>
</ul>
So, we are able to replace a piece of the system without having to change more than that individual piece
<p class="fragment"><em>When the wheel of your car is bursted,<br />you don't want to change the axletree, do you?</em></p>
</script></section>
<section data-markdown><script type="text/template">
### Example <small>(adapted from <a href="https://lostechies.com/gabrielschenker/2009/01/30/the-dependency-inversion-principle/" target="_blank">LosTechies</a>)</small>
Let's imagine a service containing a encryption algorythm
```php
class EncryptionService
{
public function encrypt($sourceFileName, $targetFileName)
{
// Read content
$content = file_get_contents($sourceFileName);
// encrypt
$encryptedContent = $this->doEncryption($content);
// write encrypted content
file_put_contents($targetFileName, $encryptedContent);
}
private function doEncryption($content)
{
// put here your encryption algorithm...
return $encryptedContent;
}
}
```
<ul class="fragment no-bullet">
<li>Highly coupled to files as input and output</li>
<li>Can't be used in another context</li>
</ul>
</script></section>
<section data-markdown><script type="text/template">
### New try following DIP
```php
class EncryptionService
{
public function encrypt(ReaderInterface $reader, WriterInterface $writer)
{
// Read content
$content = $reader->getContent();
// encrypt
$encryptedContent = $this->doEncryption($content);
// write encrypted content
$writer->write($encryptedContent);
}
// doEncryption method...
}
```
```php
interface ReaderInterface
{
public function getContent();
}
```
```php
interface WriterInterface
{
public function write($content);
}
```
</script></section>
<section data-markdown><script type="text/template">
<h3>What is better?</h3>
<ul>
<li>`EncryptionService` is not dependent of<br />hardcoded reader and writer</li>
<li>New readers and writers can be created<br />without modifying `EncryptionService` (OCP <i class="fa fa-heart"></i>)</li>
<li>`EncryptionService` can be easily tested</li>
</ul>
</script></section>
</section>
<section>
<h1>Thanks</h1>
<h2>Questions?</h2>
<ul class="no-bullet">
<li><i class="fa fa-github"></i> <a href="https://github.com/antfroger" class="text-no-transform">github.com/antfroger</a></li>
<li><i class="fa fa-twitter"></i> <a href="https://twitter.com/__tooni__" class="text-no-transform">@__tooni__</a></li>
</ul>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script src="https://use.fontawesome.com/58f1022f98.js"></script>
<script>
Reveal.initialize({
history: true,
transition: 'slide',
slideNumber: !window.location.search.match(/print-pdf/gi),
dependencies: [
{ src: 'reveal.js/plugin/markdown/marked.js' },
{ src: 'reveal.js/plugin/markdown/markdown.js' },
{ src: 'reveal.js/plugin/notes/notes.js', async: true },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>