Your SlideShare is downloading. ×
Clean Code
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Clean Code

2,403
views

Published on

Slides des conférences publiques hébergées par SUPINFO (http://www.supinfo.com) données en Octobre 2011 à Nantes et Novembre 2011 à Montréal.

Slides des conférences publiques hébergées par SUPINFO (http://www.supinfo.com) données en Octobre 2011 à Nantes et Novembre 2011 à Montréal.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,403
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Clean Code Presentation of the Uncle Bobs 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 @bargensonThursday, December 1, 11 2
  • 3. Agenda • Software Craftsmanship • Why Clean Code ? • Tips & Tricks to write Clean Code • ReferencesThursday, December 1, 11 3
  • 4. What is that ? SOFTWARE CRAFTSMANSHIPThursday, December 1, 11 4
  • 5. 5Thursday, December 1, 11 5
  • 6. 6Thursday, December 1, 11 6
  • 7. Bad Code VS. Clean Code WHY CLEAN CODE ?Thursday, December 1, 11 7
  • 8. Thursday, December 1, 11 8
  • 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
  • 10. Thursday, December 1, 11 10
  • 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 DevelopmentThursday, 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 NamesThursday, 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 classThursday, 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 clutterThursday, 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 narrativeThursday, 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 codesThursday, 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 ? 28Thursday, December 1, 11 28
  • 29. Error Handling • What do you think about this code ? 29Thursday, December 1, 11 29
  • 30. Error Handling • Don’t return Null ! 30Thursday, December 1, 11 30
  • 31. Error Handling • What do you think about this code ? 31Thursday, December 1, 11 31
  • 32. Error Handling • How to make code look like that ? • Special Case object ! 32Thursday, December 1, 11 32
  • 33. Side Effects • Consider the following class : 33Thursday, December 1, 11 33
  • 34. Side Effects • Consider the following code : • Do you see the problem ? 34Thursday, 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 ! 35Thursday, 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 36Thursday, December 1, 11 36
  • 37. Side Effects 37Thursday, December 1, 11 37
  • 38. Side Effects • What this code display ? 38Thursday, December 1, 11 38
  • 39. Side Effects 39Thursday, December 1, 11 39
  • 40. Side Effects 40Thursday, December 1, 11 40
  • 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 ! 41Thursday, December 1, 11 41
  • 42. Side Effects • Favor composition over inheritance ! 42Thursday, December 1, 11 42
  • 43. Do you want to read more about the subject ? REFERENCES 43Thursday, December 1, 11 43
  • 44. Books Clean Code A Handbook of Agile Software Craftsmanship Robert C. Martin (aka. Uncle Bob) Prentice Hall editions 44Thursday, December 1, 11 44
  • 45. Books Effective Java Second Edition Joshua Bloch Addison Wesley editions 45Thursday, 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/ 46Thursday, December 1, 11 46