Download - Clean Code
![Page 1: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/1.jpg)
Clean Code
Presentation of the Uncle Bob's Book.
1Thursday, December 1, 11
![Page 2: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/2.jpg)
The Speaker
• Brice Argenson– Full Professor in Java and Web Technologies
• Professional Experiences :
• Certifications :
– Bug Out PC– ADF– Adullact– Webpulser
– Audaxis– Groupe Open– Atos Worldline– Xebia IT
– SCJP 6– SCWCD 5
– MCP – Exam 70-316
[email protected] @bargenson
2Thursday, December 1, 11
![Page 3: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/3.jpg)
Agenda
• Software Craftsmanship
• Why Clean Code ?
• Tips & Tricks to write Clean Code
• References
3Thursday, December 1, 11
![Page 4: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/4.jpg)
SOFTWARE CRAFTSMANSHIPWhat is that ?
4Thursday, December 1, 11
![Page 5: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/5.jpg)
5
5Thursday, December 1, 11
![Page 6: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/6.jpg)
6
6Thursday, December 1, 11
![Page 7: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/7.jpg)
WHY CLEAN CODE ?Bad Code VS. Clean Code
7Thursday, December 1, 11
![Page 8: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/8.jpg)
8Thursday, December 1, 11
![Page 9: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/9.jpg)
Bad Code
• Bad Code killed some companies– Rushed the product to market– More and more features– Worse and worse code– Code unmanageable
• We all wrote bad code– Trying to go fast ?– We were in a rush ?– We’ll clean it up later…
9Thursday, December 1, 11
![Page 10: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/10.jpg)
10Thursday, December 1, 11
![Page 11: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/11.jpg)
The Cost of Bad Code
• Do you have been slowed down by messy code ?
• More & more mess è Less & less productivity
• When productivity became terrible è Redesign
• And again…• And again…
11Thursday, December 1, 11
![Page 12: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/12.jpg)
Whose Fault ?
• The customer ?– The requirements change too much ?
• The managers ?– The deadlines are too short ?
• It is mainly the developer fault !– The others look to us for the information they need– The doctor knows more than the patient about the risks
[…]
• Be professionals !
12Thursday, December 1, 11
![Page 13: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/13.jpg)
What is Clean Code ?
• What is Clean Code for you ?– It is working ?– It is efficient ?– It is short ?
• Clean Code is easy to read !
• We are authors !
13Thursday, December 1, 11
![Page 14: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/14.jpg)
What is Clean Code ?
• The Boy Scout Rule :– Leave the campground cleaner than you found it.
• Refactor your code !
• Write tests !– Test Driven Development
14Thursday, December 1, 11
![Page 15: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/15.jpg)
GET YOUR HANDS DIRTY !How to write Clean Code ?
15Thursday, December 1, 11
![Page 16: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/16.jpg)
Meaningful Names
• What is the purpose of this code ?
16Thursday, December 1, 11
![Page 17: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/17.jpg)
Meaningful Names
• And what do you think of this code ?
17Thursday, December 1, 11
![Page 18: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/18.jpg)
Meaningful Names
• Use Intention-Revealing Names
18Thursday, December 1, 11
![Page 19: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/19.jpg)
Meaningful Names
• What about Hungarian Notation ?
19Thursday, December 1, 11
![Page 20: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/20.jpg)
Meaningful Names
• What if one day we change the type ?
• Hungarian Notation make it harder to change the name or type of a variable, function or class
20Thursday, December 1, 11
![Page 21: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/21.jpg)
Meaningful Names
• What about Member Prefixes ?– Example: m_firstName
• Classes and functions should be small enough that you don’t need them
• IDE highlights or colorizes members !
• Prefixes become unseen clutter
21Thursday, December 1, 11
![Page 22: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/22.jpg)
Functions
• What do you think about this method ?– FitNesse - commit 3bec390e6f
• And now ?– FitNesse - commit 7491001db7
• What has changed ?
22Thursday, December 1, 11
![Page 23: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/23.jpg)
Functions
• Small !– Not bigger than a screen full
• Correct in the eighties (24 x 80)
– Should be transparently obvious– Indent level should not be greater than one or two
• Do one thing !– Your function is divided into section ?
• You can divide it !
• The Stepdown Rule !– We want the code to read like a top-down narrative
23Thursday, December 1, 11
![Page 24: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/24.jpg)
Functions
• Function arguments ?– Niladic
• The ideal number
– Monadic• A good number
– Dyadic• A good number
– Triadic• Should be avoided
– Polyadic• Do you need to wrap the arguments into a new type ?
24Thursday, December 1, 11
![Page 25: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/25.jpg)
Error Handling
• What do you think about this code ?
25Thursday, December 1, 11
![Page 26: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/26.jpg)
Error Handling
• Prefer exceptions to returning error codes
26Thursday, December 1, 11
![Page 27: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/27.jpg)
Error Handling
• And what do you think about this code ?
27Thursday, December 1, 11
![Page 28: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/28.jpg)
Error Handling
• For you what is the most common exception ?– NPE !
• How to avoid it ?
28
28Thursday, December 1, 11
![Page 29: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/29.jpg)
Error Handling
• What do you think about this code ?
29
29Thursday, December 1, 11
![Page 30: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/30.jpg)
Error Handling
• Don’t return Null !
30
30Thursday, December 1, 11
![Page 31: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/31.jpg)
Error Handling
• What do you think about this code ?
31
31Thursday, December 1, 11
![Page 32: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/32.jpg)
Error Handling
• How to make code look like that ?
• Special Case object !
32
32Thursday, December 1, 11
![Page 33: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/33.jpg)
Side Effects
• Consider the following class :
33
33Thursday, December 1, 11
![Page 34: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/34.jpg)
Side Effects
• Consider the following code :
• Do you see the problem ?
34
34Thursday, December 1, 11
![Page 35: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/35.jpg)
Side Effects
• To protect the internals of a Period instance from this sort of attack:
– You must make defensive copy of each mutable parameter to the constructor !
35
35Thursday, December 1, 11
![Page 36: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/36.jpg)
Side Effects
• Inheritance is a powerful way to achieve code reuse– But not always the best !
• Inheritance from ordinary concrete classes across package boundaries is dangerous !
• Unlike method invocation, inheritance violates encapsulation
36
36Thursday, December 1, 11
![Page 37: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/37.jpg)
Side Effects
37
37Thursday, December 1, 11
![Page 38: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/38.jpg)
Side Effects
• What this code display ?
38
38Thursday, December 1, 11
![Page 39: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/39.jpg)
Side Effects
39
39Thursday, December 1, 11
![Page 40: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/40.jpg)
Side Effects
40
40Thursday, December 1, 11
![Page 41: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/41.jpg)
Side Effects
• Design of the InstrumentedSet is extremely flexible :– Implement the Set interface– Receive an argument also of type Set
• With inheritance, we could work only with HashSet
• With composition, we can work with any Set implementation !
41
41Thursday, December 1, 11
![Page 42: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/42.jpg)
Side Effects
• Favor composition over inheritance !
42
42Thursday, December 1, 11
![Page 43: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/43.jpg)
REFERENCESDo you want to read more about the subject ?
43
43Thursday, December 1, 11
![Page 44: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/44.jpg)
Books
44
Clean CodeA Handbook of Agile Software Craftsmanship
Robert C. Martin (aka. Uncle Bob)
Prentice Hall editions
44Thursday, December 1, 11
![Page 45: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/45.jpg)
Books
45
Effective JavaSecond Edition
Joshua Bloch
Addison Wesley editions
45Thursday, December 1, 11
![Page 46: Clean Code](https://reader031.vdocument.in/reader031/viewer/2022020115/544fe9e3af7959cb398baa41/html5/thumbnails/46.jpg)
Links
• Sign the Manifesto – http://manifesto.softwarecraftsmanship.org
• Software Craftsmanship en pratique - Xebia– http://blog.xebia.fr/2011/01/31/software-craftsmanship-
en-pratique/
• The Clean Coder - Uncle Bob blog– http://thecleancoder.blogspot.com/
46
46Thursday, December 1, 11