/
index.en.html
552 lines (475 loc) · 23.4 KB
/
index.en.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
<title>From STUPID to SOLID code!</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 class="title">Be <strong>pragmatic</strong> while <strong>coding</strong></h1>
<em>Ahmed SIOUANI</em>
</section>
<!-- Orthogonality -->
<section>
<section data-markdown>
<script type="text/template">
# Orthogonality
</script>
</section>
<section data-markdown>
<script type="text/template">
# In mathematics
* It is a relation of two lines at right angles to one another (perpendicularity) - Wikipedia
![](images/axexy.png)
* An unchanged projection of any motion through one axe(X) to the other axe(Y).
</script>
</section>
<section data-markdown>
<script type="text/template">
<h1>Notion of <strong>independence</strong></h1>
</script>
</section>
<section data-markdown>
<script type="text/template">
# In programming
* Orthogonality = Independence, decoupling
<br><br>
* Things that are not related conceptually should not be related in the system.
<br><br>
<strong>Exemple</strong>:<br>
Database / User Interface
</script>
</section>
<section data-markdown>
<script type="text/template">
### Which class belongs to a perfectly orthogonal system?
<pre>
class Explode1 {
public explode1(InputStreamReader input)
public void readNextLine() throws IOException
public int numFields()
public String getField(int fieldNo)
}
</pre>
<br>
<pre>
class Explode2 {
public explode2(String line)
public int numFields()
public String getField(int fieldNo)
}
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Advantages
* Local fixes.
* Testability.
* Reusability.
</script>
</section>
</section>
<!-- Law of Demeter -->
<section>
<section data-markdown>
<script type="text/template">
# The Law of Demeter, LoD - 1987
### Principle of least knowledge
</script>
</section>
<section data-markdown>
<script type="text/template">
###The Law of Demeter
* A design guideline.
<br><br>
### Principles
* Each element should only talk to its friends, don't talk to strangers.
* Each element should have only limited knowledge about other elements: only elements "closely" related to the current element.
</script>
</section>
<section data-markdown>
<script type="text/template">
###Law of Demeter for Functions/Methods - (LoD-F)
Any method of a given object may only invokes the:
* Object itself
* Method's parameters
* Any objects created/instantiated within the method
* The object's direct component objects
* A global variable, accessible by the object, in the scope of the method.
</script>
</section>
<section data-markdown>
<script type="text/template">
###The Law of Demeter for Functions/Methods - (LoD-F)
![](images/demeter.jpeg)
<pre>
$this
->getFirstObject()
->doSomething(...);
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
###The Law of Demeter for Functions/Methods - (LoD-F)
<br>
Violation of the Demeter Law,
<pre>
$this
->getFirstObject()
->getSecondObject()
->doSomething();
</pre>
<br>
Encapsulation
<pre>
$this
->getFirstObject()
->doSomethingWithSecondObject()
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
###Advantages
* Adaptability and maintenability of your code.
<br><br>
###Drawbacks
* A systematic need of wrapping your calls.
</script>
</section>
</section>
<!-- Dependency Injection -->
<section>
<section data-markdown>
<script type="text/template">
# Dependency Injection
</script>
</section>
<section data-markdown class="highlighted-list">
<script type="text/template">
## Good to know!
* Do not confuse **"Dependecy Injection"**, **Inversion of Control** (IoC) and **Service Locator** (SL)
</script>
</section>
<section data-markdown>
<script type="text/template">
## Some definitions,
* <strong>Inversion of control, </strong>
- The relationship between higher lever class and detail classes
- Higher level classes shouldn't depend on detail classes, but rather the contrary
- Higher level classes should depend on abstraction (like interfaces)
* <Strong>Dependency Injection, </Strong>
- A dependency is an object used by your class
- An injection is the fact of passing an argyment to a function
* <Strong>Service Locator, </Strong>
- A container of service injected as a dependency.
- <strong>In Symfony, </strong>injection of the DIC.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Dependency Injection
<strong>Injection via the setter, </strong>
<pre>
public function setMailer(Mailer $mailer)
{
$this->mailer = $mailer;
}
</pre>
<strong>Access via the getter,,</strong>
<pre>
public function getMailer()
{
return $this->mailer;
}
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Wrong !
* <strong>Runtime Error,</strong> in case your setter isn't called before.
* The appropriate getter,
<pre>
private function getMailer()
{
if (!($this->mailer instanceof Mailer)) {
throw new \LogicException('The Mailer dependency is missing.');
}
return $this->mailer;
}
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
* Setter Injection</strong>
- Should be used only when you're not allowed to access the object construction.
- For classes that extends third-party libraries.
* <strong>Example:</strong>
- A doctrine repository is accessible only via the <strong>EntityManager</strong>. You don't have control over its construction. For this specific case, injection is performed via setters.
</script>
</section>
<section data-markdown>
<script type="text/template">
#Example
<div style="height:20px;width:20px;background-color:red;float:right;"></div>
<pre>
first_service_id:
class: %first_service.class%
calls:
- [ setAnyOtherService, [@any_other_service_id]]
</pre>
<div style="height:20px;width:20px;background-color:green;float:right;"></div>
<pre>
second_service_id:
class: %second_service.class%
arguments: [@any_other_service_id]
</pre>
</script>
</section>
</section>
<!-- Exceptions -->
<section>
<section data-markdown>
<script type="text/template">
# Exceptions
</script>
</section>
<section data-markdown>
<script type="text/template">
![](images/exception.jpg)
</script>
</section>
<section data-markdown>
<script type="text/template">
Only use exceptions for <strong>Exceptional</strong> problems
</script>
</section>
<section data-markdown>
<script type="text/template">
* File opening,
<pre>
public void open_file() throws FileNotFoundException {
$stream = new FileInputStream("/etc/passwd");
}
</pre>
<pre>
public void open_file() throws FileNotFoundException {
$stream = new FileInputStream("/home/userX/test_results.xml");
}
</pre>
</script>
</section>
<section data-markdown>
<script type="text/template">
* Error handlers
<pre>
public void open_file() {
File f = new File("/home/userX/test_results.xml");
if (!f.exists()) {
return false;
}
$stream = new FileInputStream(f);
return true;
}
</pre>
* open an inexistant file => conventional error
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
![](images/stupid.png)
## **STUPID** Code
</script>
</section>
<section data-markdown class="highlighted-list">
<script type="text/template">
## **STUPID** Code?
* **S**ingleton
* **T**ight Coupling
* **U**ntestability
* **P**remature Optimization
* **I**ndescriptive Naming
* **D**uplication
</script>
</section>
<section data-markdown>
<script type="text/template">
## **S**ingleton
* Not extensible (configuration limits)
* Introduce strong couplings
* Untestability
</script>
</section>
<section data-markdown>
<script type="text/template">
## **T**ight Coupling
Generalisation of the limits introduced by the "Signleton" Anti-pattern.
</script>
</section>
<section data-markdown>
<script type="text/template">
## **U**ntestability
</script>
</section>
<section data-markdown>
<script type="text/template">
## **P**remature Optimization
_Premature optimization is the root of all evil._ — Donald Knuth.
</script>
</section>
<section data-markdown>
<script type="text/template">
## **I**ndescriptive Naming
* Meaningless naming.
* Do not use abbreviations.
</script>
</section>
<section data-markdown>
<script type="text/template">
## **D**uplication
**D**on't **R**epeat **Y**ourself!
**K**eep **I**t **S**imple, **S**tupid!
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
![](images/solid.jpg)
## **SOLID** to the rescue!
</script>
</section>
<section data-markdown class="highlighted-list">
<script type="text/template">
## **S**OLID
* **S**ingle Responsability Principle
* **O**pen Close Principle
* **L**iskov Substitution Principle
* **I**nterface Segregation Principle
* **D**ependency Injection Principle
</script>
</section>
<section data-markdown>
<script type="text/template">
## **S**ingle Responsibility Principle (SRP)
There should **never** be **more than one reason**
<br>for a class to change.
</script>
</section>
<section>
<img src="images/srp.jpg" />
<p>Just because you can, doesn't mean you should!</p>
</section>
<section data-markdown>
<script type="text/template">
## **O**pen/Closed Principle (OCP)
* you should be able to extend a classes brehavior, without modifying it - Robert C.Martin
</script>
</section>
<section data-markdown>
<script type="text/template">
## **L**iskov Substitution Principle (LSP)
Derived classes must be substituable for their base classes - Robert C.Martin
<br><br>
* <strong>Be careful</strong>, when setting returned values in weakly-typed languages.
</script>
</section>
<section data-markdown>
<script type="text/template">
## **I**nterface Segregation Principle (ISP)
Make fine grained interfaces that are client specific - Robert C.Martin
</script>
</section>
<section data-markdown>
<script type="text/template">
## **D**ependency Injection Principle (DIP)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
# Unit tests
</script>
</section>
<section data-markdown>
<script type="text/template">
##Test the states of your logic
Exemple :
<pre>
int function_1(int a, int b) {
return a / (a + b)
}
</pre>
<br>
<pre>
testFunction_1() {
assertEqual(0.5, function_1(1,1));
}
</pre>
Code coverage 100% / State coverage: 50%
</script>
</section>
</section>
<section data-markdown>
<script type="text/template">
## Thank you, Questions?
<br>
<ul class="me">
<li><a href="http://ahsio.com" title="website">ahsio.com</a></li>
</ul>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Credits
* <a href="http://en.wikipedia.org/wiki/Andy_Hunt_(author)">Andrew Hunt</a>
* <a href="http://en.wikipedia.org/wiki/Dave_Thomas_(programmer)">David Thomas</a>
* <a href="http://lostechies.com/derickbailey/">Derick Bailey</a>
* <a href="http://williamdurand.fr/">William Durand</a>
* <a href="https://twitter.com/hhamon">Hugo Hamon</a>
* <a href="http://gnugat.github.io">Loïc Chardonnet</a>
</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(); } },
]
});
</script>
</body>
</html>