Clean Code
by Robert C. Martin
Agenda
Introduction
Meaningful names
Functions
Classes
Agenda
Introduction
Meaningful names
Functions
Classes
What is a Clean Code ?
Bjarne Stroustrup, inventor of C++ and author of The C++
Programming Language.
“Clean code does one thing well”
What is a Clean Code ?
“Big” Dave Thomas, founder of OTI, godfather of the
Eclipse strategy.
“Clean code can be read, and
enhanced by a developer other
than its original author.”
What is a Clean Code ?
Robert C. Matrin, the author.
“Less ‘type and erases’ ”
How to measure Code Cleanliness ?
Key principles
Single Responsibility Principle (SRP)
⚪A class should have one responsibility over a single part of the
functionality, and that responsibility should be entirely
encapsulated by the class.
Open/Closed Principle (OCP)
⚪Software entities (classes, modules, functions, etc.) should be
open for extension, but closed for modification.
Agenda
Introduction
Meaningful names
Functions
Classes
Meaningful Name
Use Intention-Revealing Names
⚪If a name requires a comment, then the name does not reveal
its intent
Meaningful Name
Use Intention-Revealing Names
Meaningful Name
Use Pronounceable Names
Meaningful Name
Use Searchable Names
⚪Use a search-friendly name.
Meaningful Name
Pick One Word per Concept
⚪Pick one word for one abstract concept and stick with it.
⚪‘fetch’, ‘retrieve’, and ‘get’
Don’t Pun
⚪Avoid using the same word for two purposes.
⚪‘Add’
Meaningful Name
Class Names
⚪Noun or noun phrase names.
⚪ ‘Customer’, ‘WikiPage’, ‘Account’, and ‘AddressParser’
⚪Avoid words like ‘Manager’, ‘Processor’, ‘Data’, or ‘Info’
Method Names
⚪Verb or verb phrases.
⚪ ‘postPayment’, ‘deletePage’, or ‘save’
⚪Static factory methods with names that describe the
arguments.
Meaningful Name (Final Words)
Choosing good name requires
⚪Good descriptive skills.
⚪A shared cultural background.
It’s very grateful to change names (for the better)
Agenda
Introduction
Meaningful names
Functions
Classes
Functions
Small!
⚪Around 2-4 lines!
Blocks and Indenting
⚪Any ‘if’ or ‘while’ condition should be a method call.
⚪Indentation level shouldn’t be more than 2.
Functions
Do One Thing
⚪And do it well!
Functions
Do One Thing(cont.)
⚪TO notation
One level of abstraction.
⚪‘PathParser.render(pagePath);’
⚪‘pageData.getHtml()’
⚪‘pageString.append("n")’
A lot of implementation
details (high level)
Less details
(lower level)
Functions
Reading Code from Top to Bottom: The Stepdown Rule
High level of
abstraction
Lower level of
abstraction
Functions
Function Arguments
⚪Ideal number of arguments is ZERO (niladic).
⚪Next comes one (monadic), followed closely by two (dyadic).
⚪Three arguments (triadic) should be avoided.
Common Monadic Forms
⚪A function should be for
◾Asking a question ‘boolean fileExists(“MyFile”)’
◾Transforming input into something else and returning it
‘InputStream fileOpen(“MyFile”)’
⚪A function should return something
Functions
Flag arguments
⚪Passing a boolean into a function is a truly terrible practice.
⚪‘render(true)’ ??!!
⚪‘render(boolean isSuite)’ should be
‘renderForSuite()’ and ‘renderForSingleTest()’.
Output Arguments
⚪‘public void appendFooter(StringBuffer report)’
should be :
◾‘report.appendFooter();’
⚪If your function must change the state of something, have it
change the state of its owning object.
Functions
Have No Side Effects
Side effect!
Functions
Prefer Exceptions to Returning Error Codes
One entry and one exit points
⚪A function should have one ‘return’.
⚪Avoid ‘break’, and ‘continue’.
Agenda
Introduction
Meaningful names
Functions
Classes
Class
Encapsulation
Classes Should Be Small!
⚪Small means the number of responsibilities.
⚪The name of a class should describe what responsibilities it
fulfills.
⚪A class that you spend some time to name it is a big class.
Class
Encapsulation
Classes Should Be Small!
⚪Small means the number of responsibilities.
⚪The name of a class should describe what responsibilities it
fulfills.
⚪A class that you spend some time to name it is a big class.
The Single Responsibility Principle
⚪Classes should have one responsibility—one reason to change.
⚪This helps the developer to find, where to write code
Class
Cohesion
⚪The more instance variables a method manipulates the more
cohesive.
⚪A class in which each variable is used by each method is
maximally cohesive.
⚪High cohesion is always better.
Coupling
⚪Happens when a class rely or depend(partly) on another class.
⚪Less coupling is always better.
Class
Cohesion and Coupling
To be continued..

Clean code