SlideShare a Scribd company logo
clean code
from   theory to practice
                  by   Luís Zamith
@zamith
Clean code
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!
Clean code
luís zamith
blog.zamith.pt
twitter+github:   @zamith

More Related Content

PDF
11 ruby methods
PPT
PDF
Turbo prolog 2.0 basics
PPT
StandardsandStylesCProgramming
PPT
Basic of c language
PDF
Zaridah lecture2
PPS
C programming session 05
11 ruby methods
Turbo prolog 2.0 basics
StandardsandStylesCProgramming
Basic of c language
Zaridah lecture2
C programming session 05

What's hot (20)

PPTX
Lesson 6 php if...else...elseif statements
PDF
Python syntax
PPT
C presentation book
PPTX
Computer programming(CP)
PDF
Pointers-Computer programming
PDF
Functions-Computer programming
PPTX
Learn Python The Hard Way Presentation
PPTX
Lesson 4 constant
PPTX
5. using variables, data, expressions and constants
PPT
Javascript
PDF
Strings-Computer programming
PDF
2. python basic syntax
PDF
PDF
Anton Kasyanov, Introduction to Python, Lecture2
PPTX
Lesson 2 php data types
PDF
Fafl notes [2010] (sjbit)
ODP
Basic C Programming language
PPTX
Introduction to c programming
PDF
Let’s Learn Python An introduction to Python
PPT
Lesson 6 php if...else...elseif statements
Python syntax
C presentation book
Computer programming(CP)
Pointers-Computer programming
Functions-Computer programming
Learn Python The Hard Way Presentation
Lesson 4 constant
5. using variables, data, expressions and constants
Javascript
Strings-Computer programming
2. python basic syntax
Anton Kasyanov, Introduction to Python, Lecture2
Lesson 2 php data types
Fafl notes [2010] (sjbit)
Basic C Programming language
Introduction to c programming
Let’s Learn Python An introduction to Python
Ad

Similar to Clean code (20)

PDF
Bade Smells in Code
PDF
Bad Code Smells
PPTX
Code smells
PPTX
Clean Code
PPTX
Bad Smells in Code
PDF
Refactoring
PDF
Webcast: Identify and Correct Common Code Smells
PDF
Programming != Writing Code
PPTX
Power of Patterns: Refactoring to (or away from) Patterns
PPTX
Software Craftsmanship - Code Smells - Dispensables
PPTX
Software Craftsmanship - Code Smells - Change Preventers
KEY
Code Quality Analysis
PPTX
Patterns of Design, as Presented by Ritchey
PDF
Refactoring: Improve the design of existing code
PPTX
Clean code presentation
PDF
Refactoring Workshop (Rails Pacific 2014)
PPTX
Anti-patterns
PDF
Keeping code clean
PDF
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
PPTX
Code smells and Other Malodorous Software Odors
Bade Smells in Code
Bad Code Smells
Code smells
Clean Code
Bad Smells in Code
Refactoring
Webcast: Identify and Correct Common Code Smells
Programming != Writing Code
Power of Patterns: Refactoring to (or away from) Patterns
Software Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - Change Preventers
Code Quality Analysis
Patterns of Design, as Presented by Ritchey
Refactoring: Improve the design of existing code
Clean code presentation
Refactoring Workshop (Rails Pacific 2014)
Anti-patterns
Keeping code clean
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
Code smells and Other Malodorous Software Odors
Ad

Recently uploaded (20)

PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
project resource management chapter-09.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Getting Started with Data Integration: FME Form 101
PPTX
TLE Review Electricity (Electricity).pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
A Presentation on Artificial Intelligence
project resource management chapter-09.pdf
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
SOPHOS-XG Firewall Administrator PPT.pptx
cloud_computing_Infrastucture_as_cloud_p
Unlocking AI with Model Context Protocol (MCP)
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
DP Operators-handbook-extract for the Mautical Institute
Web App vs Mobile App What Should You Build First.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Encapsulation theory and applications.pdf
Hybrid model detection and classification of lung cancer
Getting Started with Data Integration: FME Form 101
TLE Review Electricity (Electricity).pptx

Clean code