SlideShare a Scribd company logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mai Abuthraa {
Software Developer @
DownHome Solutions
Clean Code
Workshop About Me
}
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4
Robert C. Martin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
What is Clean Code? {
}
Clean Code
Introduction
Clean code is code that is
easy to understand
and easy to change.
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Is ‘Craftsmanship’ {
}
Knowledge
○ Principles
○ Pattens
○ Practice
○ Heuristics ‫واالستكشافات‬ ‫االستدالالت‬
- solving problems
- our ability to identify a problem
Work
Programming Language
Introduction
7
‫اجادة‬ ‫أو‬ ‫حرفة‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Introduction
8
80s;
‫لكن‬ ‫متفوقة‬ ‫كانت‬ ‫البداية‬ ،‫برمجيات‬ ‫شركة‬
‫لإلنقاذ‬ ‫محاولة‬ ‫وأي‬ ،‫خسرت‬ ‫الوقت‬ ‫مع‬
‫الشركة‬ ‫تكلف‬ ‫كانت‬
‫لفكرة‬ ‫بحاجة‬ ‫كنت‬ ‫هل‬ ‫؟‬ ‫عائق‬ ‫كان‬ ‫الكود‬ ‫هل‬
‫المشكلة؟‬ ‫لحل‬ ‫تلميح‬ ‫أو‬
‫مستعجلة؟‬ ‫الشركة‬ ‫إدارة‬ ‫او‬, ‫مستعجل‬ ‫الفريق‬ ‫مدير‬ ‫هل‬
‫الكود؟‬ ‫تحسين‬ ‫يؤجل‬ ‫الفريق‬ ‫مدير‬ ‫كان‬ ‫هل‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Introduction
9
The Total Cost of Owning a Mess
,‫بسرعة‬ ‫العمل‬ ‫يبدأ‬ ‫الذي‬ ‫الفريق‬
‫اي‬ ,‫إنتاجية‬ ‫ستقل‬ ‫الوقت‬ ‫مع‬
‫هي‬ ‫الكود‬ ‫على‬ ‫اضافة‬ ‫او‬ ‫تعديل‬
‫ويجب‬, ‫متشابكة‬ ‫عقدة‬ ‫عن‬ ‫عبارة‬
‫العقدة‬ ‫أو‬,‫جيد‬ ‫بشكل‬ ‫فهمها‬
‫إصالحها‬ ‫يمكن‬ ‫ال‬ ‫الى‬ ‫تكبر‬ ‫ستبقى‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Introduction
10
:‫المدراء‬ ‫قرار‬
-
.‫المبيعات‬ ‫او‬ ‫اكثر‬ ‫خدمات‬ ‫زيادة‬
-
.‫الحقيقية‬ ‫للمشكلة‬ ‫تجاهلهم‬ ‫أو‬ ‫معرفتهم‬ ‫عدم‬ ‫مع‬
:‫النتيجة‬
-
.‫إنتاجية‬ ‫ال‬
-
.‫الفريق‬ ‫على‬ ‫ضغط‬
-
.‫المشاكل‬ ‫من‬ ‫دائرة‬ ‫دخول‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Introduction
11
:‫االدارة‬ ‫تقرر‬ ‫أو‬
-
‫جديد‬ ‫فريق‬ ‫انشاء‬
Tiger team
‫من‬ ‫المشروع‬ ‫لبدء‬ ,‫الشركة‬ ‫في‬ ‫الفريق‬ ‫اعضاء‬ ‫افضل‬
.‫الشركة‬ ‫إلنقاذ‬ ‫محاولة‬ .‫الصفر‬
-
‫البرامج‬ ‫إلبقاء‬ ,‫الوقت‬ ‫كسب‬ ‫يحاول‬ ‫قديم‬ ‫فريق‬ ‫و‬
.‫تعمل‬ ‫الحالية‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Introduction
12
‫ان‬ , ‫الكاتب‬ ‫رأي‬ ‫حسب‬ ‫الوحيد‬ ‫الحل‬
.‫الكود‬ ‫جودة‬ ‫على‬ ‫نحافظ‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
— Bjarne Stroutrnp inventor of C++
I like my code to be elegant and efficient. The logic should be
straightforward and make it hard for bugs to hide, the
dependencies minimal to ease maintenance, error handling
complete according to an articulated strategy, and performance
close to optimal so as not to tempt people to make the code
messy with unprincipled optimizations. Clean code does one
thing well.
Programming Language
Quotes
13
.‫بسرعة‬ ‫المشكلة‬ ‫اكتشاف‬ = ‫واضح‬ ‫بسيط‬ ,‫وصريح‬ , ‫فعال‬ , ‫انيق‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
— Grady Booch author of Object-Oriented Analysis and
Design with Applications:
Clean code is simple and direct. Clean code reads like well-written
prose. Clean code never obscures the designers’ intent but rather
is full of crisp abstractions and straightforward lines of control.
Programming Language
Quotes
14
.‫مقال‬ ‫تقرأ‬ ‫كانك‬, ‫قراءة‬ ‫ويمكن‬ ‫مباشر‬ ‫و‬ ‫بسيط‬
‫مبسوط‬ ‫وانت‬ ‫الكود‬ ‫بتقرأ‬ ‫يعني‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
— “Big” Dave Thomas Founder of OTI and godfather of the
Eclipse strategy
Clean code can be read, and enhanced by a developer other than its original
author. It has unit and acceptance tests. It has meaningful names. It
provides one way rather than many ways for doing one thing. It has minimal
dependencies, which are explicitly defined, and provides a clear and minimal
API. Code should be literate since, depending on the language, not all
necessary information can be expressed clearly in code alone.
Programming Language
Quotes
15
.‫بسهولة‬ ‫الكود‬ ‫يقرأ‬ ‫ممكن‬ ‫الكود‬ ‫كتب‬ ‫غيرك‬ ‫شخص‬ ‫أي‬
Unit tests
.‫الكود‬ ‫في‬ ‫أساسي‬ ‫شي‬ ‫يكون‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
— Michael Feathers author of Working Effectively with Legacy
Code
I could list all of the qualities that I notice in clean code, but there is one
overarching quality that leads to all of them.Clean code always
looks like it was written by someone who cares. There
is nothing obvious that you can do to make it better. All of those things were
thought about by the code’s author, and if you try to imagine improvements,
you are led back to where you are, sitting in appreciation of the code
someone left for you—code written by someone who cared deeply about the
craft.
Programming Language
Quotes
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Meaningful Names
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Meaningful Names
While writing the code, we
name many things:
-Classes -Models
-Variable -Arguments
-Functions
18
So, We have to do it the right way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
— Uncle Bob's ‘author’
“The name of a variable, function, or class, should
answer all the big questions.
It should tell you why it exists, what it does, and how it is
used.
If a name requires a comment, then the name does not
reveal its intent.”
Clean Code
Meaningful Names Quotes
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
01 {
[Use Intention-Revealing Names]
< It is easy to say that names should reveal intent. What we want to impress upon you
is that we are serious about this. Choosing good names takes time but
saves more than it takes. So take care with your names >
}
Clean Code
Meaningful Names
20
‫صريحة‬ ‫النية‬ ‫تكشف‬ ‫التي‬ ‫األسماء‬ ‫استخدم‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
< /Good >
< /Bad >
Clean Code
Meaningful Names Example
21
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
int d; // elapsed time in days
int s; // elapsed time in seconds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
02 {
[Avoid Disinformation]
- Don’t use names that are already used as commands by
the operating system.
- Don't use the Container type:
Don't refer to a group of accounts as "accountList,"
whereas it is actually not a List. Instead, name it
"accountGroup," "accounts," or "bunchOfAccounts."
Clean Code
Meaningful Names
22
‫المضللة‬ ‫المعلومات‬ ‫تجنب‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Meaningful Names Example
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
03 {
[Make Meaningful Distinctions]
< If there are two different things in the same scope, you might
be tempted to change one name in an arbitrary way.>
}
Clean Code
Meaningful Names
24
‫مغزى‬ ‫ذات‬ ‫تمييزات‬ ‫بعمل‬ ‫قم‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Meaningful Names Example
25
String custs;
String customers;
class ProductInfo
class ProductData
What is stored in cust, which is
different from customer?
How are these classes
different?
How do you differentiate between these
methods in the same class?
void getActiveAccount()
void getActiveAccounts()
void getActiveAccountsInfo()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
04 {
[Use Pronounceable Names]
< Ensure that names are pronounceable. Nobody wants
a tongue twister.>
}
Clean Code
Meaningful Names
26
‫نطقها‬ ‫يمكن‬ ‫التي‬ ‫األسماء‬ ‫استخدم‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
<Bad />
Clean Code
Meaningful Names Example
27
Date genymdhms;
Date modymdhms;
Int pszqint;
Date GenerationTimeStamp;
Date modificationTimestamp;
Int PublicAccessCount;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
05 { [Use Searchable Names]
- Avoid single letter names and numeric constants, if
they are not easy to locate across the body of the text.
- If a variable or constant might be seen or used in
multiple places in a body of code, it is imperative to give
it a search-friendly name.
}
Clean Code
Meaningful Names
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
<Bad />
Clean Code
Meaningful Names Example
29
for (int j = 0; j < 34; j++) {
s += (t[j] * 4) / 5;
}
int realDaysPerIdealDay = 4;
const int WORK_DAYS_PER_WEEK = 5;
int sum = 0;
for (int j = 0; j < NUMBER_OF_TASKS; j++) {
int realTaskDays = taskEstimate[j] * realDaysPerIdealDay;
int realTaskWeeks = (realTaskDays / WORK_DAYS_PER_WEEK);
sum += realTaskWeeks;
}
‫النهم‬ ‫ممكن‬
local
variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
06 { [Avoid Encodings]
}
Clean Code
Meaningful Names
30
< Hungarian Notation and other forms of type
encoding are simply impediments today, because when
we change the type, we also have to change the name.
They make the code also so noisier that we cannot
easily read the meaningful part of the code. And they
confuses the reader.>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- Avoid unnecessary encodings of datatypes along with the
variable name.
Clean Code
Meaningful Names
31
String nameString;
Float salaryFloat;
with a new compiler no need to do it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
06{
[Avoid Mental Mapping]
}
Clean Code
Meaningful Names
32
‫تتفلسف‬ ‫ال‬
<If scope of code is very small and no other names
can conflict with it, loop counters can be named as i,
j, k because they are familiar to programmers.>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Cont. 6{
[Avoid Mental Mapping]
}
Clean Code
Meaningful Names
33
One difference between a smart programmer
and a professional programmer is that the
professional understands that clarity is king
1
2
3
4
5
6
7
8
9
10
11
12
13
14
07{ [Class Names]
}
Clean Code
Meaningful Names
34
< Classes and objects should have noun or
noun phrase names and should obey Single
Responsibility Principle. It is always better to
avoid noise words.>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
08{ [Method Names​
]
}
Clean Code
Meaningful Names
35
< Methods should have verb or verb phrase
names like postPayment, deletePage, or
save.>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
09{ [Don’t Be Cute​
]
}
Clean Code
Meaningful Names
36
< Using slang language or jokes in code just to show
we are clever or just to have fun can hide the
intention of author or even mislead readers and
thus we should avoid.>
‫دمك‬ ‫تخفف‬ ‫ما‬
1
2
3
4
5
6
7
8
9
10
11
12
13
14
10 { [Pick One Word per Concept​
​
]
}
Clean Code
Meaningful Names
37
< Avoiding the use of similar names for equivalent
methods in codebase can make the code cleaner.
For instance having fetch, retrieve, get for similar
concepts or having controller, manager in the same
codebase can be confusing for readers.>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
11{ [Don’t Pun]
}
Clean Code
Meaningful Names
38
< Sometimes when we try to follow the “one word
per concept” rule, we might me tempted to use
same names even though they don’t have really
same semantics or concept.>
‫معنى‬ ‫من‬ ‫اكثر‬ ‫تحمل‬ ‫التي‬ ‫الكلمات‬ ‫تجنب‬
‫تورية‬
Add append insert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
12{ [Use Solution Domain Names​
]
}
Clean Code
Meaningful Names
39
< Use computer science (CS) terms, algorithm names,
pattern names, math terms, and so forth because
readers and writers will most likely be developers and
they won’t be familiar with problem domain names>
jobQueue
Demons homeBuilder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
13{ [Use Problem Domain Names​
]
}
Clean Code
Meaningful Names
40
● When there is no general solution domain name for what you're
doing, use the name from the problem domain.
● Separating solution and problem domain concepts is part of the
job of a good programmer.
It is not good to always use a problem domain name because they may
have to ask others what it means.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
14{ [Add Meaningful Context]
}
Clean Code
Meaningful Names
41
Names themselves mostly doesn’t give a meaning
to us, thus contexts(can be well-named classes,
functions or even prefixing the name) in names
help us to understand better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Clean Code
Meaningful Names Example
42
firstName
lastName
Street
houseNumber
City
State
zipcod
User
firstName
lastName
State
User
firstName
lastName
addressState
User
firstName
lastName
User
userId
Street
houseNumber
City
State
zipcod
Address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Functions
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1{ Function arguments (2 or fewer ideally)
}
Clean Code
Functions
44
One or two arguments is the ideal case, and three should be
avoided if possible.
Anything more than that should be consolidated. Usually, if
you have more than two arguments then your function is
trying to do too much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
<Bad />
Clean Code
Functions Example
45
function createMenu(title, body, buttonText, cancellable) {
// ...
}
createMenu("Foo", "Bar", "Baz", true);
function createMenu({ title, body, buttonText, cancellable }) {
// ...
}
createMenu({
title: "Foo",
body: "Bar",
buttonText: "Baz",
cancellable: true
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2{ Functions should do one thing
}
Clean Code
Functions
46
This is by far the most important rule in software
engineering.
When functions do more than one thing, they are harder to
compose, test, and reason about. When you can isolate a
function to just one action, it can be refactored easily and
your code will read much cleaner.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
<Bad />
Clean Code
Functions Example
47
function emailClients(clients) {
clients.forEach(client => {
const clientRecord = database.lookup(client);
if (clientRecord.isActive()) {
email(client);
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
Clean Code
Functions Example
48
function getActiveClientsEmails(clients) {
clients.filter(isActiveClient).forEach(email);
}
function isActiveClient(client) {
const clientRecord = database.lookup(client);
return clientRecord.isActive();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3{ Function names should say what they do
Clean Code
Functions
49
<Bad />
function addToDate(date, month) {
// ...
}
const date = new Date();
// It's hard to tell from the function name what is added
addToDate(date, 1);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
Clean Code
Functions Example
50
function addMonthToDate(month, date) {
// ...
}
const date = new Date();
addMonthToDate(1, date);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4{ Set default objects
Clean Code
Functions
51
<Bad />
<Good />
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5{ Don't use flags as function parameters
}
Clean Code
Functions
52
Flags tell your user that this function does more than one
thing.
Functions should do one thing.
Split out your functions if they are following different code
paths based on a boolean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<Good />
<Bad />
Clean Code
Functions Example
53
function createFile(name, temp) {
if (temp) {
fs.create(`./temp/${name}`);
} else {
fs.create(name);
}
}
function createFile(name) {
fs.create(name);
}
function createTempFile(name) {
createFile(`./temp/${name}`);
}
temp ? createTempFile(name) : createFile(name)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
The References {
∗ Clean Code: A Handbook of Agile Software Craftsmanship.
∗ Refactoring.Guru.
∗ Naming Conventions From Uncle Bob's Clean Code Philosophy.
∗ Clean Code - Uncle Bob - all lessons
∗ Clean Code / Javascript
Here are some references that helped me to prepare the presentation:
Clean Code
Workshop References
}
54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
The References {
∗ What Is Clean Code 🔗 Naming Conventions / Part 1.
∗ What Is Clean Code 🔗 Naming Conventions / Part 2.
∗ Naming Classes - Why It Matters, and How to Do It Well.
∗ The Total Cost of Owning a Mess
∗ Meaningful Names in Ruby Code
Here are some references that helped me to prepare the presentation:
Clean Code
Workshop References
}
55

More Related Content

PPTX
7-clean-code
PPTX
Writing Clean Code
PPTX
Best-Practices-for-Writing-Clean-Code.Presentation
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PPTX
Clean code - DSC DYPCOE
PPSX
Clean code
PPTX
Clean code
7-clean-code
Writing Clean Code
Best-Practices-for-Writing-Clean-Code.Presentation
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean code - DSC DYPCOE
Clean code
Clean code

Similar to Clean Code Principles / How to write a clean code. (20)

PPTX
Coding Checkpoints
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PDF
Clean code and code smells
PDF
Clean Code
PDF
PDF
Naming Things (with notes)
PPTX
Clean Code - Writing code for human
PPTX
Clean_Code
PDF
UNIT I cloud computing ppt cloud ccd all about the cloud computing
PPTX
Best-Practices-in-Writing-Clean-Maintainable-Code
PPTX
Clean Code Software Engineering
PPTX
Clean Code
PPTX
Clean Code Pt I
PPT
Clean code
PPTX
C# coding standards, good programming principles & refactoring
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
PDF
WordCamp Nashville: Clean Code for WordPress
PDF
Naming guidelines for professional programmers
PDF
Php Egypt Jan14
Coding Checkpoints
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean code and code smells
Clean Code
Naming Things (with notes)
Clean Code - Writing code for human
Clean_Code
UNIT I cloud computing ppt cloud ccd all about the cloud computing
Best-Practices-in-Writing-Clean-Maintainable-Code
Clean Code Software Engineering
Clean Code
Clean Code Pt I
Clean code
C# coding standards, good programming principles & refactoring
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
WordCamp Nashville: Clean Code for WordPress
Naming guidelines for professional programmers
Php Egypt Jan14
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Programs and apps: productivity, graphics, security and other tools
Dropbox Q2 2025 Financial Results & Investor Presentation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Ad

Clean Code Principles / How to write a clean code.

  • 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Mai Abuthraa { Software Developer @ DownHome Solutions Clean Code Workshop About Me } 1
  • 5. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 What is Clean Code? { } Clean Code Introduction Clean code is code that is easy to understand and easy to change. 5
  • 7. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Is ‘Craftsmanship’ { } Knowledge ○ Principles ○ Pattens ○ Practice ○ Heuristics ‫واالستكشافات‬ ‫االستدالالت‬ - solving problems - our ability to identify a problem Work Programming Language Introduction 7 ‫اجادة‬ ‫أو‬ ‫حرفة‬
  • 8. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Introduction 8 80s; ‫لكن‬ ‫متفوقة‬ ‫كانت‬ ‫البداية‬ ،‫برمجيات‬ ‫شركة‬ ‫لإلنقاذ‬ ‫محاولة‬ ‫وأي‬ ،‫خسرت‬ ‫الوقت‬ ‫مع‬ ‫الشركة‬ ‫تكلف‬ ‫كانت‬ ‫لفكرة‬ ‫بحاجة‬ ‫كنت‬ ‫هل‬ ‫؟‬ ‫عائق‬ ‫كان‬ ‫الكود‬ ‫هل‬ ‫المشكلة؟‬ ‫لحل‬ ‫تلميح‬ ‫أو‬ ‫مستعجلة؟‬ ‫الشركة‬ ‫إدارة‬ ‫او‬, ‫مستعجل‬ ‫الفريق‬ ‫مدير‬ ‫هل‬ ‫الكود؟‬ ‫تحسين‬ ‫يؤجل‬ ‫الفريق‬ ‫مدير‬ ‫كان‬ ‫هل‬
  • 9. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Introduction 9 The Total Cost of Owning a Mess ,‫بسرعة‬ ‫العمل‬ ‫يبدأ‬ ‫الذي‬ ‫الفريق‬ ‫اي‬ ,‫إنتاجية‬ ‫ستقل‬ ‫الوقت‬ ‫مع‬ ‫هي‬ ‫الكود‬ ‫على‬ ‫اضافة‬ ‫او‬ ‫تعديل‬ ‫ويجب‬, ‫متشابكة‬ ‫عقدة‬ ‫عن‬ ‫عبارة‬ ‫العقدة‬ ‫أو‬,‫جيد‬ ‫بشكل‬ ‫فهمها‬ ‫إصالحها‬ ‫يمكن‬ ‫ال‬ ‫الى‬ ‫تكبر‬ ‫ستبقى‬
  • 10. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Introduction 10 :‫المدراء‬ ‫قرار‬ - .‫المبيعات‬ ‫او‬ ‫اكثر‬ ‫خدمات‬ ‫زيادة‬ - .‫الحقيقية‬ ‫للمشكلة‬ ‫تجاهلهم‬ ‫أو‬ ‫معرفتهم‬ ‫عدم‬ ‫مع‬ :‫النتيجة‬ - .‫إنتاجية‬ ‫ال‬ - .‫الفريق‬ ‫على‬ ‫ضغط‬ - .‫المشاكل‬ ‫من‬ ‫دائرة‬ ‫دخول‬
  • 11. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Introduction 11 :‫االدارة‬ ‫تقرر‬ ‫أو‬ - ‫جديد‬ ‫فريق‬ ‫انشاء‬ Tiger team ‫من‬ ‫المشروع‬ ‫لبدء‬ ,‫الشركة‬ ‫في‬ ‫الفريق‬ ‫اعضاء‬ ‫افضل‬ .‫الشركة‬ ‫إلنقاذ‬ ‫محاولة‬ .‫الصفر‬ - ‫البرامج‬ ‫إلبقاء‬ ,‫الوقت‬ ‫كسب‬ ‫يحاول‬ ‫قديم‬ ‫فريق‬ ‫و‬ .‫تعمل‬ ‫الحالية‬
  • 12. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Introduction 12 ‫ان‬ , ‫الكاتب‬ ‫رأي‬ ‫حسب‬ ‫الوحيد‬ ‫الحل‬ .‫الكود‬ ‫جودة‬ ‫على‬ ‫نحافظ‬
  • 13. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 — Bjarne Stroutrnp inventor of C++ I like my code to be elegant and efficient. The logic should be straightforward and make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well. Programming Language Quotes 13 .‫بسرعة‬ ‫المشكلة‬ ‫اكتشاف‬ = ‫واضح‬ ‫بسيط‬ ,‫وصريح‬ , ‫فعال‬ , ‫انيق‬
  • 14. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 — Grady Booch author of Object-Oriented Analysis and Design with Applications: Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designers’ intent but rather is full of crisp abstractions and straightforward lines of control. Programming Language Quotes 14 .‫مقال‬ ‫تقرأ‬ ‫كانك‬, ‫قراءة‬ ‫ويمكن‬ ‫مباشر‬ ‫و‬ ‫بسيط‬ ‫مبسوط‬ ‫وانت‬ ‫الكود‬ ‫بتقرأ‬ ‫يعني‬
  • 15. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 — “Big” Dave Thomas Founder of OTI and godfather of the Eclipse strategy Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since, depending on the language, not all necessary information can be expressed clearly in code alone. Programming Language Quotes 15 .‫بسهولة‬ ‫الكود‬ ‫يقرأ‬ ‫ممكن‬ ‫الكود‬ ‫كتب‬ ‫غيرك‬ ‫شخص‬ ‫أي‬ Unit tests .‫الكود‬ ‫في‬ ‫أساسي‬ ‫شي‬ ‫يكون‬
  • 16. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 — Michael Feathers author of Working Effectively with Legacy Code I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads to all of them.Clean code always looks like it was written by someone who cares. There is nothing obvious that you can do to make it better. All of those things were thought about by the code’s author, and if you try to imagine improvements, you are led back to where you are, sitting in appreciation of the code someone left for you—code written by someone who cared deeply about the craft. Programming Language Quotes 16
  • 18. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Meaningful Names While writing the code, we name many things: -Classes -Models -Variable -Arguments -Functions 18 So, We have to do it the right way
  • 19. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 — Uncle Bob's ‘author’ “The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.” Clean Code Meaningful Names Quotes 19
  • 20. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 01 { [Use Intention-Revealing Names] < It is easy to say that names should reveal intent. What we want to impress upon you is that we are serious about this. Choosing good names takes time but saves more than it takes. So take care with your names > } Clean Code Meaningful Names 20 ‫صريحة‬ ‫النية‬ ‫تكشف‬ ‫التي‬ ‫األسماء‬ ‫استخدم‬
  • 21. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 < /Good > < /Bad > Clean Code Meaningful Names Example 21 int elapsedTimeInDays; int daysSinceCreation; int daysSinceModification; int fileAgeInDays; int d; // elapsed time in days int s; // elapsed time in seconds
  • 22. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 02 { [Avoid Disinformation] - Don’t use names that are already used as commands by the operating system. - Don't use the Container type: Don't refer to a group of accounts as "accountList," whereas it is actually not a List. Instead, name it "accountGroup," "accounts," or "bunchOfAccounts." Clean Code Meaningful Names 22 ‫المضللة‬ ‫المعلومات‬ ‫تجنب‬
  • 24. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 03 { [Make Meaningful Distinctions] < If there are two different things in the same scope, you might be tempted to change one name in an arbitrary way.> } Clean Code Meaningful Names 24 ‫مغزى‬ ‫ذات‬ ‫تمييزات‬ ‫بعمل‬ ‫قم‬
  • 25. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Meaningful Names Example 25 String custs; String customers; class ProductInfo class ProductData What is stored in cust, which is different from customer? How are these classes different? How do you differentiate between these methods in the same class? void getActiveAccount() void getActiveAccounts() void getActiveAccountsInfo()
  • 26. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 04 { [Use Pronounceable Names] < Ensure that names are pronounceable. Nobody wants a tongue twister.> } Clean Code Meaningful Names 26 ‫نطقها‬ ‫يمكن‬ ‫التي‬ ‫األسماء‬ ‫استخدم‬
  • 27. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> <Bad /> Clean Code Meaningful Names Example 27 Date genymdhms; Date modymdhms; Int pszqint; Date GenerationTimeStamp; Date modificationTimestamp; Int PublicAccessCount;
  • 28. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 05 { [Use Searchable Names] - Avoid single letter names and numeric constants, if they are not easy to locate across the body of the text. - If a variable or constant might be seen or used in multiple places in a body of code, it is imperative to give it a search-friendly name. } Clean Code Meaningful Names 28
  • 29. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> <Bad /> Clean Code Meaningful Names Example 29 for (int j = 0; j < 34; j++) { s += (t[j] * 4) / 5; } int realDaysPerIdealDay = 4; const int WORK_DAYS_PER_WEEK = 5; int sum = 0; for (int j = 0; j < NUMBER_OF_TASKS; j++) { int realTaskDays = taskEstimate[j] * realDaysPerIdealDay; int realTaskWeeks = (realTaskDays / WORK_DAYS_PER_WEEK); sum += realTaskWeeks; } ‫النهم‬ ‫ممكن‬ local variables
  • 30. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 06 { [Avoid Encodings] } Clean Code Meaningful Names 30 < Hungarian Notation and other forms of type encoding are simply impediments today, because when we change the type, we also have to change the name. They make the code also so noisier that we cannot easily read the meaningful part of the code. And they confuses the reader.>
  • 31. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - Avoid unnecessary encodings of datatypes along with the variable name. Clean Code Meaningful Names 31 String nameString; Float salaryFloat; with a new compiler no need to do it
  • 32. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 06{ [Avoid Mental Mapping] } Clean Code Meaningful Names 32 ‫تتفلسف‬ ‫ال‬ <If scope of code is very small and no other names can conflict with it, loop counters can be named as i, j, k because they are familiar to programmers.>
  • 33. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Cont. 6{ [Avoid Mental Mapping] } Clean Code Meaningful Names 33 One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king
  • 34. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 07{ [Class Names] } Clean Code Meaningful Names 34 < Classes and objects should have noun or noun phrase names and should obey Single Responsibility Principle. It is always better to avoid noise words.>
  • 35. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 08{ [Method Names​ ] } Clean Code Meaningful Names 35 < Methods should have verb or verb phrase names like postPayment, deletePage, or save.>
  • 36. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 09{ [Don’t Be Cute​ ] } Clean Code Meaningful Names 36 < Using slang language or jokes in code just to show we are clever or just to have fun can hide the intention of author or even mislead readers and thus we should avoid.> ‫دمك‬ ‫تخفف‬ ‫ما‬
  • 37. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 10 { [Pick One Word per Concept​ ​ ] } Clean Code Meaningful Names 37 < Avoiding the use of similar names for equivalent methods in codebase can make the code cleaner. For instance having fetch, retrieve, get for similar concepts or having controller, manager in the same codebase can be confusing for readers.>
  • 38. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 11{ [Don’t Pun] } Clean Code Meaningful Names 38 < Sometimes when we try to follow the “one word per concept” rule, we might me tempted to use same names even though they don’t have really same semantics or concept.> ‫معنى‬ ‫من‬ ‫اكثر‬ ‫تحمل‬ ‫التي‬ ‫الكلمات‬ ‫تجنب‬ ‫تورية‬ Add append insert
  • 39. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 12{ [Use Solution Domain Names​ ] } Clean Code Meaningful Names 39 < Use computer science (CS) terms, algorithm names, pattern names, math terms, and so forth because readers and writers will most likely be developers and they won’t be familiar with problem domain names> jobQueue Demons homeBuilder
  • 40. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 13{ [Use Problem Domain Names​ ] } Clean Code Meaningful Names 40 ● When there is no general solution domain name for what you're doing, use the name from the problem domain. ● Separating solution and problem domain concepts is part of the job of a good programmer. It is not good to always use a problem domain name because they may have to ask others what it means.
  • 41. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14{ [Add Meaningful Context] } Clean Code Meaningful Names 41 Names themselves mostly doesn’t give a meaning to us, thus contexts(can be well-named classes, functions or even prefixing the name) in names help us to understand better.
  • 42. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Clean Code Meaningful Names Example 42 firstName lastName Street houseNumber City State zipcod User firstName lastName State User firstName lastName addressState User firstName lastName User userId Street houseNumber City State zipcod Address
  • 44. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1{ Function arguments (2 or fewer ideally) } Clean Code Functions 44 One or two arguments is the ideal case, and three should be avoided if possible. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much.
  • 45. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> <Bad /> Clean Code Functions Example 45 function createMenu(title, body, buttonText, cancellable) { // ... } createMenu("Foo", "Bar", "Baz", true); function createMenu({ title, body, buttonText, cancellable }) { // ... } createMenu({ title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true });
  • 46. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 2{ Functions should do one thing } Clean Code Functions 46 This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, it can be refactored easily and your code will read much cleaner.
  • 47. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> <Bad /> Clean Code Functions Example 47 function emailClients(clients) { clients.forEach(client => { const clientRecord = database.lookup(client); if (clientRecord.isActive()) { email(client); } }); }
  • 48. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> Clean Code Functions Example 48 function getActiveClientsEmails(clients) { clients.filter(isActiveClient).forEach(email); } function isActiveClient(client) { const clientRecord = database.lookup(client); return clientRecord.isActive(); }
  • 49. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3{ Function names should say what they do Clean Code Functions 49 <Bad /> function addToDate(date, month) { // ... } const date = new Date(); // It's hard to tell from the function name what is added addToDate(date, 1);
  • 50. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> Clean Code Functions Example 50 function addMonthToDate(month, date) { // ... } const date = new Date(); addMonthToDate(1, date);
  • 51. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 4{ Set default objects Clean Code Functions 51 <Bad /> <Good />
  • 52. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5{ Don't use flags as function parameters } Clean Code Functions 52 Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean
  • 53. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <Good /> <Bad /> Clean Code Functions Example 53 function createFile(name, temp) { if (temp) { fs.create(`./temp/${name}`); } else { fs.create(name); } } function createFile(name) { fs.create(name); } function createTempFile(name) { createFile(`./temp/${name}`); } temp ? createTempFile(name) : createFile(name)
  • 54. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The References { ∗ Clean Code: A Handbook of Agile Software Craftsmanship. ∗ Refactoring.Guru. ∗ Naming Conventions From Uncle Bob's Clean Code Philosophy. ∗ Clean Code - Uncle Bob - all lessons ∗ Clean Code / Javascript Here are some references that helped me to prepare the presentation: Clean Code Workshop References } 54
  • 55. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The References { ∗ What Is Clean Code 🔗 Naming Conventions / Part 1. ∗ What Is Clean Code 🔗 Naming Conventions / Part 2. ∗ Naming Classes - Why It Matters, and How to Do It Well. ∗ The Total Cost of Owning a Mess ∗ Meaningful Names in Ruby Code Here are some references that helped me to prepare the presentation: Clean Code Workshop References } 55

Editor's Notes

  • #5: Ask students before: What is Clean Code?
  • #8: سرد قصة
  • #10: الاخطاء او التسرع في اتخاذ القرار من قِبل المدراء
  • #22: حتى لو هي list , يفضل عدم اضافة كلمة List
  • #23: Is this zero or O
  • #24: Developers get themselves into trouble, when they write code to satisfy the compiler
  • #28: يمكن استخدام اسم طويل ولكن معبرّ ويمكن الاسم القصير اذا كان معبر
  • #30: يمكن استخدام اسم طويل ولكن معبرّ ويمكن الاسم القصير اذا كان معبر
  • #31: حتى لو هي list , يفضل عدم اضافة كلمة List