SlideShare a Scribd company logo
5
Most read
12
Most read
13
Most read
Clean code
Nguyen Quang Duc
Contents
▪ What is clean code, bad code
▪ Why it matters
▪ Meaningful names and formatting
▪ Functions
▪ Comments
Clean code vs bad code
Clean code
▪ Easy to understand
▪ Easy to maintain
▪ Easy to extend
Bad code
▪ Mysterious
▪ Fragile
▪ Dangerous
Why do we write bad code
▪ Tight schedule
▪ Tired of project
▪ Get working now, clean up later (or never)
▪ Everybody does it!
Why should we write clean code
▪ We spent most of our time reading rather than writing
▪ Bad code will cost you:
▪ Time
▪ Money
▪ Relationship
▪ Talent
The boy scout rule
▪ Leave the campground cleaner than you found it.
How do we write clean
code
Meaningful names
▪ Use Intention-Revealing Names
▪ Don’t be afraid of longer names, the name should say everything about it.
▪ Avoid abbreviation
This is bad:
protected $d;
This is good:
protected $elapsedTimeInDays;
protected $daysSinceCreation;
protected $daysSinceModification;
protected $fileAgeInDays;
▪ Use pronounceable name
This is bad:
public $genymdhms;
public $modymdhms;
This is good:
public $generationTimestamp;
public $modificationTimestamp;
▪ Use one word per concept
Be consistent. For example, don’t use get and fetch to do the same thing in different classes
▪ Use verbs for function names and nouns for classes and attributes
Formatting
▪ Follow a single coding style
▪ There is no obvious "best" style. Just being consistent.
▪ Each line should be composed of a single expression.
▪ Each block should present a single thought.
▪ Blocks should be separated with a single, empty line.
▪ A line of the code should not exceed 80 characters
▪ Our eyes are more comfortable when reading tall and narrow columns of text
▪ Use indentation and alignment to improve the readability
Functions
▪ The smaller the better
▪ A function should only do one thing
▪ No nested control structure
▪ Less arguments are better
▪ No side effects
▪ Avoid output arguments
▪ Don’t repeat yourself
Hard to read Easy to understand
Comments
▪ Don’t comment bad code, rewrite it
▪ If code is readable you don’t need comments
▪ Explain your intention in comments
▪ Warn of consequences in comments
▪ Emphasis important points in comments
▪ Always have you PHPDoc comments
▪ Noise comments are bad
▪ Never leave code commented
▪ The best comment is the code itself
For example:
// check if customer can buy products
if ($customer->isActive() && $customer->getAge() > 65)
is not as good as:
if ($customer->canBuyProducts())
▪ Avoid obvious comments
▪ Other kind of useful comments
Legal comments, e.g. // Copyright (c) 2014 Magento Inc.
Informative comments, e.g. // Returns an instance of a Customer object
Purpose-explaining comments, e.g. return 1; // it is default value
Warning comments, e.g. // very slow query, don’t use if you don’t need to
TODO comments, e.g. // @TODO rewrite this query
Reference
▪ Clean Code: A Handbook of Agile
Software Craftsmanship by Robert
C. Martin
▪ http://www.slideshare.net/mariosa
ngiorgio/clean-code-and-code-
smells
▪ http://www.slideshare.net/JandV/c
lean-code-summary
▪ http://www.slideshare.net/josedasi
lva/phplx

More Related Content

PPTX
clean code book summary - uncle bob - English version
saber tabatabaee
 
PDF
Clean code
Achintya Kumar
 
PPTX
Clean code slide
Anh Huan Miu
 
KEY
Clean Code
Hendrik Ebel
 
KEY
Clean code and Code Smells
Mario Sangiorgio
 
PPTX
Clean code
Mahmoud Zizo
 
PPTX
Clean code
ifnu bima
 
PPTX
Clean Code I - Best Practices
Theo Jungeblut
 
clean code book summary - uncle bob - English version
saber tabatabaee
 
Clean code
Achintya Kumar
 
Clean code slide
Anh Huan Miu
 
Clean Code
Hendrik Ebel
 
Clean code and Code Smells
Mario Sangiorgio
 
Clean code
Mahmoud Zizo
 
Clean code
ifnu bima
 
Clean Code I - Best Practices
Theo Jungeblut
 

What's hot (20)

PPTX
Clean Code
swaraj Patil
 
PPTX
Clean Code
Dmytro Turskyi
 
PPTX
Clean code
Henrique Smoco
 
ODP
Clean code
Knoldus Inc.
 
PDF
Clean Code
ISchwarz23
 
PDF
Clean code
Alvaro García Loaisa
 
PDF
Clean code
Arturo Herrero
 
PPTX
Naming Standards, Clean Code
CleanestCode
 
PPT
Clean Code summary
Jan de Vries
 
PPTX
Clean Code Principles
YeurDreamin'
 
PPTX
The Art of Clean code
Victor Rentea
 
PDF
Clean coding-practices
John Ferguson Smart Limited
 
PPTX
Clean Code
Victor Rentea
 
PDF
clean code
Douglas Siviotti
 
PDF
Clean code: meaningful Name
nahid035
 
PPTX
Clean Code: Chapter 3 Function
Kent Huang
 
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
PPTX
Apresentação Clean Code
André Leoni
 
PPTX
Intro to c++
temkin abdlkader
 
PDF
Writing clean code
Angel Garcia Olloqui
 
Clean Code
swaraj Patil
 
Clean Code
Dmytro Turskyi
 
Clean code
Henrique Smoco
 
Clean code
Knoldus Inc.
 
Clean Code
ISchwarz23
 
Clean code
Arturo Herrero
 
Naming Standards, Clean Code
CleanestCode
 
Clean Code summary
Jan de Vries
 
Clean Code Principles
YeurDreamin'
 
The Art of Clean code
Victor Rentea
 
Clean coding-practices
John Ferguson Smart Limited
 
Clean Code
Victor Rentea
 
clean code
Douglas Siviotti
 
Clean code: meaningful Name
nahid035
 
Clean Code: Chapter 3 Function
Kent Huang
 
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
Apresentação Clean Code
André Leoni
 
Intro to c++
temkin abdlkader
 
Writing clean code
Angel Garcia Olloqui
 
Ad

Similar to Clean code (20)

PPTX
Clean code - DSC DYPCOE
Patil Shreyas
 
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
Gavin Pickin
 
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
Ortus Solutions, Corp
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
komvjzfjj621
 
PDF
WordCamp Nashville: Clean Code for WordPress
mtoppa
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
sagolbencib
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
krantzloigu
 
PDF
Writing Readable Code
eddiehaber
 
PDF
Clean Code V2
Jean Carlo Machado
 
PDF
Clean Code
Chris Farrell
 
PPTX
Clean Code Pt I
Paul Boocock
 
PDF
UNIT I cloud computing ppt cloud ccd all about the cloud computing
vishnubala78900
 
PDF
Good code, Bad Code
josedasilva
 
ODP
Clean Code - Part 2
Knoldus Inc.
 
PPTX
Best-Practices-in-Writing-Clean-Maintainable-Code
Ozias Rondon
 
PDF
Socrates15 - Clean Code
Victor Maliy
 
PPTX
CLEAN CODING AND DEVOPS Final.pptx
JEEVANANTHAMG6
 
PPSX
Clean code
AgniGonalves
 
PPTX
Clean code
Smita Prasad
 
PDF
Clean Code
Daniel Kummer
 
Clean code - DSC DYPCOE
Patil Shreyas
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
Gavin Pickin
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
Ortus Solutions, Corp
 
Clean Code. An Agile Guide to Software Craft Kameron H.
komvjzfjj621
 
WordCamp Nashville: Clean Code for WordPress
mtoppa
 
Clean Code. An Agile Guide to Software Craft Kameron H.
sagolbencib
 
Clean Code. An Agile Guide to Software Craft Kameron H.
krantzloigu
 
Writing Readable Code
eddiehaber
 
Clean Code V2
Jean Carlo Machado
 
Clean Code
Chris Farrell
 
Clean Code Pt I
Paul Boocock
 
UNIT I cloud computing ppt cloud ccd all about the cloud computing
vishnubala78900
 
Good code, Bad Code
josedasilva
 
Clean Code - Part 2
Knoldus Inc.
 
Best-Practices-in-Writing-Clean-Maintainable-Code
Ozias Rondon
 
Socrates15 - Clean Code
Victor Maliy
 
CLEAN CODING AND DEVOPS Final.pptx
JEEVANANTHAMG6
 
Clean code
AgniGonalves
 
Clean code
Smita Prasad
 
Clean Code
Daniel Kummer
 
Ad

Recently uploaded (20)

PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Exploring AI Agents in Process Industries
amoreira6
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 

Clean code

  • 2. Contents ▪ What is clean code, bad code ▪ Why it matters ▪ Meaningful names and formatting ▪ Functions ▪ Comments
  • 3. Clean code vs bad code Clean code ▪ Easy to understand ▪ Easy to maintain ▪ Easy to extend Bad code ▪ Mysterious ▪ Fragile ▪ Dangerous
  • 4. Why do we write bad code ▪ Tight schedule ▪ Tired of project ▪ Get working now, clean up later (or never) ▪ Everybody does it!
  • 5. Why should we write clean code ▪ We spent most of our time reading rather than writing ▪ Bad code will cost you: ▪ Time ▪ Money ▪ Relationship ▪ Talent
  • 6. The boy scout rule ▪ Leave the campground cleaner than you found it.
  • 7. How do we write clean code
  • 8. Meaningful names ▪ Use Intention-Revealing Names ▪ Don’t be afraid of longer names, the name should say everything about it. ▪ Avoid abbreviation This is bad: protected $d; This is good: protected $elapsedTimeInDays; protected $daysSinceCreation; protected $daysSinceModification; protected $fileAgeInDays;
  • 9. ▪ Use pronounceable name This is bad: public $genymdhms; public $modymdhms; This is good: public $generationTimestamp; public $modificationTimestamp; ▪ Use one word per concept Be consistent. For example, don’t use get and fetch to do the same thing in different classes ▪ Use verbs for function names and nouns for classes and attributes
  • 10. Formatting ▪ Follow a single coding style ▪ There is no obvious "best" style. Just being consistent. ▪ Each line should be composed of a single expression. ▪ Each block should present a single thought. ▪ Blocks should be separated with a single, empty line. ▪ A line of the code should not exceed 80 characters ▪ Our eyes are more comfortable when reading tall and narrow columns of text ▪ Use indentation and alignment to improve the readability
  • 11. Functions ▪ The smaller the better ▪ A function should only do one thing ▪ No nested control structure ▪ Less arguments are better ▪ No side effects ▪ Avoid output arguments ▪ Don’t repeat yourself
  • 12. Hard to read Easy to understand
  • 13. Comments ▪ Don’t comment bad code, rewrite it ▪ If code is readable you don’t need comments ▪ Explain your intention in comments ▪ Warn of consequences in comments ▪ Emphasis important points in comments ▪ Always have you PHPDoc comments ▪ Noise comments are bad ▪ Never leave code commented
  • 14. ▪ The best comment is the code itself For example: // check if customer can buy products if ($customer->isActive() && $customer->getAge() > 65) is not as good as: if ($customer->canBuyProducts()) ▪ Avoid obvious comments ▪ Other kind of useful comments Legal comments, e.g. // Copyright (c) 2014 Magento Inc. Informative comments, e.g. // Returns an instance of a Customer object Purpose-explaining comments, e.g. return 1; // it is default value Warning comments, e.g. // very slow query, don’t use if you don’t need to TODO comments, e.g. // @TODO rewrite this query
  • 15. Reference ▪ Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin ▪ http://www.slideshare.net/mariosa ngiorgio/clean-code-and-code- smells ▪ http://www.slideshare.net/JandV/c lean-code-summary ▪ http://www.slideshare.net/josedasi lva/phplx