SlideShare a Scribd company logo
1 of 27
Download to read offline
Clean Code
2
A Handbook of Agile Software Craftsmanship
Robert C. Martin
Note by waegaein@github.com
CH2 Meaningful Names
Variable Names
Function Names
Class Names
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
def sendtostore_packagestock0
→ def calculate_stock_with_notifying_store
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
• allow readers to understand difference without comment nor deduction.
def sendtostore_packagestock0
→ def calculate_stock_with_notifying_store
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
• allow readers to understand difference without comment nor deduction.
• include semantic words.
def sendtostore_packagestock0
def sendtostore_packagestock1
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
• allow readers to understand difference without comment nor deduction.
• include semantic words.
• include words that are technical or related to the problem domain.
def calculate_rate
→ def calculate_surcharge
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
• allow readers to understand difference without comment nor deduction.
• include semantic words.
• include words that are technical or related to the problem domain.
• have context from its scope.
Package_stock.package_stock_id
→ Package_stock.id
CH2 Meaningful Names
Names should
• tell you why it exists, what it does, and how it is used.
• allow readers to understand difference without comment nor deduction.
• include semantic words.
• include words that are technical or related to the problem domain.
• have context from its scope.
• consists of words.
daydelay
→ days_delayed
CH2 Meaningful Names
Names should not
• include obscure clues that may lead to false conclusions.
def create_invoice_with_assurance
→ def create_invoice_with_retry
CH2 Meaningful Names
Names should not
• include obscure clues that may lead to false conclusions.
• use inconsistent synonyms for a single concept.
(‘calculate_stock_with_notifying_store’ exists)
def send_mail_to_branch
→ def send_mail_to_store
CH2 Meaningful Names
Names should not
• include obscure clues that may lead to false conclusions.
• use inconsistent synonyms for a single concept.
• include syntactic encodings.
v_count
→ count
CH2 Meaningful Names
Names should not
• include obscure clues that may lead to false conclusions.
• use inconsistent synonyms for a single concept.
• include syntactic encodings.
• consists of over-abbreviated letters.
def get_psscharge
→ def get_package_stock_surchage
CH2 Meaningful Names
• Prefer clarity to smartness and entertainment.
_, _ = select.select([], [])
→ discarded, discarded = select.select(list(), list())
CH2 Meaningful Names
• Prefer clarity to smartness and entertainment.
• Class names are nouns or noun phrases.
class SlackNotify
→ class SlackNotification
CH2 Meaningful Names
• Prefer clarity to smartness and entertainment.
• Class names are nouns or noun phrases.
• Method names are verbs or verb phrases.
def surcharge_calculation
→ def calculate_surcharge
CH3 Functions
Functions should
• be as small as possible.
CH3 Functions
Functions should
• be as small as possible.
• have the indent level of one or two.
CH3 Functions
Functions should
• be as small as possible.
• have the indent level of one or two.
• do only one thing.
CH3 Functions
Functions should
• be as small as possible.
• have the indent level of one or two.
• do only one thing.
• include steps within one level of abstraction.
CH3 Functions
Functions should
• be as small as possible.
• have the indent level of one or two.
• do only one thing.
• include steps within one level of abstraction.
• have two arguments at maximum, taking advantage of argument objects/lists.
def send_mail_with_file(email_from, email_to, subject, html_content, attach_file_path,
attach_file_name, mime_type)
→ def send_mail_with_file(base_email, attachment)
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
• pass Boolean flag argument as it forces more than one behavior.
calculate_surcharge(is_promotion=False, …)
→ calculate_surcharge, calcuate_surcharge_for_promotion
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
• pass Boolean flag argument as it forces more than one behavior.
• transform input argument instead of using return value.
…
charge_list.sort()
…
return
…
charge_list_sorted = sorted(charge_list)
…
return charge_list_sorted
→
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
• pass Boolean flag argument as it forces more than one behavior.
• transform input argument instead of using return value.
• have side effects that are not implied by names.
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
• pass Boolean flag argument as it forces more than one behavior.
• transform input argument instead of using return value.
• have side effects that are not implied by names.
• perform command and query at the same time.
• perform action and error handling at the same time.
CH3 Functions
Functions should not
• expose switch statements, which do multiple things inherently, to the high level.
• pass Boolean flag argument as it forces more than one behavior.
• transform input argument instead of using return value.
• have side effects that are not implied by names.
• perform command and query at the same time.
• perform action and error handling at the same time.
• have multiple entries or exits.
CH3 Functions

More Related Content

What's hot

Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenicsshelton88
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl languageMasahiro Honma
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 

What's hot (6)

Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Hw1 rubycalisthenics
Hw1 rubycalisthenicsHw1 rubycalisthenics
Hw1 rubycalisthenics
 
Types and perl language
Types and perl languageTypes and perl language
Types and perl language
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Regex posix
Regex posixRegex posix
Regex posix
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 

Similar to Clean Code - 2

Similar to Clean Code - 2 (20)

Programming fundamental 02.pptx
Programming fundamental 02.pptxProgramming fundamental 02.pptx
Programming fundamental 02.pptx
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Wordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean CodingWordcamp St. Louis - Clean Coding
Wordcamp St. Louis - Clean Coding
 
Week2
Week2Week2
Week2
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Calc i derivatives
Calc i derivativesCalc i derivatives
Calc i derivatives
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Clean Code
Clean CodeClean Code
Clean Code
 
Unison Language - Contact
Unison Language - ContactUnison Language - Contact
Unison Language - Contact
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
DIG1108C Lesson 5 Fall 2014
DIG1108C Lesson 5 Fall 2014DIG1108C Lesson 5 Fall 2014
DIG1108C Lesson 5 Fall 2014
 
Apple Swift API Design Guideline
Apple Swift API Design GuidelineApple Swift API Design Guideline
Apple Swift API Design Guideline
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
 
DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014DIG1108C Lesson 2 Fall 2014
DIG1108C Lesson 2 Fall 2014
 
CLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptxCLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptx
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 

More from Don Kim

Clean Code - 5
Clean Code - 5Clean Code - 5
Clean Code - 5Don Kim
 
Clean Code - 4
Clean Code - 4Clean Code - 4
Clean Code - 4Don Kim
 
Clean Code - 3
Clean Code - 3Clean Code - 3
Clean Code - 3Don Kim
 
Clean Code - 1
Clean Code - 1Clean Code - 1
Clean Code - 1Don Kim
 
Design and Analyze Secure Networked Systems - 7
Design and Analyze Secure Networked Systems - 7Design and Analyze Secure Networked Systems - 7
Design and Analyze Secure Networked Systems - 7Don Kim
 
Design and Analyze Secure Networked Systems - 6
Design and Analyze Secure Networked Systems - 6Design and Analyze Secure Networked Systems - 6
Design and Analyze Secure Networked Systems - 6Don Kim
 
Design and Analyze Secure Networked Systems - 5
Design and Analyze Secure Networked Systems - 5Design and Analyze Secure Networked Systems - 5
Design and Analyze Secure Networked Systems - 5Don Kim
 
Design and Analyze Secure Networked Systems - 4
Design and Analyze Secure Networked Systems - 4Design and Analyze Secure Networked Systems - 4
Design and Analyze Secure Networked Systems - 4Don Kim
 
Design and Analyze Secure Networked Systems - 3
Design and Analyze Secure Networked Systems - 3Design and Analyze Secure Networked Systems - 3
Design and Analyze Secure Networked Systems - 3Don Kim
 
Design and Analyze Secure Networked Systems - 2
Design and Analyze Secure Networked Systems - 2Design and Analyze Secure Networked Systems - 2
Design and Analyze Secure Networked Systems - 2Don Kim
 
Design and Analyze Secure Networked Systems - 1
Design and Analyze Secure Networked Systems - 1Design and Analyze Secure Networked Systems - 1
Design and Analyze Secure Networked Systems - 1Don Kim
 

More from Don Kim (11)

Clean Code - 5
Clean Code - 5Clean Code - 5
Clean Code - 5
 
Clean Code - 4
Clean Code - 4Clean Code - 4
Clean Code - 4
 
Clean Code - 3
Clean Code - 3Clean Code - 3
Clean Code - 3
 
Clean Code - 1
Clean Code - 1Clean Code - 1
Clean Code - 1
 
Design and Analyze Secure Networked Systems - 7
Design and Analyze Secure Networked Systems - 7Design and Analyze Secure Networked Systems - 7
Design and Analyze Secure Networked Systems - 7
 
Design and Analyze Secure Networked Systems - 6
Design and Analyze Secure Networked Systems - 6Design and Analyze Secure Networked Systems - 6
Design and Analyze Secure Networked Systems - 6
 
Design and Analyze Secure Networked Systems - 5
Design and Analyze Secure Networked Systems - 5Design and Analyze Secure Networked Systems - 5
Design and Analyze Secure Networked Systems - 5
 
Design and Analyze Secure Networked Systems - 4
Design and Analyze Secure Networked Systems - 4Design and Analyze Secure Networked Systems - 4
Design and Analyze Secure Networked Systems - 4
 
Design and Analyze Secure Networked Systems - 3
Design and Analyze Secure Networked Systems - 3Design and Analyze Secure Networked Systems - 3
Design and Analyze Secure Networked Systems - 3
 
Design and Analyze Secure Networked Systems - 2
Design and Analyze Secure Networked Systems - 2Design and Analyze Secure Networked Systems - 2
Design and Analyze Secure Networked Systems - 2
 
Design and Analyze Secure Networked Systems - 1
Design and Analyze Secure Networked Systems - 1Design and Analyze Secure Networked Systems - 1
Design and Analyze Secure Networked Systems - 1
 

Recently uploaded

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

Clean Code - 2

  • 1. Clean Code 2 A Handbook of Agile Software Craftsmanship Robert C. Martin Note by waegaein@github.com
  • 2. CH2 Meaningful Names Variable Names Function Names Class Names
  • 3. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. def sendtostore_packagestock0 → def calculate_stock_with_notifying_store
  • 4. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. • allow readers to understand difference without comment nor deduction. def sendtostore_packagestock0 → def calculate_stock_with_notifying_store
  • 5. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. • allow readers to understand difference without comment nor deduction. • include semantic words. def sendtostore_packagestock0 def sendtostore_packagestock1
  • 6. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. • allow readers to understand difference without comment nor deduction. • include semantic words. • include words that are technical or related to the problem domain. def calculate_rate → def calculate_surcharge
  • 7. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. • allow readers to understand difference without comment nor deduction. • include semantic words. • include words that are technical or related to the problem domain. • have context from its scope. Package_stock.package_stock_id → Package_stock.id
  • 8. CH2 Meaningful Names Names should • tell you why it exists, what it does, and how it is used. • allow readers to understand difference without comment nor deduction. • include semantic words. • include words that are technical or related to the problem domain. • have context from its scope. • consists of words. daydelay → days_delayed
  • 9. CH2 Meaningful Names Names should not • include obscure clues that may lead to false conclusions. def create_invoice_with_assurance → def create_invoice_with_retry
  • 10. CH2 Meaningful Names Names should not • include obscure clues that may lead to false conclusions. • use inconsistent synonyms for a single concept. (‘calculate_stock_with_notifying_store’ exists) def send_mail_to_branch → def send_mail_to_store
  • 11. CH2 Meaningful Names Names should not • include obscure clues that may lead to false conclusions. • use inconsistent synonyms for a single concept. • include syntactic encodings. v_count → count
  • 12. CH2 Meaningful Names Names should not • include obscure clues that may lead to false conclusions. • use inconsistent synonyms for a single concept. • include syntactic encodings. • consists of over-abbreviated letters. def get_psscharge → def get_package_stock_surchage
  • 13. CH2 Meaningful Names • Prefer clarity to smartness and entertainment. _, _ = select.select([], []) → discarded, discarded = select.select(list(), list())
  • 14. CH2 Meaningful Names • Prefer clarity to smartness and entertainment. • Class names are nouns or noun phrases. class SlackNotify → class SlackNotification
  • 15. CH2 Meaningful Names • Prefer clarity to smartness and entertainment. • Class names are nouns or noun phrases. • Method names are verbs or verb phrases. def surcharge_calculation → def calculate_surcharge
  • 16. CH3 Functions Functions should • be as small as possible.
  • 17. CH3 Functions Functions should • be as small as possible. • have the indent level of one or two.
  • 18. CH3 Functions Functions should • be as small as possible. • have the indent level of one or two. • do only one thing.
  • 19. CH3 Functions Functions should • be as small as possible. • have the indent level of one or two. • do only one thing. • include steps within one level of abstraction.
  • 20. CH3 Functions Functions should • be as small as possible. • have the indent level of one or two. • do only one thing. • include steps within one level of abstraction. • have two arguments at maximum, taking advantage of argument objects/lists. def send_mail_with_file(email_from, email_to, subject, html_content, attach_file_path, attach_file_name, mime_type) → def send_mail_with_file(base_email, attachment)
  • 21. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level.
  • 22. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level. • pass Boolean flag argument as it forces more than one behavior. calculate_surcharge(is_promotion=False, …) → calculate_surcharge, calcuate_surcharge_for_promotion
  • 23. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level. • pass Boolean flag argument as it forces more than one behavior. • transform input argument instead of using return value. … charge_list.sort() … return … charge_list_sorted = sorted(charge_list) … return charge_list_sorted →
  • 24. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level. • pass Boolean flag argument as it forces more than one behavior. • transform input argument instead of using return value. • have side effects that are not implied by names.
  • 25. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level. • pass Boolean flag argument as it forces more than one behavior. • transform input argument instead of using return value. • have side effects that are not implied by names. • perform command and query at the same time. • perform action and error handling at the same time.
  • 26. CH3 Functions Functions should not • expose switch statements, which do multiple things inherently, to the high level. • pass Boolean flag argument as it forces more than one behavior. • transform input argument instead of using return value. • have side effects that are not implied by names. • perform command and query at the same time. • perform action and error handling at the same time. • have multiple entries or exits.