SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code SmellsMario Sangiorgio
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency InjectionTheo Jungeblut
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best PracticesTheo Jungeblut
 
Clean Code: Stop wasting my time
Clean Code: Stop wasting my timeClean Code: Stop wasting my time
Clean Code: Stop wasting my timeEdorian
 
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-principlesEdorian
 

Andere mochten auch (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
 

Mehr von Brice Argenson

RSpock Testing Framework for Ruby
RSpock Testing Framework for RubyRSpock Testing Framework for Ruby
RSpock Testing Framework for RubyBrice Argenson
 
Serverless Applications
Serverless ApplicationsServerless Applications
Serverless ApplicationsBrice Argenson
 
Serverless - Lunch&Learn CleverToday - Mars 2017
Serverless - Lunch&Learn CleverToday - Mars 2017Serverless - Lunch&Learn CleverToday - Mars 2017
Serverless - Lunch&Learn CleverToday - Mars 2017Brice Argenson
 
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 2017Brice Argenson
 
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]Brice Argenson
 
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...Brice Argenson
 
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 DevOpsBrice Argenson
 
Introduction to Continuous Integration with Jenkins
Introduction to Continuous Integration with JenkinsIntroduction to Continuous Integration with Jenkins
Introduction to Continuous Integration with JenkinsBrice Argenson
 
Java EE - Servlets API
Java EE - Servlets APIJava EE - Servlets API
Java EE - Servlets APIBrice Argenson
 
JS-Everywhere - SSE Hands-on
JS-Everywhere - SSE Hands-onJS-Everywhere - SSE Hands-on
JS-Everywhere - SSE Hands-onBrice Argenson
 
JS-Everywhere - LocalStorage Hands-on
JS-Everywhere - LocalStorage Hands-onJS-Everywhere - LocalStorage Hands-on
JS-Everywhere - LocalStorage Hands-onBrice 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 entrepriseBrice Argenson
 

Mehr von 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
 

Kürzlich hochgeladen

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.pdfsudhanshuwaghmare1
 
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 WorkerThousandEyes
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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 FresherRemote DBA Services
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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...Miguel Araújo
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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 DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 FMESafe Software
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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, Adobeapidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
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 DiscoveryTrustArc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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 TerraformAndrey Devyatkin
 

Kürzlich hochgeladen (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
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - 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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 

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