7. 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
8. 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
14. 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
15. Solu
tion
#1
Extract Method:
Split method into many
auxiliary methods
16. Solu
tion
#2
Replace temp with query:
Move local variables
into methods
18. 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
19. Solu
tion
#1
Extract class:
If the class has multiple
responsibilities
20. Solu
tion
#2
Extract service object:
If the class has many
objects related to a
single action