Legacy Code 
Evolve or 
Rewrite ? 
A retrospective 
Cyrille Martraire - @cyriux -
http://1.bp.blogspot.com/_SOzNIrpkT7M/TIhYjDKKz-I/AAAAAAAABjo/NCPSaVmZn0w/ 
s1600/100_2824.jpg
Passionate 
developer 
PARIS 
Since 1999 
! 
@cyriux
Paris Software 
Craftsmanship Community 
http://www.meetup.com/paris-software-craftsmanship/
TDD 
BDD 
DDD 
Legacy
LEGACY
Legacy: 
Evolve or rewrite?
Actual Example
Once upon a time…
There was a 
frustrated ambition
We Want:
Large Ambition
Asset Management
wanna do big things 
stuck with a legacy
So what do we do?
REWRITE
STOP!
We all want to 
rewrite
hard to release 
regressions
Legacy
Legacy 
No Test 
& Technical Debt too
http://skillsmatter.com/podcast/design-architecture/talk-from-eric-evans
Plenty of Legacy code 
working since 1993 
Redoing = Waste + Risk
Poorly designed code 
Working well 
Never changed 
Is OK
KEEP iT
KEEP GUI, DB, 
APP Server, 
middleware
We Want 
NEW FEATURES! CHANGE 
SOMETHING
Haz Change?
JUST 
DO IT!
QUICK
Did we break 
something?
No Test 
http://martinezglass.com
Takes a lot of time 
No Test 
+ Debug!
Just Do It: 
Not that quick 
Again and again
Could we do better?
EVOLVE
Surround with tests 
FIRST
Surround with tests!
Testing legacy code is 
hard & slow
Testing is an 
investment
Driver = Cost of 
testing
A matter of velocity
Legacy testing effort 
Vs. 
All new tested code? 
Is it worth it?
1:4 
(Ok, I totally made it up) 
ratio
REDO
Differentiating to 
the business?
The CEO will tell you
Not differentiating?
BUY
&ADAPT 
your 
process
Differentiating!
REWRITE
"What if we also rewrote 
this crappy part?" 
• Why not, but not now 
• Don't get lost redoing 
everything forever
...
JUST 
ENOUGH
You can rewrite 
You cannot 
rewrite all
What to rewrite? 
Where to 
stop?
What to rewrite? 
Find 
boundaries
Discover the app 
! 
! 
Read the documents 
Interview people 
Use the application 
Read the code 
Fix bugs
Visualize 
[primary flow & support features] 
http://www.fimarkets.com/pagesen/back-middle-front-office.php
Visualize 
[primary flow & support features] 
http://www.fimarkets.com/pagesen/back-middle-front-office.php
Visualize 
[primary flow & support features] 
http://www.fimarkets.com/pagesen/back-middle-front-office.php
http://staff.bath.ac.uk/enssa/LPDev.htm 
Asset 
Inventory
Asset Capture 
Transmission 
Exhaust 
http://martinfowler.com/bliki/AssetCapture.html 
Engine
Asset Capture 
Transmission 
Exhaust 
Focus on one single asset : the engine 
http://martinfowler.com/bliki/AssetCapture.html 
Engine
Main concepts 
[in selling-something business] 
Trading 
Financial 
Instruments
Main concepts 
[in selling-something business] 
Trading 
Financial 
Instruments 
DON’t 
TOUCH 
REWRITE
DOMAIN FTW!
BOUNDARIES 
& STRUCTURE
Agree on maxims 
[communicate the plan] 
• “Only one 
work site at 
a time: 
instruments” 
! 
• “When in 
Rome, do as 
Romans do”
"The code says it all"
Legacy: Fossilized 
Domain Expertise 
•inspiration 
•obfuscated 
http://newspaper.li/fossil/
Rewrite: 
! 
because of CHANGES 
(or it’s a bad idea)
needs
Rewrite: 
! 
must inject NEW knowledge
Rewrite: 
! 
need business ANALYSIS
WHOLE 
TEAM
Like a new 
project from 
scratch!
So how do 
we 
integrate?
STRANGLE
Strangler 
application 
A new module will 
progressively 
strangle the 
former one, for 
one single asset 
only 
http://martinfowler.com/bliki/StranglerApplication.html 
http://www.flickr.com/photos/louisfoecy/4114597043
Feature Toggles 
! 
Progressively 
turn off legacy 
functionalities 
& turn on the 
new ones 
OLD NEW
if(c == category1) 
{legacy.compute(...)} 
else if(c == category2) 
{strangler.compute(...)} 
//...
Grow a 
Bubble 
Context
! 
Listen to domain experts 
High hygiene standards 
Pair-programming 
TDD, BDD, DDD
Invest in domain 
knowledge 
• Bi-weekly 30mn 
training sessions
Dream model must pass 
every scenario 
Given a floating rate bond on EURIBOR 3M 
And a nominal of 15M EUR 
And an issue date of 2011/06/15 
And an end date of 2012/06/14 
And an SEMI_ANNUAL calculation period ! 
When the EURIBOR 3M evolves: 
Bespoke custom 
DREAM MODEL 
| 2011/09/15 | 3.5% | 
| 2012/03/15 | 4.0% | ! 
Then the cash-flows are: 
| 2011/12/13 | 23000 | 
| 2012/06/14 | 25500 |
Anti-corruption 
layer
Adapter
API 
SPI
Easier to integrate by 
construction 
bespoke lib
Old code: Freeze it
Rewrite 
= 
clear boundaries 
+ 
new strangler application 
+ 
minimal integration
Deliver 
Value 
Quickly
Questions? Did you try 
similar things too? 
Let’s discuss! 
@cyriux
Follow me @cyriux 
! 
Slides: slideshare.net/cyriux 
Blog: cyrille.martraire.com 
! 
In Paris? Join !
Merci

Legacy Code: Evolve or Rewrite?