Lesson 11 of 12 advanced

Legacy code and behaviour-preserving migration

পুরোনো code আগে বুঝে test-এ ধরো। একবারে rewrite করলে আসল নিয়ম হারিয়ে যেতে পারে।

Guess first

Pick an answer. You will see if you were right at the end of the lesson.

What undocumented behaviour could disappear?

Open interactive version (quiz + challenge)

Real-world analogy

Repair a working bridge one tested section at a time while traffic still depends on it.

What is it?

Legacy systems often contain business knowledge that is absent from documentation. Characterisation tests record current behaviour before refactoring. Some current behaviour is a bug; label it explicitly and agree on the desired change rather than accidentally preserving or deleting it.

Real-world relevance

One shared support-ticket product, three optional entry points. Frontend: Characterise an old form component before replacing its state library. Backend: Characterise a legacy endpoint before changing its query or response mapping. Mobile: Characterise the sync adapter before replacing persistence or navigation.

Key points

Quick check

Answer from memory — scrolling up first makes it stick less.

What artifact demonstrates this lesson?

Which statement is defensible?

Code example

def legacy_label(code):
    return {0: "queued", 1: "saved"}.get(code, "unknown")

def test_characterisation():
    assert legacy_label(0) == "queued"
    assert legacy_label(1) == "saved"
    assert legacy_label(99) == "unknown"

Line-by-line walkthrough

  1. 1. Python: unknown input is an explicit recorded behaviour. A replacement that throws for 99 changes the contract and needs a deliberate decision.

Spot the bug

Rewrite the whole module and trust the new architecture
Need a hint?
What undocumented behaviour could disappear?
Show answer
Capture the existing contract, replace a narrow seam and verify the migration incrementally.

Explain like I'm 5

পুরোনো code আগে বুঝে test-এ ধরো। একবারে rewrite করলে আসল নিয়ম হারিয়ে যেতে পারে।

Fun fact

A useful artifact for this lesson: Choose a narrow function or adapter that can be replaced independently.

Hands-on challenge

Choose ONE stack; you do not need to build all three. Junior foundation: Write characterisation tests for a small legacy function before changing it. Senior extension: Plan a staged migration, compatibility window, reconciliation and rollback. Evidence: save the input, expected behaviour, actual result and one failure or limitation. Junior means basic coding and Git, not zero programming experience.

More resources

Open interactive version (quiz + challenge) Download the whole course as PDF / Word

Lock it in

← Back to course: AI-Assisted Software Engineering