Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Pin behavior before a refactor
card 3 of 6
How to pin behaviour
builds on: Refactors that quietly change behavior, What makes a test able to fail
Tests that record what existing code currently does are called **characterization tests**. To write them:
1. Pick edge inputs: empty, null/`None`, duplicates, invalid input (which error?), input whose order matters, calls with side effects.
2. Run the **old** code. Paste its actual result as the expected value, even if it looks wrong, and add a note saying so.
3. Check that all of them are green on the old code. Commit them on their own.
4. Tell the agent: refactor, and do not edit these tests.
5. Any red afterwards means behaviour changed. Tell: the diff edits a pinned test. Later cost: the behaviour change ships hidden behind a green suite.
Terms on this card
- refactor
- Restructuring code to make it cleaner or simpler while keeping its observable behaviour exactly the same for every caller.
- pin
- To record a piece of current behaviour as a test's expected value, so that any change to it turns the test red.
- the diff
- The change under review in unified diff format: '-' lines removed, '+' lines added, unprefixed lines are unchanged context.
- edge inputs
- Inputs at the limits of what code handles, including unusual or invalid ones: empty values, null, duplicates, malformed data, maximum sizes.
- side effects
- Anything a call does besides returning a value, such as writing a file, changing shared state or sending a request.
Lesson goal: Write characterization tests that capture what existing code currently does before letting an agent refactor it. Use them to catch unintended behaviour changes that the agent's own new tests would miss. ·
the whole lesson