5. Whatâs âLegacy Codeâ?
Quiz: Legacy Code?
A. Code difficult to change
B. Inherited code difficult to change
C. Valuable code weâre afraid to change
8. Whatâs âLegacy Codeâ?
o Nervous (I depend on⊠)
âengagedâ with debugger , complex infrastructure
o Fear / Under pressure
.. so I do changes inside the existing code
o Unsafe
Did i broke something ? Is this working ?
o Resigned
âEdit and prayâ
12. The white rabbitâs hole
We need a map for this journey: how
can we face with Legacy code?
UNDERSTAND COVER REFACTOR CHANGE
Each step has different target: focus
on 1 at times.
13. The white rabbitâs hole: HOW
âą Use it before looking at the code
âą Find one thing you know
1. look for keywords
2. and trace the actions backward
âą Sketch refactoring
âą Keep reading someone else code
14. The white rabbitâs hole: COVER
You need 100% of test coverage
if condition1
âŠ.
âŠ.
if condition2
âŠ..
âŠ..
if condition3
âŠ..
âŠ..
else
âŠ..
âŠ..
else
âŠ..
âŠ..
else
âŠ..
âŠ..
Start from shortest
to deepest branch
15. COVER: test the unknown
assume to know what the code is
supposed to do
⊠but what if we donât ?
âąTest name ???
âąExpected result ???
16. COVER: test the unknown
1. Write a test named âxâ
2. Set any expected result
3. Run it and get a Failure
4. Copy the text and make it pass
5. Give a better name
17. COVER: test the unknown
Extract the code you want to change in a
separate section
- Smaller problem to solve
- Quicker way to the coverage
18. EXAMPLE: Subclass and Override
public class A {
public void do1(âŠ) {
user = session.getAttribute(âuserâ);
...
}
}
Souce Code
public void do1(âŠ) {
...
logged = isLogged(âmario")
}
protected Boolean isLogged(..)
public class ATest
{
[TestMEthod]
public void do1WhenLogged() { }
private class TestableA : A
{
protected override boolean isLogged()
{
return true
}
}
}
Test Code
19. The white rabbitâs hole: REFACTOR
âą Related to change phase
Start
End
Add a Test
Hard to
Create or
Make it Pass?
Refactor
Apply the
change
20. The white rabbitâs hole: REFACTOR
âą Start from the deepest brach
if condition1
âŠ.
âŠ.
if condition2
âŠ..
âŠ..
if condition3
âŠ..
âŠ..
else
âŠ..
âŠ..
else
âŠ..
âŠ..
else
âŠ..
âŠ..
21. The white rabbitâs hole: CHANGE
Adding a new feature: Common mistake
âLetâs put new feature inside this existing
method because it should happen at the
same timeâ
Problem : ⊠test old and new code together
22. EXAMPLE: Adding a new feature
public void do1()
{
âŠ
// new Feature(..).apply(âŠ)
}
Souce Code
public class Feature
{
public void apply(âŠ)
{
âŠ
}
}
public void do1()
{
âŠ
new Feature(..).apply(âŠ)
}
Productive Code
TDD
23. DONâT REPEAT YOURSELF
⊠Split screen vertically TDD
⊠Continuous testing plugin
⊠Use shortcuts instead of repeating actions
⊠Small commits / steps
25. REFERENCES
âą Refactoring: Improving the Design of Existing Code
https://www.amazon.it/Refactoring-Improving-Design-Existing-
Code/dp/0201485672/ref=sr_1_1?ie=UTF8&qid=1504536816&sr=8-
1&keywords=Refactoring%3A+Improving+the+Design+of+Existing+Code
âą M. Feathers- Working Effectively with Legacy Code
https://www.amazon.it/Working-Effectively-Legacy-Michael-
Feathers/dp/0131177052/ref=sr_1_1?s=english-books&ie=UTF8&qid=1504536861&sr=1-
1&keywords=Working+Effectively+with+Legacy+Code%3A+Michael+Feathers