Misadventures In Legacy Code Eric Lee Not a

  • Slides: 30
Download presentation
Misadventures In Legacy Code Eric Lee

Misadventures In Legacy Code Eric Lee

 Not a professional presenter – be patient! 16 years at Microsoft 6 years

Not a professional presenter – be patient! 16 years at Microsoft 6 years in Office building automated testing systems 9 years in Microsoft Game Studios making games testable 1 year in Engineering Excellence building a secure source code portal Enthusiastic Agile practitioner I’ve inherited at least three “legacy” systems during my career

 Part 1: What is a legacy system? Part 2: How do I survive

Part 1: What is a legacy system? Part 2: How do I survive a legacy system? Part 3: How do I avoid creating more legacy systems?

Part 1: What is a legacy system?

Part 1: What is a legacy system?

leg·a·cy [ léggəssee ] noun (plural leg·a·cies) Definition: 1. bequest made in will: money

leg·a·cy [ léggəssee ] noun (plural leg·a·cies) Definition: 1. bequest made in will: money or property that is left to somebody in a will 2. something from past: something that is handed down or remains from a previous generation or time Encarta® World English Dictionary

Bob Jagendorf

Bob Jagendorf

Zombies: Legacy apps: Walking undead corpses Extremely hideous and disgusting Overwhelm you with numbers

Zombies: Legacy apps: Walking undead corpses Extremely hideous and disgusting Overwhelm you with numbers Infect anything they touch Consume your brain and destroy your will Very hard to eradicate Campy fiction disgusting Overwhelm you with numbers Infect anything they touch Consume your brain and destroy your will Very hard to eradicate Horrifyingly real

“Legacy” is the degree to which code: fails to capture essence fails to communicate

“Legacy” is the degree to which code: fails to capture essence fails to communicate essence captures irrelevant detail (ceremony)

“Legacy code is code without tests. ”

“Legacy code is code without tests. ”

 Legacy apps are often (but not always) old Sometimes no original developers remain

Legacy apps are often (but not always) old Sometimes no original developers remain Extremely difficult to make changes without breaking things Critical infrastructure has often been lost Source code is archaic, bewildering, and way too complex for what it does Documentation is nonexistent or wrong No automated test suite

Part 2: How do I survive a legacy system?

Part 2: How do I survive a legacy system?

 Sure, if you have unlimited resources Otherwise, probably not! The legacy code base

Sure, if you have unlimited resources Otherwise, probably not! The legacy code base holds valuable, hard- won knowledge Legacy code base continues to accumulate new features and bug fixes l Feature parity is a moving target Users want to see regular evidence of progress

 First, take a deep breath - this is gonna hurt Take inventory Eliminate

First, take a deep breath - this is gonna hurt Take inventory Eliminate friction Build a safety net Divide and conquer Practice software archeology Build for the future Have boat-loads of patience

 Understand the environment: l History of the project l Problem space l Stakeholders

Understand the environment: l History of the project l Problem space l Stakeholders l Target audience l Future goals Capture initial code metrics Inspect and prune all documentation

FRICTION

FRICTION

 Make lists of annoyances l After three months problems become invisible Always ask

Make lists of annoyances l After three months problems become invisible Always ask the five whys Get everything under source control Enable local builds, local execution Repeatable build process l Build scripts l Dedicated build machine for official builds Repeatable deployment

 You need tests! l Automated if you can, manual if you can’t l

You need tests! l Automated if you can, manual if you can’t l Let you change the code with less terror Baby steps l Start with integration tests l Don’t try to write unit tests for everything all at once Michael Feathers, Working Effectively With Legacy Code

 Don’t start with sweeping architectural changes Focus on one small area at a

Don’t start with sweeping architectural changes Focus on one small area at a time Refactor/rewrite as you go Build firewalls to protect new code from old code Practice the Boy Scout rule Have patience and a long-term plan

 Legacy code is like an archeological dig site l Layer upon layer of

Legacy code is like an archeological dig site l Layer upon layer of history l Lots of rubble from previous times Before changing code, try to understand: l The historical purpose of the code l The desired purpose of the code today Be suspicious and paranoid l Any mysterious behavior will bite you later l Always look for root causes

 Clear away the rubble Source code is a liability, not an asset l

Clear away the rubble Source code is a liability, not an asset l Delete dead code l Delete redundant comments l Simplify, simplify! l I reduced one component from 4000 to 400 ELOC, another from 6800 to 3200 Write unit tests for all new code

 Focus on today’s requirements, not ancient history Rethink all assumptions Bad code is

Focus on today’s requirements, not ancient history Rethink all assumptions Bad code is often a symptom of squishy problem definition Get really crisp on the fundamental concepts

 The code lies, don’t trust it Dig until you understand the whole story

The code lies, don’t trust it Dig until you understand the whole story Collect evidence and build a case Refactor the code until it tells the truth

How do I avoid creating more legacy systems?

How do I avoid creating more legacy systems?

“Always code as if the person who ends up maintaining your code is a

“Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live. ”

 Write unit and integration tests for all code Follow S. O. L. I.

Write unit and integration tests for all code Follow S. O. L. I. D design principles Be a naming Nazi Refactor constantly to match current goals Avoid “minimally invasive” fixes Write self-explanatory code, skip heavy docs

 Promote shared ownership Create and maintain long-term vision Stay focused on business value

Promote shared ownership Create and maintain long-term vision Stay focused on business value Automate everything Stamp out friction Limit technical debt

 Any questions? Contact me! l Email: saintgimp@hotmail. com l Twitter: @saintgimp l Blog:

Any questions? Contact me! l Email: saintgimp@hotmail. com l Twitter: @saintgimp l Blog: http: //blogs. msdn. com/elee