When Does New Code Become Legacy Code?
Legacy code is code without tests
At what point does code become legacy? And what is good code? Answering the second question in a way that suits the rest of this blog post; Good code is code that does not require change, code that performs within spec and has no need of patching or updating even if years, perhaps decades, old.
Is legacy code just old code? And what threshold determines when code becomes old? Code written 30 years ago? Code written 3 months ago? Code written three sprints ago? Does source code then obey the "Seven Ages Of Man" from Shakespeare’s play "As You Like It"[1]?
All the world’s a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages. At first, the infant,
Mewling and puking in the nurse’s arms.
Then the whining schoolboy, with his satchel
And shining morning face, creeping like snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress' eyebrow. Then a soldier,
Full of strange oaths and bearded like the pard,
Jealous in honour, sudden and quick in quarrel,
Seeking the bubble reputation
Even in the cannon’s mouth. And then the justice,
In fair round belly with good capon lined,
With eyes severe and beard of formal cut,
Full of wise saws and modern instances;
And so he plays his part. The sixth age shifts
Into the lean and slippered pantaloon,
With spectacles on nose and pouch on side;
His youthful hose, well saved, a world too wide
For his shrunk shank, and his big manly voice,
Turning again toward childish treble, pipes
And whistles in his sound. Last scene of all,
That ends this strange eventful history,
Is second childishness and mere oblivion,
Sans teeth, sans eyes, sans taste, sans everything.
William Shakespeare
Probably not. So legacy code can’t be old code, or code that you inherited from a team or developer that has since moved on (perhaps to the great Kubernetes cluster in the sky).
In Working Effectively with Legacy Code"[2], Michael Feathers posits;
Legacy code is code without tests
Michael Feathers
The above definition is one I agree with. Writing code without the associate tests means creating new legacy code with each commit. Code without tests results in low confidence in releases that will function to spec when deployed into production for the first time, which means adding manual test cycles, which means code deployed outside of the CI/CD pipeline.