SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
Clean Code

    Hendrik Ebel
   10. Mar. 2009
                   1
Topics
Motivation
Meaningful Names
Comments
Functions
                              By Robert C. Martin & Co.


Objects and Data Structures      Object Mentor Inc.




Error Handling
Unit Tests
                                                          2
What is Clean Code?
One Question ...




                      3
What is Clean Code?
   One Question ...
                              elegant
                                           efficient
        simple and direct

                             readability         easy to enhance
 like a well-written prose
                                        care
                no duplications
                              was made for the problem
...many answers!
                                                                   4
What is Clean Code?
   One Question ...
                              elegant
                                           efficient
        simple and direct

                             readability         easy to enhance
 like a well-written prose
                                        care
                no duplications
                              was made for the problem
...many answers!
                                                                   5
Motivation
The Total Cost of Owning a Mess




          productivity vs. time

                                  6
Aims of Clean Code

producing better code
@author writing for readers
code has to be kept clean over time
„Leave the campground cleaner than
you found it.“


                                      7
Meaningful Names




                   8
Meaningful Names
variable, function or class names should
answer all the big questions:

 why it exists?
 what it does?
 how it is used?


                                           9
Meaningful Names
If a name requires a comment, then the
name does not reveal its intent
avoid disinformation
 don't use type information in names
 (example: personList)
 Spelling similar concepts similarly is
 information. Using inconsistent spellings is
 disinformation.

                                                10
Comments




           11
Good and Bad Comments
•Public API Comments     •Redundant Comments
•Legal Comments          •Noise Comments
•Explanation of Intent   •Position Markers
•Warning for             •Closing Brace Comments
 Consequences            •Commented-Out Code
•real TODO Comments      •Obsolete Comments
                         •Nonpublic JavaDocs



                                                   12
Comments

„Purpose of a comment is to explain code
that does not explain itself.“
Comments do not make up for bad code
Don‘t use a comment when you can use a
function or a variable
Comments can contains lies

                                           13
Functions




            14
Functions
The goal is to tell the story of the
system.
„The first rule of functions is that they
should be small.“
Do One Thing!
Stepdown Rule


                                            15
Functions
Ideal number of arguments is zero
More than three should‘t be used
anyway
Flag arguments are ugly.
Avoid output arguments
Side effects are lies.


                                    16
Objects and Data
   Structures




                   17
Objects
hide data and expose functions
easy to add new objects
hard to add new behaviors




                                 18
Data Structures
expose data and have no meaningful
functions
easy to add new behaviors
hard to add new data structures

Choose the approach that is best for the job.



                                                19
Error Handling




                 20
Error Handling

Write code that is clean and rebust
See error handling as a separate concern
Use exceptions rather than return codes
Use unchecked exceptions



                                           21
Error Handling
Don‘t return NULL
 throwing an exception or a special
 case object like
 „Collections.emptyList()“
Don‘t pass NULL
 InvalidArgumentExceptions or assert
 better: forbid passing NULL by default
                                          22
Unit Tests




             23
Unit Tests
Test Driven Development (TDD)
 test and production code are written
 together
 tests just a few seconds ahead
Keeping tests clean
Test code is just as important as
production code.

                                        24
Unit Tests
One Assert or Single Concept per Test
F.I.R.S.T.
  Fast
  Independent
  Repeatable (in any environment)
  Self-Validation
  Timly

                                        25
Thanks - Any Questions?




                          26
Sources
Book
  „Clean Code“ by Robert C. Martin
  ISBN: 0132350882
Images
  http://www.failblog.org
  http://www.flickr.com/photos/hugovk/199425487/
  http://www.flickr.com/photos/jackpot321/1809424991/
  http://www.osnews.com/story/19266/WTFs_m

                                                        27

Más contenido relacionado

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 
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
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Clean Code (PDF version)

  • 1. Clean Code Hendrik Ebel 10. Mar. 2009 1
  • 2. Topics Motivation Meaningful Names Comments Functions By Robert C. Martin & Co. Objects and Data Structures Object Mentor Inc. Error Handling Unit Tests 2
  • 3. What is Clean Code? One Question ... 3
  • 4. What is Clean Code? One Question ... elegant efficient simple and direct readability easy to enhance like a well-written prose care no duplications was made for the problem ...many answers! 4
  • 5. What is Clean Code? One Question ... elegant efficient simple and direct readability easy to enhance like a well-written prose care no duplications was made for the problem ...many answers! 5
  • 6. Motivation The Total Cost of Owning a Mess productivity vs. time 6
  • 7. Aims of Clean Code producing better code @author writing for readers code has to be kept clean over time „Leave the campground cleaner than you found it.“ 7
  • 9. Meaningful Names variable, function or class names should answer all the big questions: why it exists? what it does? how it is used? 9
  • 10. Meaningful Names If a name requires a comment, then the name does not reveal its intent avoid disinformation don't use type information in names (example: personList) Spelling similar concepts similarly is information. Using inconsistent spellings is disinformation. 10
  • 11. Comments 11
  • 12. Good and Bad Comments •Public API Comments •Redundant Comments •Legal Comments •Noise Comments •Explanation of Intent •Position Markers •Warning for •Closing Brace Comments Consequences •Commented-Out Code •real TODO Comments •Obsolete Comments •Nonpublic JavaDocs 12
  • 13. Comments „Purpose of a comment is to explain code that does not explain itself.“ Comments do not make up for bad code Don‘t use a comment when you can use a function or a variable Comments can contains lies 13
  • 14. Functions 14
  • 15. Functions The goal is to tell the story of the system. „The first rule of functions is that they should be small.“ Do One Thing! Stepdown Rule 15
  • 16. Functions Ideal number of arguments is zero More than three should‘t be used anyway Flag arguments are ugly. Avoid output arguments Side effects are lies. 16
  • 17. Objects and Data Structures 17
  • 18. Objects hide data and expose functions easy to add new objects hard to add new behaviors 18
  • 19. Data Structures expose data and have no meaningful functions easy to add new behaviors hard to add new data structures Choose the approach that is best for the job. 19
  • 21. Error Handling Write code that is clean and rebust See error handling as a separate concern Use exceptions rather than return codes Use unchecked exceptions 21
  • 22. Error Handling Don‘t return NULL throwing an exception or a special case object like „Collections.emptyList()“ Don‘t pass NULL InvalidArgumentExceptions or assert better: forbid passing NULL by default 22
  • 24. Unit Tests Test Driven Development (TDD) test and production code are written together tests just a few seconds ahead Keeping tests clean Test code is just as important as production code. 24
  • 25. Unit Tests One Assert or Single Concept per Test F.I.R.S.T. Fast Independent Repeatable (in any environment) Self-Validation Timly 25
  • 26. Thanks - Any Questions? 26
  • 27. Sources Book „Clean Code“ by Robert C. Martin ISBN: 0132350882 Images http://www.failblog.org http://www.flickr.com/photos/hugovk/199425487/ http://www.flickr.com/photos/jackpot321/1809424991/ http://www.osnews.com/story/19266/WTFs_m 27