Legacy Reporting Module Modernisation
A monthly sales report is produced by a tangled legacy module that nobody dares touch, yet it must be changed to add a new data source without altering any figures finance already relies on.
- Estimated effort
- 20-28 hours
- Difficulty
- Advanced
- Stack
- Any mainstream language, SQL or CSV data, Test framework, Version control
- Related program
- 02 · Existing Project Engineering
Project brief
Harbourline Supplies is a fictional wholesaler. Its monthly sales report is generated by a single large module, written over many years, that reads orders, applies discounts, rounds figures and writes a CSV for finance. It has no tests, mixes SQL, rules and formatting, and contains duplicated code. Finance, represented by Tomasz Berg, uses the numbers for month-end and will not accept any change in them, including rounding differences.
You will be given, or should create, a small legacy-style module and a synthetic dataset of a few hundred orders with awkward cases: refunds, orders spanning month boundaries, missing regions and discounts that stack. Your task is to restructure the module so that the data access, the business rules and the CSV formatting are separate, making it possible to add a second data source later, without changing a single character of the output for the supplied datasets.
Work in small steps. Before you change anything, capture the current outputs for several datasets as golden files and write tests that compare against them. Where you see behaviour that looks wrong, record it and keep it unless the client agrees otherwise. You may use AI assistance to explain code, propose a refactoring plan or draft tests. Log each prompt and decision, and verify each result by running the comparison yourself.
Skills you will practise
- Reading an unfamiliar codebase and mapping its data flow
- Capturing current behaviour with characterisation and golden-master tests
- Refactoring in small, reversible steps
- Separating data access, rules and formatting
- Reviewing AI suggestions for behaviour changes
Deliverables
- A codebase orientation note with an entry point and data-flow map
- A set of golden-master output files and the tests that compare against them
- The refactored module, committed as small steps
- A change log showing evidence that outputs are unchanged at each step
- A list of suspected defects noticed but deliberately not fixed
- A prompt log with review notes
Prerequisites
- Comfortable reading code you did not write
- Experience writing automated tests
- Version control with small, meaningful commits
- Completed Reading an Unfamiliar Codebase, or equivalent experience
Helpful lessons: Reading an Unfamiliar Codebase, Mapping Entry Points and Data Flow, Sharing Code with an AI Assistant Safely, Writing a Codebase Orientation Guide.
Suggested architecture
The target structure separates three responsibilities that the legacy code mixes: a data source that returns plain records, a rules layer that computes figures, and a formatter that writes the CSV. A golden-master test harness sits around the whole pipeline.
| Component | Responsibility |
|---|---|
| Data source | Reads orders from the synthetic dataset and returns plain records, hiding where they come from. |
| Rules layer | Applies discounts, refunds and rounding exactly as the legacy code did, as pure functions. |
| Formatter | Turns computed rows into the CSV text, including column order, quoting and number formatting. |
| Report runner | Wires the data source, rules and formatter together and exposes the same entry point as before. |
| Golden-master harness | Runs the report for each stored dataset and compares the result with the stored output byte for byte. |
| Legacy module (kept during migration) | Remains runnable until the end so old and new outputs can be compared directly. |
- Dataset
Synthetic orders
- Data source
Plain records
- Rules layer
Figures and rounding
- Formatter
CSV text
- Compare
Byte-for-byte check against the golden file
Milestones and tasks
- 1
Orient yourself
Understand what the module does and how data moves through it.
- Find the entry points and trace the main data flow from input to CSV.
- List the rules you can see, and mark the ones you do not understand.
- Write a one-page orientation note for a new colleague.
- 2
Capture current behaviour
Build a safety net before changing anything.
- Create or choose several datasets, including edge cases such as refunds and month boundaries.
- Run the legacy module and store the outputs as golden files.
- Write tests that compare new runs with the golden files and fail on any difference.
- Make sure the tests are deterministic: fix dates, ordering and any random values.
- 3
Separate the layers
Extract data access, rules and formatting in small safe steps.
- Plan the steps, each of which can be undone and checked.
- Extract the formatter first, then the rules, then the data source.
- After every step run the golden tests and commit only when they pass.
- Record in the change log which tests you ran and what they showed.
- 4
Handle the awkward cases
Deal with suspicious behaviour without changing outputs.
- List behaviour that looks like a defect, with the dataset that shows it.
- Keep current behaviour and add a test that names it as intended-for-now.
- Write the questions you would ask finance before changing any of it.
- 5
Prove it and hand over
Show that nothing changed and that the new structure is easier to extend.
- Run a final full comparison for all datasets.
- Demonstrate adding a second data source stub without touching the rules.
- Write the handover note and complete the submission checklist.
Tests to write
- T1For each stored dataset, the refactored report matches the golden file byte for byte.
- T2A refund within the same month reduces the month total exactly as the legacy module did.
- T3An order spanning a month boundary is assigned to the same month as before.
- T4Stacked discounts produce the same rounded figures as the legacy module.
- T5An order with a missing region appears in the same row or group as before.
- T6An empty dataset produces the same output (header only or empty) as before.
- T7The report is deterministic: running it twice gives identical output.
- T8The rules layer functions can be tested without a database or a file system.
- T9A stub data source with the same record shape produces the same output as the original source for the same orders.
Submission checklist
Assessment rubric
| Criterion | Weight | Strong | Adequate | Needs work |
|---|---|---|---|---|
| Understanding of the existing code | 15% | Accurate map of entry points and data flow; unknowns are marked rather than guessed. | Reasonable map with some unverified claims. | Description is vague or inaccurate. |
| Safety net | 25% | Golden files cover edge cases; tests are deterministic and fail on any change. | Golden tests exist but edge cases are thin or flaky. | No reliable comparison with the legacy behaviour. |
| Refactoring quality | 25% | Layers are cleanly separated, steps are small and reversible, and outputs are identical throughout. | Layers are separated but in large steps, or minor output differences were found and fixed. | Large rewrite; outputs changed or cannot be shown to match. |
| Judgement about behaviour | 15% | Suspected defects are listed and preserved, with questions for the client. | Some defects noticed; unclear whether they were preserved. | Behaviour changed silently, including 'fixes' no one asked for. |
| AI use, review and documentation | 20% | Every AI suggestion was checked by running the comparison; log and rationale are complete. | Log exists but verification is partial. | AI-generated changes accepted without comparison. |
- Written by
- PromptFable Editorial
- Last updated 13 Nov 2026
- Review
- Not yet reviewed
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.