SlideShare a Scribd company logo
Clean code ch03
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

PPT
Oop interfaces
PPTX
Learning php 7
PPTX
How to Use Constraint and SQL Constraint in Odoo 15
PPTX
Programming style guildelines
PPT
Codings Standards
PDF
Introduction to Preprocessors
PPS
Commenting Best Practices
PPTX
Using Rhino Mocks for Effective Unit Testing
Oop interfaces
Learning php 7
How to Use Constraint and SQL Constraint in Odoo 15
Programming style guildelines
Codings Standards
Introduction to Preprocessors
Commenting Best Practices
Using Rhino Mocks for Effective Unit Testing

What's hot (9)

PDF
Regular Expression Injection
PPTX
Vb decision making statements
PPTX
Looping statements
PPTX
C# conditional branching statement
PDF
Control structures functions and modules in python programming
PPT
C language UPTU Unit3 Slides
PPTX
Decision statements
PPTX
Loop control statements
PPTX
Data structures vb
Regular Expression Injection
Vb decision making statements
Looping statements
C# conditional branching statement
Control structures functions and modules in python programming
C language UPTU Unit3 Slides
Decision statements
Loop control statements
Data structures vb
Ad

Similar to Clean code ch03 (20)

PPTX
Clean code ch02
PPTX
Clean Code Principles / How to write a clean code.
ODT
Android Open source coading guidel ine
PPTX
7-clean-code
PDF
Php Egypt Jan14
ODP
Clean Code - Part 2
PPTX
Best-Practices-for-Writing-Clean-Code.Presentation
PPTX
Clean code - DSC DYPCOE
PPTX
Clean code
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PPTX
Code reviews
PPTX
Code Restructuring in Software Engineering.pptx
PDF
Android coding standard
PDF
Writing clean code with modern Java
PPTX
Clean code quotes - Citações e provocações
PDF
Writing code for people
PDF
Clean Code
PPTX
What is Python?
PPTX
Clean code
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean code ch02
Clean Code Principles / How to write a clean code.
Android Open source coading guidel ine
7-clean-code
Php Egypt Jan14
Clean Code - Part 2
Best-Practices-for-Writing-Clean-Code.Presentation
Clean code - DSC DYPCOE
Clean code
Clean Code. An Agile Guide to Software Craft Kameron H.
Code reviews
Code Restructuring in Software Engineering.pptx
Android coding standard
Writing clean code with modern Java
Clean code quotes - Citações e provocações
Writing code for people
Clean Code
What is Python?
Clean code
Clean Code. An Agile Guide to Software Craft Kameron H.
Ad

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
System and Network Administration Chapter 2
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Transform Your Business with a Software ERP System
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
Which alternative to Crystal Reports is best for small or large businesses.pdf
System and Network Administration Chapter 2
How to Choose the Right IT Partner for Your Business in Malaysia
PTS Company Brochure 2025 (1).pdf.......
Online Work Permit System for Fast Permit Processing
Transform Your Business with a Software ERP System
How Creative Agencies Leverage Project Management Software.pdf
CHAPTER 2 - PM Management and IT Context
L1 - Introduction to python Backend.pptx
Operating system designcfffgfgggggggvggggggggg
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Softaken Excel to vCard Converter Software.pdf
Introduction to Artificial Intelligence
Design an Analysis of Algorithms II-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily
Nekopoi APK 2025 free lastest update

Clean code ch03

  • 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