SlideShare a Scribd company logo
Clean code
Nadiia Vlasenko
nadia.vlasenko.mail@gmail.com
The total cost of owning a mess
Later
equals never
Clean code
Meaningful
names
Meaningful
names
Meaningful distinctions
PronounceableMeaningful
names
Meaningful
names
Searchable
Meaningful
names
Method Names
Verb or verb phrase
Meaningful
names
One word per concept
Should be consistent
Meaningful
names
Don’t pun
Avoid using the same word for
two purposes
Functions
Functions Small
Good size:
Functions
Do one things
Function does one thing if:
● You can describe your function with only
one TO paragraph
TO RenderPageWithSetupsAndTeardowns, we
check to see whether the page is a test page and if
so, we include the setups and teardowns. In either
case we render the page in HTML.
● You can’t extract function with a name
that is not a restatement of
implementation
Functions One level of abstraction per
function
high getHtml();
intermediate String pagePathName =
PathParser.render(pagePath);
low .append(“n”).
Switch statements
What is wrong with it?
Functions
● Hard to make small
● Does more than one thing
● Violating SRP
● Violating OCP
● Unlimited number of functions
with the same structure
Use polymorphism and hide
switch case to ABSTRACT
FACTORY method
Descriptive names
Functions
E.g. Method does not only convert to trades. It also
unexpectedly validates tradeItems:
● Ward's principle: “You know you are working on clean code when
each routine turns out to be pretty much what you expected.”
● Don’t be afraid to make a name long
● Don’t be afraid to spend time on choosing name
ArgumentsFunctions
● Ideal number of
arguments: zero
● Avoid output
argument and use
return value
Flag argumentsFunctions
Hard to understand:
render(true)
Change
render(boolean isSuite)
to
renderForSuite();
and
renderForSingleTest();
Argument objects
When function has more than
two arguments consider to wrap
them into a class
Functions
Verbs and KeywordsFunctions
assertEquals(expected, actual)
to
assertExpectedEqualsActual(
expected, actual)
write(name)
to
writeField(name)
Have no side effects
Functions
E.g. our function mutates
validationErrors argument
Functions Error handling is one thing
Extract try/catch block
Comments
Comments
Good comments:
● Informative
● Explanation of intent
● Clarification
Comments
Good comments:
● Warning of consequences
● TODO
● Amplification of importance
● Javadocs
Comments
Bad comments:
● Noise comments
● Commented out code
● Javadoc in nonpublic code
Comments Bad comments
Don’t use comment when you
can use a function or a variable
Formatting The newspaper metaphor
The first paragraph gives you
the synopsis of the whole story,
hiding the details.
As you continue the details
increase.
Вставьте таблицу или график
с результатами
The law of
demeter
Method M of object O should
only call following methods:
● Methods of Object O itself
(because you could talk with
yourself)
● Methods of objects passed
as arguments to M
● Methods of objects held in
instance variable
● Methods of objects which
are created locally in method
MВставьте таблицу или график
с результатами
Error handling
Error handling
Use unchecked exceptions
What price?
Open/Closed Principle violation
You must declare exception in
signature of each method
between you and the catch.
Signature changes in law level
force signature changes on
many higher levels
Error handling Provide context with
exceptions
Mention the operation that failed
and the type of failure to be able
to log in catch
Define the normal flow
to
Error handling
Don’t return null
● Return empty list
● Wrap a null-returning
method of third-party with a
method returning exception
or special case object
Don’t pass null
Boundaries
Boundaries
Using third-party code
Encapsulate to protect from
changes in third-party code
Boundaries
Learning tests
Write some tests to understand
third-party code instead of
experimenting in production
code
● Cost nothing as we have to
learn API anyway.
● Verify whether third-party
packages work the way we
expect them to
Boundaries
Using code which does not
yet exist
Writing an
abstraction keeps
client code more
readable and
focused on what it is
trying to accomplish
Unit Tests
Unit Tests
Unit Tests
The Three laws of TDD
1. You may not write production code until
you have written a failing unit test pass.
1. You may not write any more of a unit
test than is sufficient to fail; and not
compiling is failing.
1. You may not write more production
code than is sufficient to pass the one
failing unit test.
Unit Tests Given-when-then
Unit Tests
Single concept for test
One then per test
Unit Tests
Classes
Classes Classes should be small
Class is too large if:
● We can’t derive a concise
name (without words
Processor, Manager, Super)
● We can’t to write a brief
description without “if”,
“and”, “or”, “but”
Classes
Single Responsibility Principle
=
One reason to change
Don’t be afraid of navigating through
lots of small classes. You code will
not have more concepts. This
concepts will be more organized.
Classes Cohesion
Maximally cohesive class:
each variable is used by each
method
Strive to
“Low coupling high cohesion"
The Boy Scout Rule
Leave the campground cleaner than you found it.
Literature
Clean Code by Robert Martin
The Toyota Way by Jeffrey K. Liker

More Related Content

PPTX
The Art of Clean code
PPTX
Clean Code
PPTX
Clean Code Principles
PPTX
Clean code
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
PDF
Integration testing with spring @snow one
KEY
Clean Code
PDF
Clean coding-practices
The Art of Clean code
Clean Code
Clean Code Principles
Clean code
Clean Pragmatic Architecture - Avoiding a Monolith
Integration testing with spring @snow one
Clean Code
Clean coding-practices

What's hot (20)

PPTX
Clean code slide
PDF
Clean code
PPTX
clean code book summary - uncle bob - English version
PDF
Unit Testing like a Pro - The Circle of Purity
KEY
Clean code and Code Smells
PPTX
React js
PDF
PDF
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
PDF
Clean pragmatic architecture @ devflix
PDF
DDD in PHP
PPTX
Introduction to react and redux
PPTX
React workshop presentation
PPTX
Clean code coding like a professional
PDF
Design functional solutions in Java, a practical example
PDF
Clean code
PDF
The Art of Unit Testing - Towards a Testable Design
PPT
Java Basics
PDF
Domain driven design and model driven development
PDF
The Power of Composition
PPTX
Clean Code I - Best Practices
Clean code slide
Clean code
clean code book summary - uncle bob - English version
Unit Testing like a Pro - The Circle of Purity
Clean code and Code Smells
React js
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Clean pragmatic architecture @ devflix
DDD in PHP
Introduction to react and redux
React workshop presentation
Clean code coding like a professional
Design functional solutions in Java, a practical example
Clean code
The Art of Unit Testing - Towards a Testable Design
Java Basics
Domain driven design and model driven development
The Power of Composition
Clean Code I - Best Practices
Ad

Similar to Clean code (20)

PPTX
Clean code - DSC DYPCOE
ODP
Clean Code - Part 2
PPT
Clean Code
PPT
Coding Standards
PPTX
Clean code
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
PPTX
Clean code quotes - Citações e provocações
PDF
Clean code
PDF
Clean code and code smells
PPTX
Clean Code - Writing code for human
PPTX
Are You Ready For Clean Code?
PDF
Clean Code
PPT
Clean code
PDF
Good Coding Practices with JavaScript
PPTX
Writing Clean Code (Recommendations by Robert Martin)
PPTX
Unit testing
PPTX
UNIT TESTING PPT
PPTX
Clean code, Feb 2012
PPTX
Clean Code: Chapter 3 Function
PDF
Clean Code
Clean code - DSC DYPCOE
Clean Code - Part 2
Clean Code
Coding Standards
Clean code
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
Clean code quotes - Citações e provocações
Clean code
Clean code and code smells
Clean Code - Writing code for human
Are You Ready For Clean Code?
Clean Code
Clean code
Good Coding Practices with JavaScript
Writing Clean Code (Recommendations by Robert Martin)
Unit testing
UNIT TESTING PPT
Clean code, Feb 2012
Clean Code: Chapter 3 Function
Clean Code
Ad

Recently uploaded (20)

PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Website Design Services for Small Businesses.pdf
PDF
Cost to Outsource Software Development in 2025
PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
history of c programming in notes for students .pptx
PDF
AutoCAD Professional Crack 2025 With License Key
PPTX
assetexplorer- product-overview - presentation
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Reimagine Home Health with the Power of Agentic AI​
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Design an Analysis of Algorithms II-SECS-1021-03
Why Generative AI is the Future of Content, Code & Creativity?
Design an Analysis of Algorithms I-SECS-1021-03
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Download FL Studio Crack Latest version 2025 ?
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Wondershare Filmora 15 Crack With Activation Key [2025
Website Design Services for Small Businesses.pdf
Cost to Outsource Software Development in 2025
Complete Guide to Website Development in Malaysia for SMEs
history of c programming in notes for students .pptx
AutoCAD Professional Crack 2025 With License Key
assetexplorer- product-overview - presentation
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Monitoring Stack: Grafana, Loki & Promtail
Reimagine Home Health with the Power of Agentic AI​

Clean code