@asgrim
Tips for Tackling
a Legacy Codebase
James Titcumb
ScotlandPHP 2021
$ whoami
James Titcumb
www.jamestitcumb.com
www.roave.com
@asgrim
@asgrim
So, you joined a new team.
@asgrim
WTF per minute: 0
@asgrim
WTF per minute: 0
@asgrim
WTF per minute: 1
@asgrim
WTF per minute: 3
@asgrim WTF per minute: 15
@asgrim WTF per minute: 256
@asgrim
@asgrim
"Legacy"
@asgrim
"Legacy"
@asgrim
"Legacy"
Can mean...
● Old code
● Code that uses outdated patterns
● Obsolete (but still used)
● Difficult to maintain
● Uses old frameworks / libraries
● Fragile / untouchable
● Expensive to replace
@asgrim
"Legacy"
Can mean...
● Old code
● Code that uses outdated patterns
● Obsolete (but still used)
● Difficult to maintain
● Uses old frameworks / libraries
● Fragile / untouchable
● Expensive to replace
● Working code
● Code that (usually) still provides VALUE
● Code written an hour ago
@asgrim
"Code can be in two states:
in production
...or almost useless"
- Srdjan Vranac (@vranac)
@asgrim
Result of Fast Iteration
@asgrim
Result of Years of Stability
@asgrim
"Legacy" !== "bad"
@asgrim
Refactoring
@asgrim
"If it ain't broke, don't fix it"
- Someone in USA, probably
@asgrim
How to Tackle Legacy Existing Code
General skills
@asgrim
Business Communication
@asgrim
Peer Communication
@asgrim
Agree direction
@asgrim
Define processes
@asgrim
Produce documentation
Technical skills & approaches
@asgrim
Reading code.
@asgrim
Understanding code.
@asgrim
Understanding code.
Review code!
@asgrim
Understanding code.
Try using a step debugger!
@asgrim
Understanding code.
Write tests to characterise the code!
@asgrim
Understanding the business.
@asgrim
Success factor
😊
Developer Experience + Business Value = Happiness
@asgrim
Should you refactor?
@asgrim
Don't refactor for tech reasons alone
@asgrim
When you DO decide to refactor
@asgrim
Limit your scope
@asgrim
Make a plan
@asgrim
Tooling
@asgrim
Coding standards
@asgrim
@asgrim
@asgrim
Static Analysis "Whack-a-Mole"
@asgrim
Testing
@asgrim
Starting from nothing
@asgrim
Starting from something bad
@asgrim
Good tests > Bad tests > No tests
@asgrim
Make good testing habits
@asgrim
Make all team members reviewers
@asgrim
Feedback, don't "gatekeep"
@asgrim
Policies
"Usual" policies
@asgrim
Policies
"Flexible" policies
@asgrim
@asgrim
One approach to tackle "no tests"
@asgrim
Focus on critical functionality
@asgrim
Then build up day-to-day
@asgrim
Limit the scope of characterisation tests
@asgrim
Delete the test…?
@asgrim
Make sure tests are run in CI
@asgrim
@asgrim
Automate all the checks!
@asgrim
To summarise...
"Legacy" !== "bad"
● It's just "existing code"
● If it's in production, it usually produces VALUE
How to tackle existing codebase?
● You don't always need to refactor
● Communicate - peers & business
● Produce documentation
● Learn to really READ code!
● Determine what produces VALUE (both business value + developer experience)
● Use tools to help improve code quality
● Characterise critical functionality with tests
● Build up your "test pyramid"
● Automate all the things!
Any questions?
James Titcumb 🥃
@asgrim

Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)