SlideShare a Scribd company logo
CLEAN CODE 
Par : Abdelhakim Bachar
Plan 
• Introduction 
• Meaningful Names 
• Functions 
• Data Structures 
• Error Handling 
• Class 
• Unit Tests 
• Refactoring 
• Smells and Heuristics
Introduction 
What Is Clean Code? There are 
probably as many definitions as there 
are programmers. 
I like my code to be elegant and efficient. The logic should be 
straightforward to make it hard for bugs to hide, the 
dependencies minimal to ease maintenance, error handling 
complete according to an articulated strategy, and 
performance close to optimal so as not to tempt people to 
make the code messy with unprincipled optimizations. Clean 
code does one thing well. 
Bjarne Stroustrup, inventor of C++ and author of The C++ Programming Language
Meaningful Names 
• Use Intention-Revealing Names 
• Avoid Disinformation 
• Use Pronounceable Names 
• Avoid Encodings 
• Class Names 
• Method Names 
• Pick One Word per Concept 
• Don’t Pun 
• Use Solution Domain Names
Functions 
• Small 
• Do One Thing 
• One Level of Abstraction per Function 
• Use Descriptive Names 
• Function Arguments 
• Command Query Separation 
• Prefer Exceptions to Returning Error 
Codes
Data Structures 
• Data Abstraction 
• The Law of Demeter 
• Data Transfer Objects
Error Handling 
• Use Exceptions Rather Than Return Codes 
• Write Your Try-Catch-Finally Statement First 
• Provide Context with Exceptions 
• Define Exception Classes in Terms of a 
Caller’s Needs 
• Define the Normal Flow 
• Don’t Return Null 
• Don’t Pass Null
Class 
• Class Organization 
– Encapsulation 
• Classes Should Be Small 
– The Single Responsibility Principle 
– Cohesion 
• Organizing for Change 
– Isolating from Change
Unit Tests 
• The Three Laws of TDD 
• Keeping Tests Clean 
– Tests Enable the -ilities 
• Clean Tests 
– Domain-Specific Testing Language1 
– A Dual Standard 
• One Assert per Test 
– Single Concept per Test 
• F.I.R.S.T
Refactoring 
• First, Make It Work 
• Then Make It Right
Smells and Heuristics 
• General 
• Environment 
• Comments 
• Functions 
• Java 
• Names 
• Tests
Smells and Heuristics : General 
• Multiple Languages in One Source File 
• Incorrect Behavior at the Boundaries 
• Duplication 
• Base Classes Depending on Their Derivatives 
• Too Much Information 
• Dead Code 
• Vertical Separation 
• Function Names Should Say What They Do 
• Follow Standard Conventions 
• Replace Magic Numbers with Named Constants 
• Be Precise 
• Avoid Negative Conditionals 
• Functions Should Do One Thing 
• One Level of Abstraction 
• Keep Configurable Data at High Levels 
• Avoid Transitive Navigation
Smells and Heuristics : Environment 
• Build Requires More Than One Step 
• Tests Require More Than One Step
Smells and Heuristics : Comments 
• Inappropriate Information 
• Obsolete Comment 
• Redundant Comment 
• Poorly Written Comment 
• Commented-Out Code
Smells and Heuristics : Functions 
• Too Many Arguments 
• Output Arguments 
• Flag Arguments 
• Dead Function
Smells and Heuristics : Java 
• Avoid Long Import Lists by Using 
Wildcards 
• Don’t Inherit Constants 
• Constants versus Enums
Smells and Heuristics : Names 
• Choose Descriptive Names 
• Choose Names at the Appropriate 
Level of Abstraction 
• Use Standard Nomenclature Where 
Possible 
• Avoid Encodings 
• Names Should Describe Side-Effects
Smells and Heuristics : Tests 
• Insufficient Tests 
• Use a Coverage Tool! 
• Don’t Skip Trivial Tests 
• An Ignored Test Is a Question about an 
Ambiguity 
• Test Boundary Conditions 
• Exhaustively Test Near Bugs 
• Tests Should Be Fast
Merci à vous

More Related Content

Similar to Clean Code

Clean Code
Clean CodeClean Code
Clean Code
swaraj Patil
 
Writing Clean Code (Recommendations by Robert Martin)
Writing Clean Code (Recommendations by Robert Martin)Writing Clean Code (Recommendations by Robert Martin)
Writing Clean Code (Recommendations by Robert Martin)
Shirish Bari
 
Android Deobfuscation: Tools and Techniques
Android Deobfuscation: Tools and TechniquesAndroid Deobfuscation: Tools and Techniques
Android Deobfuscation: Tools and Techniques
caleb194331
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
Juggernaut Liu
 
Clean code
Clean codeClean code
Clean code
Simon Sönnby
 
Introduction to Testing and TDD
Introduction to Testing and TDDIntroduction to Testing and TDD
Introduction to Testing and TDD
Sarah Dutkiewicz
 
Ruby code smells
Ruby code smellsRuby code smells
Ruby code smells
Rodrigo Urubatan
 
Eurosport's Kodakademi #2
Eurosport's Kodakademi #2Eurosport's Kodakademi #2
Eurosport's Kodakademi #2
Benjamin Baumann
 
AP computer barron book ppt AP CS A.pptx
AP computer barron book ppt AP CS A.pptxAP computer barron book ppt AP CS A.pptx
AP computer barron book ppt AP CS A.pptx
KoutheeshSellamuthu
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
Sarah Dutkiewicz
 
Art of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices AntipatternsArt of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices Antipatterns
El Mahdi Benzekri
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
Jaime Jorge
 
Metric driven refactoring
Metric driven refactoringMetric driven refactoring
Metric driven refactoring
Nick Harrison
 
Software design with Domain-driven design
Software design with Domain-driven design Software design with Domain-driven design
Software design with Domain-driven design
Allan Mangune
 
Writing clean and maintainable code
Writing clean and maintainable codeWriting clean and maintainable code
Writing clean and maintainable code
Marko Heijnen
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
Noam Kfir
 
TDD In Practice
TDD In PracticeTDD In Practice
TDD In Practice
Alan Christensen
 
Testing-Tools-Magnitia-Content.pdf
Testing-Tools-Magnitia-Content.pdfTesting-Tools-Magnitia-Content.pdf
Testing-Tools-Magnitia-Content.pdf
AnanthReddy38
 
Refactoring
RefactoringRefactoring
Php traits
Php traitsPhp traits
Php traits
Shaun Morrow
 

Similar to Clean Code (20)

Clean Code
Clean CodeClean Code
Clean Code
 
Writing Clean Code (Recommendations by Robert Martin)
Writing Clean Code (Recommendations by Robert Martin)Writing Clean Code (Recommendations by Robert Martin)
Writing Clean Code (Recommendations by Robert Martin)
 
Android Deobfuscation: Tools and Techniques
Android Deobfuscation: Tools and TechniquesAndroid Deobfuscation: Tools and Techniques
Android Deobfuscation: Tools and Techniques
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
Clean code
Clean codeClean code
Clean code
 
Introduction to Testing and TDD
Introduction to Testing and TDDIntroduction to Testing and TDD
Introduction to Testing and TDD
 
Ruby code smells
Ruby code smellsRuby code smells
Ruby code smells
 
Eurosport's Kodakademi #2
Eurosport's Kodakademi #2Eurosport's Kodakademi #2
Eurosport's Kodakademi #2
 
AP computer barron book ppt AP CS A.pptx
AP computer barron book ppt AP CS A.pptxAP computer barron book ppt AP CS A.pptx
AP computer barron book ppt AP CS A.pptx
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
Art of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices AntipatternsArt of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices Antipatterns
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
 
Metric driven refactoring
Metric driven refactoringMetric driven refactoring
Metric driven refactoring
 
Software design with Domain-driven design
Software design with Domain-driven design Software design with Domain-driven design
Software design with Domain-driven design
 
Writing clean and maintainable code
Writing clean and maintainable codeWriting clean and maintainable code
Writing clean and maintainable code
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
TDD In Practice
TDD In PracticeTDD In Practice
TDD In Practice
 
Testing-Tools-Magnitia-Content.pdf
Testing-Tools-Magnitia-Content.pdfTesting-Tools-Magnitia-Content.pdf
Testing-Tools-Magnitia-Content.pdf
 
Refactoring
RefactoringRefactoring
Refactoring
 
Php traits
Php traitsPhp traits
Php traits
 

Recently uploaded

Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 

Recently uploaded (20)

Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 

Clean Code

  • 1. CLEAN CODE Par : Abdelhakim Bachar
  • 2. Plan • Introduction • Meaningful Names • Functions • Data Structures • Error Handling • Class • Unit Tests • Refactoring • Smells and Heuristics
  • 3. Introduction What Is Clean Code? There are probably as many definitions as there are programmers. I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well. Bjarne Stroustrup, inventor of C++ and author of The C++ Programming Language
  • 4. Meaningful Names • Use Intention-Revealing Names • Avoid Disinformation • Use Pronounceable Names • Avoid Encodings • Class Names • Method Names • Pick One Word per Concept • Don’t Pun • Use Solution Domain Names
  • 5. Functions • Small • Do One Thing • One Level of Abstraction per Function • Use Descriptive Names • Function Arguments • Command Query Separation • Prefer Exceptions to Returning Error Codes
  • 6. Data Structures • Data Abstraction • The Law of Demeter • Data Transfer Objects
  • 7. Error Handling • Use Exceptions Rather Than Return Codes • Write Your Try-Catch-Finally Statement First • Provide Context with Exceptions • Define Exception Classes in Terms of a Caller’s Needs • Define the Normal Flow • Don’t Return Null • Don’t Pass Null
  • 8. Class • Class Organization – Encapsulation • Classes Should Be Small – The Single Responsibility Principle – Cohesion • Organizing for Change – Isolating from Change
  • 9. Unit Tests • The Three Laws of TDD • Keeping Tests Clean – Tests Enable the -ilities • Clean Tests – Domain-Specific Testing Language1 – A Dual Standard • One Assert per Test – Single Concept per Test • F.I.R.S.T
  • 10. Refactoring • First, Make It Work • Then Make It Right
  • 11. Smells and Heuristics • General • Environment • Comments • Functions • Java • Names • Tests
  • 12. Smells and Heuristics : General • Multiple Languages in One Source File • Incorrect Behavior at the Boundaries • Duplication • Base Classes Depending on Their Derivatives • Too Much Information • Dead Code • Vertical Separation • Function Names Should Say What They Do • Follow Standard Conventions • Replace Magic Numbers with Named Constants • Be Precise • Avoid Negative Conditionals • Functions Should Do One Thing • One Level of Abstraction • Keep Configurable Data at High Levels • Avoid Transitive Navigation
  • 13. Smells and Heuristics : Environment • Build Requires More Than One Step • Tests Require More Than One Step
  • 14. Smells and Heuristics : Comments • Inappropriate Information • Obsolete Comment • Redundant Comment • Poorly Written Comment • Commented-Out Code
  • 15. Smells and Heuristics : Functions • Too Many Arguments • Output Arguments • Flag Arguments • Dead Function
  • 16. Smells and Heuristics : Java • Avoid Long Import Lists by Using Wildcards • Don’t Inherit Constants • Constants versus Enums
  • 17. Smells and Heuristics : Names • Choose Descriptive Names • Choose Names at the Appropriate Level of Abstraction • Use Standard Nomenclature Where Possible • Avoid Encodings • Names Should Describe Side-Effects
  • 18. Smells and Heuristics : Tests • Insufficient Tests • Use a Coverage Tool! • Don’t Skip Trivial Tests • An Ignored Test Is a Question about an Ambiguity • Test Boundary Conditions • Exhaustively Test Near Bugs • Tests Should Be Fast