SlideShare a Scribd company logo
1 of 36
Download to read offline
clean code
from   theory to practice
                  by   Luís Zamith
@zamith
Clean Code
TAKING YOUR PROGRAMS TO THE LAUNDRY
My d
                                efin
                                    ition
1. Looks like you cared

2. Pretty much what you expected

3. Language looks as perfect fit
for problem
circular friction
reduction device     =
with central axis




Design Patterns
template method: reports
                                                          bad
                                                                code
    class Report
     def initialize
       @title = 'Monthly Report'
       @text = [ 'Things are going', 'really, really well.' ]
     end

     def output_report
      ...HTML specific stuff...
     end
    end
template method: reports                                           bett
                                                                        er co
class Report                                                                       de
 def initialize
   @title = 'Monthly Report'                       def output_start; end
   @text = ['Things are going', 'really, really
well.']                                            def output_head
 end                                                output_line(@title)
                                                   end
 def output_report
  output_start                                     def output_line(line)
  output_head                                       raise 'Called abstract method: output_line'
  @text.each do |line|                             end
   output_line(line)
  end                                              def output_end; end
  output_end                                      end
 end
Design Principles
     THE THEORY BEHIND THE THEORY
Single responsibility
Open closed
Liskov substitution
Interface segragation     Uncle
                        Bob Martin

Dependency inversion
RECA
                       P
DESIGN
PRINCIPLES
     +       = CODE
               CLEAN
DESIGN
PATTERNS
Code Smells
   Sir, your issues have some code.
Smell number 1
long method
TOO LONG
  Can’t tell what it
does at a glance, it’s


                TOOO
                  T  R
                    LONG
More than one level of


                 C LONG
 nesting, probably



            E FATOO
More than one level of


           R TOO LONG
 abstraction, may be

 Flog score greater
  than 15, may be
Solu
                      tion
                             #1

Extract Method:
Split method into many
auxiliary methods
Solu
                           tion
                                  #2

Replace temp with query:
Move local variables
into methods
Smell number 2
large class
TOO BIG
Need to scroll to know
  what it does, it’s



                   O R
                 TOO BIG
                  T
More private methods


                C BIG
   than public, it’s



            E FA TOO
   More than seven


           R TOO BIG
  methods, probably

  Flog score greater
   than 50, may be
Solu
                        tion
                               #1
Extract class:
If the class has multiple
responsibilities
Solu
                              tion
                                     #2
Extract service object:
If the class has many
objects related to a
single action
strategy pattern: reports


report = Report.new(HTMLFormatter.new)
report.output_report

report.formatter = PlainTextFormatter.new
report.output_report
beware of
the god class
Smell number 3
feature envy
               All your features
                    are mine
4
Smelling feature envy:



                         SYMPTOMS
Smelling feature envy:


   Many references to the
       same object
Smelling feature envy:

 Local variables or params that
  are used more than instance
     variables and methods
Smelling feature envy:


  Methods that include a class
      name in their name
Smelling feature envy:


Private methods on the same
class with the same parameter
Solu
                         tion
                                #1
Extract method:
If possible move part of a
method to the
appropriate class
Solu
                     tion
                            #2
Move method:

Move the entire method
into the right class
DESIGN
PRINCIPLES
     +       = CODE
               CLEAN
DESIGN
PATTERNS
missing
  some

style
Learn the principles

  use the   patterns
    Know the
   smells
thanks!
luís zamith
blog.zamith.pt
twitter+github:   @zamith

More Related Content

What's hot

5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
CtOlaf
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov
 

What's hot (20)

Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
Python syntax
Python syntaxPython syntax
Python syntax
 
C presentation book
C presentation bookC presentation book
C presentation book
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Lesson 4 constant
Lesson 4  constantLesson 4  constant
Lesson 4 constant
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
 
Javascript
JavascriptJavascript
Javascript
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
2. python basic syntax
2. python   basic syntax2. python   basic syntax
2. python basic syntax
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
 
Lesson 2 php data types
Lesson 2   php data typesLesson 2   php data types
Lesson 2 php data types
 
Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
C language
C languageC language
C language
 

Similar to Clean code

Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
Edorian
 
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptxChapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
rajinevitable05
 

Similar to Clean code (20)

Ruby Hell Yeah
Ruby Hell YeahRuby Hell Yeah
Ruby Hell Yeah
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
PROBLEM SOLVING TECHNIQUES USING PYTHON.pptx
PROBLEM SOLVING TECHNIQUES USING PYTHON.pptxPROBLEM SOLVING TECHNIQUES USING PYTHON.pptx
PROBLEM SOLVING TECHNIQUES USING PYTHON.pptx
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
[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,...
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Refactoring
RefactoringRefactoring
Refactoring
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Learn the Art of Writing Clean Code
Learn the Art of Writing Clean CodeLearn the Art of Writing Clean Code
Learn the Art of Writing Clean Code
 
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptxChapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
Chapter vvxxxxxxxxxxx1 - Part 1 (3).pptx
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Clean code