AWS Community Day CPH - Three problems of Terraform
Clean Code
1. Clean Code
Presentation of the Uncle Bob's Book.
Thursday, December 1, 11 1
2. The Speaker
• Brice Argenson
– Full Professor in Java and Web Technologies
• Professional Experiences :
– Bug Out PC – Audaxis
– ADF – Groupe Open
– Adullact – Atos Worldline
– Webpulser – Xebia IT
• Certifications :
– SCJP 6 – MCP – Exam 70-316
– SCWCD 5
brice.argenson@supinfo.com @bargenson
Thursday, December 1, 11 2
9. 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…
Thursday, December 1, 11 9
11. 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…
Thursday, December 1, 11 11
12. 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 !
Thursday, December 1, 11 12
13. 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 !
Thursday, December 1, 11 13
14. What is Clean Code ?
• The Boy Scout Rule :
– Leave the campground cleaner than you found it.
• Refactor your code !
• Write tests !
– Test Driven Development
Thursday, December 1, 11 14
15. How to write Clean Code ?
GET YOUR HANDS DIRTY !
Thursday, December 1, 11 15
16. Meaningful Names
• What is the purpose of this code ?
Thursday, December 1, 11 16
17. Meaningful Names
• And what do you think of this code ?
Thursday, December 1, 11 17
18. Meaningful Names
• Use Intention-Revealing Names
Thursday, December 1, 11 18
19. Meaningful Names
• What about Hungarian Notation ?
Thursday, December 1, 11 19
20. 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
Thursday, December 1, 11 20
21. 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
Thursday, December 1, 11 21
22. Functions
• What do you think about this method ?
– FitNesse - commit 3bec390e6f
• And now ?
– FitNesse - commit 7491001db7
• What has changed ?
Thursday, December 1, 11 22
23. 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
Thursday, December 1, 11 23
24. 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 ?
Thursday, December 1, 11 24
25. Error Handling
• What do you think about this code ?
Thursday, December 1, 11 25
26. Error Handling
• Prefer exceptions to returning error codes
Thursday, December 1, 11 26
27. Error Handling
• And what do you think about this code ?
Thursday, December 1, 11 27
28. Error Handling
• For you what is the most common exception ?
– NPE !
• How to avoid it ?
28
Thursday, December 1, 11 28
29. Error Handling
• What do you think about this code ?
29
Thursday, December 1, 11 29
31. Error Handling
• What do you think about this code ?
31
Thursday, December 1, 11 31
32. Error Handling
• How to make code look like that ?
• Special Case object !
32
Thursday, December 1, 11 32
33. Side Effects
• Consider the following class :
33
Thursday, December 1, 11 33
34. Side Effects
• Consider the following code :
• Do you see the problem ?
34
Thursday, December 1, 11 34
35. 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
Thursday, December 1, 11 35
36. 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
Thursday, December 1, 11 36
41. 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
Thursday, December 1, 11 41
42. Side Effects
• Favor composition over inheritance !
42
Thursday, December 1, 11 42
43. Do you want to read more about the subject ?
REFERENCES
43
Thursday, December 1, 11 43
44. Books
Clean Code
A Handbook of Agile Software Craftsmanship
Robert C. Martin (aka. Uncle Bob)
Prentice Hall editions
44
Thursday, December 1, 11 44
45. Books
Effective Java
Second Edition
Joshua Bloch
Addison Wesley editions
45
Thursday, December 1, 11 45
46. 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
Thursday, December 1, 11 46