Slide 1

Slide 1 text

clean code from theory to practice by Luís Zamith

Slide 2

Slide 2 text

@zamith

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Clean Code TAKING YOUR PROGRAMS TO THE LAUDRY

Slide 5

Slide 5 text

1. Looks like you cared 2. Pretty much what you expected 3. Language looks as perfect fit for problem My definition

Slide 6

Slide 6 text

Design Patterns circular friction reduction device with central axis =

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

template method: reports better code class Report def initialize @title = 'Monthly Report' @text = ['Things are going', 'really, really well.'] end def output_report output_start output_head @text.each do |line| output_line(line) end output_end end def output_start; end def output_head output_line(@title) end def output_line(line) raise 'Called abstract method: output_line' end def output_end; end end

Slide 9

Slide 9 text

Design Principles THE THEORY BEHIND THE THEORY

Slide 10

Slide 10 text

S O L I D ingle responsibility pen closed iskov substitution nterface segragation ependency inversion Uncle Bob Martin

Slide 11

Slide 11 text

RECAP DESIGN PRINCIPLES DESIGN PATTERNS + = CLEAN CODE

Slide 12

Slide 12 text

Code Smells Sir, your issues have some code.

Slide 13

Slide 13 text

long method Smell number 1

Slide 14

Slide 14 text

Can’t tell what it does at a glance, it’s TOO LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be Flog score greater than 15, may be TOO LONG REFACTOR

Slide 15

Slide 15 text

Extract Method: Split method into many auxiliary methods Solution #1

Slide 16

Slide 16 text

Replace temp with query: Move local variables into methods Solution #2

Slide 17

Slide 17 text

large class Smell number 2

Slide 18

Slide 18 text

Need to scroll to know what it does, it’s TOO BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably Flog score greater than 50, may be TOO BIG REFACTOR

Slide 19

Slide 19 text

Extract class: If the class has multiple responsibilities Solution #1

Slide 20

Slide 20 text

Extract service object: If the class has many objects related to a single action Solution #2

Slide 21

Slide 21 text

strategy pattern: reports report = Report.new(HTMLFormatter.new) report.output_report report.formatter = PlainTextFormatter.new report.output_report

Slide 22

Slide 22 text

beware of the god class

Slide 23

Slide 23 text

feature envy Smell number 3 All your features are mine

Slide 24

Slide 24 text

Smelling feature envy: 4SYMPTOMS

Slide 25

Slide 25 text

Smelling feature envy: Many references to the same object

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Smelling feature envy: Private methods on the same class with the same parameter

Slide 29

Slide 29 text

Extract method: If possible move part of a method to the appropriate class Solution #1

Slide 30

Slide 30 text

Move method: Move the entire method into the right class Solution #2

Slide 31

Slide 31 text

DESIGN PRINCIPLES DESIGN PATTERNS + = CLEAN CODE

Slide 32

Slide 32 text

missing some style

Slide 33

Slide 33 text

Learn the principles patterns Know the use e smells

Slide 34

Slide 34 text

thanks!

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

blog.zamith.pt luís zamith twitter+github: @zamith