SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
Clean Code


                     Presentation of the Uncle Bob's Book.




Thursday, December 1, 11                                         1
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
Agenda

      • Software Craftsmanship

      • Why Clean Code ?

      • Tips & Tricks to write Clean Code

      • References




Thursday, December 1, 11                    3
What is that ?

         SOFTWARE CRAFTSMANSHIP



Thursday, December 1, 11          4
5

Thursday, December 1, 11       5
6

Thursday, December 1, 11       6
Bad Code VS. Clean Code

         WHY CLEAN CODE ?



Thursday, December 1, 11           7
Thursday, December 1, 11   8
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
Thursday, December 1, 11   10
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
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
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
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
How to write Clean Code ?

         GET YOUR HANDS DIRTY !



Thursday, December 1, 11             15
Meaningful Names
      • What is the purpose of this code ?




Thursday, December 1, 11                      16
Meaningful Names
      • And what do you think of this code ?




Thursday, December 1, 11                       17
Meaningful Names
      • Use Intention-Revealing Names




Thursday, December 1, 11                      18
Meaningful Names

      • What about Hungarian Notation ?




Thursday, December 1, 11                      19
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
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
Functions
      • What do you think about this method ?
            – FitNesse - commit 3bec390e6f


      • And now ?
            – FitNesse - commit 7491001db7


      • What has changed ?




Thursday, December 1, 11                        22
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
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
Error Handling

      • What do you think about this code ?




Thursday, December 1, 11                      25
Error Handling

      • Prefer exceptions to returning error codes




Thursday, December 1, 11                             26
Error Handling

      • And what do you think about this code ?




Thursday, December 1, 11                          27
Error Handling

      • For you what is the most common exception ?
            – NPE !


      • How to avoid it ?




                                                      28

Thursday, December 1, 11                                   28
Error Handling

      • What do you think about this code ?




                                              29

Thursday, December 1, 11                           29
Error Handling

      • Don’t return Null !




                                            30

Thursday, December 1, 11                         30
Error Handling

      • What do you think about this code ?




                                              31

Thursday, December 1, 11                           31
Error Handling

      • How to make code look like that ?




      • Special Case object !

                                            32

Thursday, December 1, 11                         32
Side Effects
      • Consider the following class :




                                          33

Thursday, December 1, 11                       33
Side Effects

      • Consider the following code :




      • Do you see the problem ?




                                          34

Thursday, December 1, 11                       34
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
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
Side Effects




                                          37

Thursday, December 1, 11                       37
Side Effects

      • What this code display ?




                                          38

Thursday, December 1, 11                       38
Side Effects




                                          39

Thursday, December 1, 11                       39
Side Effects




                                          40

Thursday, December 1, 11                       40
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
Side Effects

      • Favor composition over inheritance !




                                               42

Thursday, December 1, 11                            42
Do you want to read more about the subject ?

         REFERENCES


                                                        43

Thursday, December 1, 11                                     43
Books

                            Clean Code
   A Handbook of Agile Software Craftsmanship



              Robert C. Martin (aka. Uncle Bob)



                           Prentice Hall editions




                                                        44

Thursday, December 1, 11                                     44
Books

                       Effective Java
                           Second Edition



                            Joshua Bloch



                     Addison Wesley editions




                                                    45

Thursday, December 1, 11                                 45
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

Contenu connexe

En vedette

Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
Theo Jungeblut
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
Edorian
 

En vedette (8)

Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Clean Code: Stop wasting my time
Clean Code: Stop wasting my timeClean Code: Stop wasting my time
Clean Code: Stop wasting my time
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 

Plus de Brice Argenson

Soutenance mémoire : Implémentation d'un DSL en entreprise
Soutenance mémoire : Implémentation d'un DSL en entrepriseSoutenance mémoire : Implémentation d'un DSL en entreprise
Soutenance mémoire : Implémentation d'un DSL en entreprise
Brice Argenson
 

Plus de Brice Argenson (13)

RSpock Testing Framework for Ruby
RSpock Testing Framework for RubyRSpock Testing Framework for Ruby
RSpock Testing Framework for Ruby
 
Serverless Applications
Serverless ApplicationsServerless Applications
Serverless Applications
 
Serverless - Lunch&Learn CleverToday - Mars 2017
Serverless - Lunch&Learn CleverToday - Mars 2017Serverless - Lunch&Learn CleverToday - Mars 2017
Serverless - Lunch&Learn CleverToday - Mars 2017
 
Docker 1.13 - Docker meetup février 2017
Docker 1.13 - Docker meetup février 2017Docker 1.13 - Docker meetup février 2017
Docker 1.13 - Docker meetup février 2017
 
Docker 1.12 & Swarm Mode [Montreal Docker Meetup Sept. 2016]
Docker 1.12 & Swarm Mode [Montreal Docker Meetup Sept. 2016]Docker 1.12 & Swarm Mode [Montreal Docker Meetup Sept. 2016]
Docker 1.12 & Swarm Mode [Montreal Docker Meetup Sept. 2016]
 
Packagez et déployez vos applications avec Docker - Montréal CloudFoundry Mee...
Packagez et déployez vos applications avec Docker - Montréal CloudFoundry Mee...Packagez et déployez vos applications avec Docker - Montréal CloudFoundry Mee...
Packagez et déployez vos applications avec Docker - Montréal CloudFoundry Mee...
 
The Patterns to boost your time to market - An introduction to DevOps
The Patterns to boost your time to market - An introduction to DevOpsThe Patterns to boost your time to market - An introduction to DevOps
The Patterns to boost your time to market - An introduction to DevOps
 
Introduction to Continuous Integration with Jenkins
Introduction to Continuous Integration with JenkinsIntroduction to Continuous Integration with Jenkins
Introduction to Continuous Integration with Jenkins
 
Java EE - Servlets API
Java EE - Servlets APIJava EE - Servlets API
Java EE - Servlets API
 
JS-Everywhere - SSE Hands-on
JS-Everywhere - SSE Hands-onJS-Everywhere - SSE Hands-on
JS-Everywhere - SSE Hands-on
 
JS-Everywhere - LocalStorage Hands-on
JS-Everywhere - LocalStorage Hands-onJS-Everywhere - LocalStorage Hands-on
JS-Everywhere - LocalStorage Hands-on
 
Effective Java
Effective JavaEffective Java
Effective Java
 
Soutenance mémoire : Implémentation d'un DSL en entreprise
Soutenance mémoire : Implémentation d'un DSL en entrepriseSoutenance mémoire : Implémentation d'un DSL en entreprise
Soutenance mémoire : Implémentation d'un DSL en entreprise
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

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
  • 3. Agenda • Software Craftsmanship • Why Clean Code ? • Tips & Tricks to write Clean Code • References Thursday, December 1, 11 3
  • 4. What is that ? SOFTWARE CRAFTSMANSHIP Thursday, December 1, 11 4
  • 7. Bad Code VS. Clean Code WHY CLEAN CODE ? Thursday, December 1, 11 7
  • 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
  • 30. Error Handling • Don’t return Null ! 30 Thursday, December 1, 11 30
  • 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
  • 37. Side Effects 37 Thursday, December 1, 11 37
  • 38. Side Effects • What this code display ? 38 Thursday, December 1, 11 38
  • 39. Side Effects 39 Thursday, December 1, 11 39
  • 40. Side Effects 40 Thursday, 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 ! 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