Inside
30days
Album
Fighting against
legacy code
@kyanny
#sprk2012
129݄17݄༵
Slide 2
Slide 2 text
My bitter story
For lonely Railers
129݄17݄༵
Slide 3
Slide 3 text
•Writing tests against
legacy code
•Technical debt
•Attitude towards legacy
code
129݄17݄༵
Slide 4
Slide 4 text
@kyanny
Kensuke Nagae
ˑϒʔϝϥϯ
129݄17݄༵
Slide 5
Slide 5 text
paperboy&co.
129݄17݄༵
Slide 6
Slide 6 text
http://sqale.jp
129݄17݄༵
Slide 7
Slide 7 text
http://30d.jp
129݄17݄༵
Slide 8
Slide 8 text
turn
profitable
129݄17݄༵
Slide 9
Slide 9 text
$ rails -v
129݄17݄༵
Slide 10
Slide 10 text
2.0.2
~ June 2012
129݄17݄༵
Slide 11
Slide 11 text
ଔ
ۀ
ੜ
129݄17݄༵
Slide 12
Slide 12 text
2.1.2
Jul 2012 ~
129݄17݄༵
Slide 13
Slide 13 text
2008/04 ~ 2012/06
129݄17݄༵
Slide 14
Slide 14 text
Why need 4 years?
129݄17݄༵
Slide 15
Slide 15 text
Lack of tests
129݄17݄༵
Slide 16
Slide 16 text
Need more tests, but...
129݄17݄༵
Slide 17
Slide 17 text
Writing tests
against legacy code
is difficult
129݄17݄༵
Slide 18
Slide 18 text
TDD is idealistic :(
129݄17݄༵
Slide 19
Slide 19 text
Stub & Mock
is your friend :)
129݄17݄༵
Slide 20
Slide 20 text
Stub
def pay!
if credit_card.valid? # validate with complex process
Payment.create!
end
end
credit_card.stub(:valid?).and_return(true)
expects { pay! }.change { Payment.count }.by(1)
129݄17݄༵
Slide 21
Slide 21 text
Mock
def photo.rotate(angle)
rpc.call('rotatePhoto', angle)
end
rpc.should_receive(:call).with('rotatePhoto', 180)
photo.rotate(180)
129݄17݄༵
Slide 22
Slide 22 text
Great weapon for
fighting legacy code
129݄17݄༵
Slide 23
Slide 23 text
129݄17݄༵
Slide 24
Slide 24 text
“I was worried about
writing tests against
existent code.
It seems stupid.
Is it worth?”
129݄17݄༵
Slide 25
Slide 25 text
129݄17݄༵
Slide 26
Slide 26 text
༷Խςετ
Characterization test
http://en.wikipedia.org/wiki/Characterization_test
129݄17݄༵
Slide 27
Slide 27 text
describe
(characterize) the
actual behaviour
129݄17݄༵
“Friends don’t let friends
use backgrounDRb.”
http://www.engineyard.com/blog/2009/5-tips-for-
deploying-background-jobs/
129݄17݄༵
Slide 36
Slide 36 text
DelayedJob
129݄17݄༵
Slide 37
Slide 37 text
DelayedJob
Not
Slide 38
Slide 38 text
Monkey patching :P
129݄17݄༵
Slide 39
Slide 39 text
if Rails::VERSION::STRING <= '2.1.2'
unless Time.singleton_methods.include?(:zone)
class Time
def self.zone
Time # enable to call Time.zone.now
end
end
end
end
129݄17݄༵
Slide 40
Slide 40 text
Bad schema
129݄17݄༵
Slide 41
Slide 41 text
SELECT * FROM photos
WHERE album_id = ?
AND (deleted_at IS NULL
OR deleted_at >= ?)
SQL modification by acts_as_paranoid
129݄17݄༵
Slide 42
Slide 42 text
create_table :photos do |t|
t.string :id
end
Non Integer id (UUID)
129݄17݄༵
Slide 43
Slide 43 text
Bad schema causes slow
queries, but schema
change is hard.
129݄17݄༵
Slide 44
Slide 44 text
Buy high-performance
hardware to make up for
bad design :)
129݄17݄༵
Slide 45
Slide 45 text
Long holiday
Buy new server & SSD
129݄17݄༵
Slide 46
Slide 46 text
129݄17݄༵
Slide 47
Slide 47 text
Attitude towards
legacy code
129݄17݄༵
Slide 48
Slide 48 text
Be realistic as a
business person
129݄17݄༵
Slide 49
Slide 49 text
Don’t stick the ideal
129݄17݄༵
Slide 50
Slide 50 text
Be proud as a Developer
129݄17݄༵
Slide 51
Slide 51 text
Aim for perfect code
129݄17݄༵
Slide 52
Slide 52 text
129݄17݄༵
Slide 53
Slide 53 text
Don't leave a technical
debt to future
129݄17݄༵
Slide 54
Slide 54 text
Break the negative chain
129݄17݄༵
Slide 55
Slide 55 text
Conclusion
129݄17݄༵
Slide 56
Slide 56 text
Writing tests
against legacy code
is difficult.
But it will pay.
129݄17݄༵
Slide 57
Slide 57 text
Technical debt can make
up by money.
Save your time.
129݄17݄༵