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
6. What’s “Legacy Code”?
Is there a Pragmatic Approach?
Should I care about it?
What about your feelings?
7.
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