SlideShare a Scribd company logo
Clean Code
‫نویسنده‬:Robert C. Martin
‫سال‬:2008
‫مطالب‬ ‫سرفصل‬
1.Meaningful Names
2.Functions
3.Comments
4.Formatting
5.Error Handling
6.Boundires
7.Unit Tests
8.Class
‫ی‬‫نامگذار‬ ‫قواعد‬:
1.‫معلوم‬ ‫های‬ ‫نام‬‫از‬‫استفاده‬
4.‫بیان‬‫قابل‬
3.‫گردد‬ ‫تمایز‬‫باعث‬ ‫گه‬ ‫ای‬ ‫کلمه‬ ‫از‬‫استفاده‬
5.‫باشد‬ ‫جستجو‬ ‫قابل‬‫سریع‬
8.‫مفهوم‬ ‫یک‬‫ای‬‫ر‬‫ب‬ ‫کلمه‬ ‫یک‬
6.‫پیشوند‬
7.‫ذهنی‬ ‫کلمات‬
2.‫غلط‬ ‫اطالعات‬ ‫از‬‫اجتناب‬
Soultion Domain Names 9.
‫توابع‬ ‫قاعده‬:
1.Small
2.Do One Thing
3.One Level Of Abstractions
4.Switch Case Statement
5.Use Descriptive Names
6.Function Arguments
8.Have No Side Effect
9.Don’t Repeat Yourself
10. Prefer Exceptions to Returning Error Codes
7. Argument Objects
Comments
‫بودند‬ ‫تر‬ ‫نزدیک‬ ‫انسان‬ ‫بان‬‫ز‬ ‫به‬ ‫ی‬ ‫نویس‬ ‫برنامه‬ ‫های‬ ‫بان‬‫ز‬ ‫اگر‬
‫نبود‬‫کامنت‬ ‫نوشتن‬ ‫به‬ ‫ی‬‫نیاز‬.
‫کند‬ ‫تغییر‬ ‫باید‬ ‫هم‬ ‫کامنت‬ ‫های‬ ‫نوشته‬ ،‫کند‬ ‫تغییر‬ ‫برنامه‬ ‫کد‬ ‫اگر‬.
‫نکته‬:
‫ل‬‫او‬ ‫قاعده‬:
1Comments Do Not Make Up for Bad Code
"Ooh,
I would better comment that!"
=> No! You would be better clean it!
‫ل‬‫او‬ ‫قاعده‬:
1Comments Do Not Make Up for Bad Code
‫ل‬‫او‬ ‫قاعده‬:
1Comments Do Not Make Up for Bad Code
‫دوم‬ ‫قاعده‬:
2Explain Yourself in Code
This:
// Check to see if the employee is eligible for full benefits
if (employee.flags && HOURLY_FLAG) && (employee.age >65)) {
vs:
// Check to see if the employee is eligible for full benefits
if (employee.isEligibleForFullBenefits()){
=> Create a function that says the same thing as the comment you want to write
‫سوم‬ ‫قاعده‬:
3Don’t Use a Comment When You Can Use a Function or a Variable
Consider the following stretch of code:
// does the module from the global list <mod> depend on the
// subsystem we are part of?
if (smodule.getDependSubsystems().contains(subSysMod.getSubSystem()))
This could be rephrased without the comment as
ArrayList moduleDependees = smodule.getDependSubsystems();
String ourSubSystem = subSysMod.getSubSystem();
if (moduleDependees.contains(ourSubSystem))
Good Comments
Legal Comments
Informative Comments
(Put all the terms and conditions into an external document.)
(Useful information about the implementation and taken decisions.)
TODO Comments
Bad Comments
Redundant Comments Less information than the code.
It is not easier to read than the code.
It is less precise
Noise Comments Auto generated or very obvious comments that are
nothing but noise.
Formatting
The Purpose of Formatting
Code formatting is about communication.
Readability of code.
The style and discipline survives to changes.
‫نمونه‬:
Type Formatting
Vertical Formatting
Horizontal Formatting
The Newspaper Metaphor
We would like a source file to be like a newspaper article:
At the top you expect a headline
The first paragraph gives you a synopsis of the whole story.
As you continue down-ward the details increase
In a source file: high-level concepts and algorithms => lowest level functions and details
1‫ل‬‫او‬‫قاعده‬:
Vertical Density ‫دوم‬ ‫قاعده‬:
2
Vertical Distance
Conceptual Affinity
Group of functions performing a similar operation. They share common naming scheme and perform
variations of the same task. We want them to be close together.
Variables should be declared as close to their usage as possible Our functions are short -> local
variables at the top. Control variables for loops => declare them within the loop statement
Instance variables
Should be declared at the top of the class. Everybody should know where to go to see the declarations.
Dependent functions
If one functions calls another they should be vertically close, an the caller should be above the calle, if it is
possible.
‫سوم‬ ‫قاعده‬:
3
Team Rules
Every programmer has his own favorite formatting rules,
but if he works in a team, them the team rules
‫نمونه‬:
Error Handling
Use Exceptions Rather Than Return Codes
It is better to throw an exception.
The calling code is cleaner.
Its logic is not obscured by error handling.
Try to separate the algorithm from error handling:
public void sendShutDown() {
try {
tryToShutDown();
} catch (DeviceShutDownError e) {
logger.log(e);
}
private void tryToShutDown() throws DeviceShutDownError {
// ..
}
‫ل‬‫او‬‫قاعده‬:
1
Don’t Return Null
If you return null then you have to manage "the null" with if's...".
When we return null we are creating work for ourselves.
You can return an empty list and clean up the code:
2‫دوم‬ ‫قاعده‬:
List<Employee> employees = getEmployees();
if (employees != null) {
for(Employee e : employees) {
totalPay += e.getPay();
}
}
vs
List<Employee> employees = getEmployees();
for(Employee e : employees) {
totalPay += e.getPay();
}
//...
public List<Employee> getEmployees() {
if( .. there are no employees .. )
return Collections.emptyList();
}
Don’t Pass Null
Returning null from methods is bad, but passing null into methods is worse
3‫سوم‬ ‫قاعده‬:
‫مطالب‬‫خالصه‬:
Formatting
Error HandlingComments
Comments Do Not Make Up for Bad Code
Explain Yourself in Code
Don’t Use a Comment When You Can Use a Function or a Variable
The Newspaper Metaphor
Use Exceptions Rather Than Return Codes
Don’t Pass Null
Don’t Return Null
Vertical Density
Vertical Distance
Vertical Formatting
Horizontal Formatting

More Related Content

What's hot

Regular Expression Injection
Regular Expression InjectionRegular Expression Injection
Regular Expression Injection
NSConclave
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
pragya ratan
 
Looping statements
Looping statementsLooping statements
Looping statements
Jaya Kumari
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
Srinivas Narasegouda
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Rakesh Roshan
 
Decision statements
Decision statementsDecision statements
Decision statements
Jaya Kumari
 
Loop control statements
Loop control statementsLoop control statements
Loop control statements
Jaya Kumari
 
Data structures vb
Data structures vbData structures vb
Data structures vb
nicky_walters
 

What's hot (9)

Regular Expression Injection
Regular Expression InjectionRegular Expression Injection
Regular Expression Injection
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 
Looping statements
Looping statementsLooping statements
Looping statements
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Control structures functions and modules in python programming
Control structures functions and modules in python programmingControl structures functions and modules in python programming
Control structures functions and modules in python programming
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Decision statements
Decision statementsDecision statements
Decision statements
 
Loop control statements
Loop control statementsLoop control statements
Loop control statements
 
Data structures vb
Data structures vbData structures vb
Data structures vb
 

Similar to Clean code ch03

C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
Eyob Lube
 
Clean code quotes - Citações e provocações
Clean code quotes - Citações e provocaçõesClean code quotes - Citações e provocações
Clean code quotes - Citações e provocações
André de Fontana Ignacio
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
Knoldus Inc.
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
sotlsoc
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
wasntgosu
 
The dependency inversion principle
The dependency inversion principleThe dependency inversion principle
The dependency inversion principle
navicorevn
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
gavhays
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
wasntgosu
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
wasntgosu
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
Perfomatix Solutions
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
Automating C# Coding Standards using StyleCop and FxCop
Automating C# Coding Standards using StyleCop and FxCopAutomating C# Coding Standards using StyleCop and FxCop
Automating C# Coding Standards using StyleCop and FxCop
BlackRabbitCoder
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
Anilkumar Patil
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
Lyndon White
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
Home
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
Igor Crvenov
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean Code
Luan Reffatti
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka
 
Coding Best Practices.docx
Coding Best Practices.docxCoding Best Practices.docx
Coding Best Practices.docx
ShitanshuKumar15
 
Clean Code
Clean CodeClean Code
Clean Code
Dmytro Turskyi
 

Similar to Clean code ch03 (20)

C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Clean code quotes - Citações e provocações
Clean code quotes - Citações e provocaçõesClean code quotes - Citações e provocações
Clean code quotes - Citações e provocações
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
The dependency inversion principle
The dependency inversion principleThe dependency inversion principle
The dependency inversion principle
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
 
Automating C# Coding Standards using StyleCop and FxCop
Automating C# Coding Standards using StyleCop and FxCopAutomating C# Coding Standards using StyleCop and FxCop
Automating C# Coding Standards using StyleCop and FxCop
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean Code
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Coding Best Practices.docx
Coding Best Practices.docxCoding Best Practices.docx
Coding Best Practices.docx
 
Clean Code
Clean CodeClean Code
Clean Code
 

Recently uploaded

TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
aeeva
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
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
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
sandeepmenon62
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
alowpalsadig
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
seospiralmantra
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 

Recently uploaded (20)

TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
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
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
Upturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in NashikUpturn India Technologies - Web development company in Nashik
Upturn India Technologies - Web development company in Nashik
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 

Clean code ch03

  • 1.
  • 2. Clean Code ‫نویسنده‬:Robert C. Martin ‫سال‬:2008
  • 4. ‫ی‬‫نامگذار‬ ‫قواعد‬: 1.‫معلوم‬ ‫های‬ ‫نام‬‫از‬‫استفاده‬ 4.‫بیان‬‫قابل‬ 3.‫گردد‬ ‫تمایز‬‫باعث‬ ‫گه‬ ‫ای‬ ‫کلمه‬ ‫از‬‫استفاده‬ 5.‫باشد‬ ‫جستجو‬ ‫قابل‬‫سریع‬ 8.‫مفهوم‬ ‫یک‬‫ای‬‫ر‬‫ب‬ ‫کلمه‬ ‫یک‬ 6.‫پیشوند‬ 7.‫ذهنی‬ ‫کلمات‬ 2.‫غلط‬ ‫اطالعات‬ ‫از‬‫اجتناب‬ Soultion Domain Names 9.
  • 5. ‫توابع‬ ‫قاعده‬: 1.Small 2.Do One Thing 3.One Level Of Abstractions 4.Switch Case Statement 5.Use Descriptive Names 6.Function Arguments 8.Have No Side Effect 9.Don’t Repeat Yourself 10. Prefer Exceptions to Returning Error Codes 7. Argument Objects
  • 7. ‫بودند‬ ‫تر‬ ‫نزدیک‬ ‫انسان‬ ‫بان‬‫ز‬ ‫به‬ ‫ی‬ ‫نویس‬ ‫برنامه‬ ‫های‬ ‫بان‬‫ز‬ ‫اگر‬ ‫نبود‬‫کامنت‬ ‫نوشتن‬ ‫به‬ ‫ی‬‫نیاز‬. ‫کند‬ ‫تغییر‬ ‫باید‬ ‫هم‬ ‫کامنت‬ ‫های‬ ‫نوشته‬ ،‫کند‬ ‫تغییر‬ ‫برنامه‬ ‫کد‬ ‫اگر‬. ‫نکته‬:
  • 8. ‫ل‬‫او‬ ‫قاعده‬: 1Comments Do Not Make Up for Bad Code "Ooh, I would better comment that!" => No! You would be better clean it!
  • 11. ‫دوم‬ ‫قاعده‬: 2Explain Yourself in Code This: // Check to see if the employee is eligible for full benefits if (employee.flags && HOURLY_FLAG) && (employee.age >65)) { vs: // Check to see if the employee is eligible for full benefits if (employee.isEligibleForFullBenefits()){ => Create a function that says the same thing as the comment you want to write
  • 12. ‫سوم‬ ‫قاعده‬: 3Don’t Use a Comment When You Can Use a Function or a Variable Consider the following stretch of code: // does the module from the global list <mod> depend on the // subsystem we are part of? if (smodule.getDependSubsystems().contains(subSysMod.getSubSystem())) This could be rephrased without the comment as ArrayList moduleDependees = smodule.getDependSubsystems(); String ourSubSystem = subSysMod.getSubSystem(); if (moduleDependees.contains(ourSubSystem))
  • 13. Good Comments Legal Comments Informative Comments (Put all the terms and conditions into an external document.) (Useful information about the implementation and taken decisions.) TODO Comments
  • 14. Bad Comments Redundant Comments Less information than the code. It is not easier to read than the code. It is less precise Noise Comments Auto generated or very obvious comments that are nothing but noise.
  • 16. The Purpose of Formatting Code formatting is about communication. Readability of code. The style and discipline survives to changes.
  • 19. The Newspaper Metaphor We would like a source file to be like a newspaper article: At the top you expect a headline The first paragraph gives you a synopsis of the whole story. As you continue down-ward the details increase In a source file: high-level concepts and algorithms => lowest level functions and details 1‫ل‬‫او‬‫قاعده‬:
  • 20. Vertical Density ‫دوم‬ ‫قاعده‬: 2
  • 21. Vertical Distance Conceptual Affinity Group of functions performing a similar operation. They share common naming scheme and perform variations of the same task. We want them to be close together. Variables should be declared as close to their usage as possible Our functions are short -> local variables at the top. Control variables for loops => declare them within the loop statement Instance variables Should be declared at the top of the class. Everybody should know where to go to see the declarations. Dependent functions If one functions calls another they should be vertically close, an the caller should be above the calle, if it is possible. ‫سوم‬ ‫قاعده‬: 3
  • 22. Team Rules Every programmer has his own favorite formatting rules, but if he works in a team, them the team rules
  • 25. Use Exceptions Rather Than Return Codes It is better to throw an exception. The calling code is cleaner. Its logic is not obscured by error handling. Try to separate the algorithm from error handling: public void sendShutDown() { try { tryToShutDown(); } catch (DeviceShutDownError e) { logger.log(e); } private void tryToShutDown() throws DeviceShutDownError { // .. } ‫ل‬‫او‬‫قاعده‬: 1
  • 26. Don’t Return Null If you return null then you have to manage "the null" with if's...". When we return null we are creating work for ourselves. You can return an empty list and clean up the code: 2‫دوم‬ ‫قاعده‬: List<Employee> employees = getEmployees(); if (employees != null) { for(Employee e : employees) { totalPay += e.getPay(); } } vs List<Employee> employees = getEmployees(); for(Employee e : employees) { totalPay += e.getPay(); } //... public List<Employee> getEmployees() { if( .. there are no employees .. ) return Collections.emptyList(); }
  • 27. Don’t Pass Null Returning null from methods is bad, but passing null into methods is worse 3‫سوم‬ ‫قاعده‬:
  • 28. ‫مطالب‬‫خالصه‬: Formatting Error HandlingComments Comments Do Not Make Up for Bad Code Explain Yourself in Code Don’t Use a Comment When You Can Use a Function or a Variable The Newspaper Metaphor Use Exceptions Rather Than Return Codes Don’t Pass Null Don’t Return Null Vertical Density Vertical Distance Vertical Formatting Horizontal Formatting