SlideShare una empresa de Scribd logo
1 de 15
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

Más contenido relacionado

La actualidad más candente

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 
Clean code: meaningful Name
Clean code: meaningful NameClean code: meaningful Name
Clean code: meaningful Name
nahid035
 

La actualidad más candente (20)

Clean code
Clean codeClean code
Clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
 
Javascript Clean Code
Javascript Clean CodeJavascript Clean Code
Javascript Clean Code
 
Clean code: meaningful Name
Clean code: meaningful NameClean code: meaningful Name
Clean code: meaningful Name
 
Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a Monolith
 
clean code
clean codeclean code
clean code
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 

Similar a Clean code

Whats Preventing Me To Write Nearly Accurate Code.Key
Whats Preventing Me To Write Nearly Accurate Code.KeyWhats Preventing Me To Write Nearly Accurate Code.Key
Whats Preventing Me To Write Nearly Accurate Code.Key
nhm taveer hossain khan
 

Similar a Clean code (20)

AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
 
Striving towards better PHP code
Striving towards better PHP codeStriving towards better PHP code
Striving towards better PHP code
 
Clean Code and Common Engineering Practices
Clean Code and Common Engineering PracticesClean Code and Common Engineering Practices
Clean Code and Common Engineering Practices
 
CLEAN WEB
CLEAN WEBCLEAN WEB
CLEAN WEB
 
As a Salesforce Developer I will... 7 Ground Rules for Success, Robert Sösemann
As a Salesforce Developer I will... 7 Ground Rules for Success, Robert SösemannAs a Salesforce Developer I will... 7 Ground Rules for Success, Robert Sösemann
As a Salesforce Developer I will... 7 Ground Rules for Success, Robert Sösemann
 
Clean code
Clean codeClean code
Clean code
 
Raya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readabilityRaya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readability
 
Clean Code Pt I
Clean Code Pt IClean Code Pt I
Clean Code Pt I
 
Whats Preventing Me To Write Nearly Accurate Code.Key
Whats Preventing Me To Write Nearly Accurate Code.KeyWhats Preventing Me To Write Nearly Accurate Code.Key
Whats Preventing Me To Write Nearly Accurate Code.Key
 
Javascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxJavascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptx
 
Seven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC OsloSeven ways to be a happier JavaScript developer - NDC Oslo
Seven ways to be a happier JavaScript developer - NDC Oslo
 
Managing Tech Teams
Managing Tech TeamsManaging Tech Teams
Managing Tech Teams
 
PHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHPPHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHP
 
Huong dan viet cv cho Lap Trinh Vien, Cach phong van xin viec hieu qua - TopC...
Huong dan viet cv cho Lap Trinh Vien, Cach phong van xin viec hieu qua - TopC...Huong dan viet cv cho Lap Trinh Vien, Cach phong van xin viec hieu qua - TopC...
Huong dan viet cv cho Lap Trinh Vien, Cach phong van xin viec hieu qua - TopC...
 
Software Design Notes
Software Design NotesSoftware Design Notes
Software Design Notes
 
Олег Мізьов "Reading between lines" Lviv Project Management Day 2017
Олег Мізьов "Reading between lines" Lviv Project Management Day 2017Олег Мізьов "Reading between lines" Lviv Project Management Day 2017
Олег Мізьов "Reading between lines" Lviv Project Management Day 2017
 
Can my underperforming law firm website be saved?
Can my underperforming law firm website be saved?Can my underperforming law firm website be saved?
Can my underperforming law firm website be saved?
 
Sanely working with Legacy Code - PyTexas 2024
Sanely working with Legacy Code - PyTexas 2024Sanely working with Legacy Code - PyTexas 2024
Sanely working with Legacy Code - PyTexas 2024
 
CLEAN CODE
CLEAN CODECLEAN CODE
CLEAN CODE
 

Último

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Último (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 

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