247: Machine-Coding Review, Refactoring, Hardening, and Explanation
Learning outcomes
By the end of this lesson, you should be able to:
- explain and apply a code review pass to a realistic implementation;
- explain and apply an accessibility pass to a realistic implementation;
- explain and apply a failure pass to a realistic implementation;
- explain and apply a performance pass to a realistic implementation;
- explain and apply a testing pass to a realistic implementation.
Prerequisites and retrieval
This lesson builds on the earlier 01–06 foundation and the preceding lessons in this module. Before you read, retrieve one concrete example from an earlier project where one of these concerns showed up. That retrieval matters because the goal is not to memorize a set of labels. In a timed machine-coding exercise, you need to make a defensible decision while keeping the implementation readable, accessible, testable, and straightforward to extend under interview pressure.
Terminology
- Code review pass: Read the solution as a reviewer would. Trace one feature through the implementation, then look for ambiguous names, unnecessary abstractions, duplicated state, unsafe assertions, and hidden side effects.
- Accessibility pass: Complete the entire workflow with only a keyboard. Inspect accessible names and roles, visible focus, form errors, dialog focus, and status cues that do not depend on color alone.
- Failure pass: Deliberately force a network error, slow response, empty data, duplicate submission, invalid storage, and content at the boundary of the expected size.
- Performance pass: Use evidence to inspect obvious large-list, repeated-request, and rendering hotspots.
- Testing pass: Keep a small, reliable set of tests that proves the riskiest contracts. A test suite that is flaky does not provide dependable confidence.
- Explanation pass: Prepare a 5–10 minute walkthrough that covers requirements, architecture, state ownership, data flow, trade-offs, known limitations, and what you would do next with more time.
Mental model
Treat Machine-Coding Review, Refactoring, Hardening, and Explanation as a design problem with observable inputs, outputs, invariants, and failure modes. The final stage is not just cosmetic cleanup. It turns a working demo into a submission that another developer can review: accidental complexity is removed, edge states are verified, and the architecture and trade-offs can be explained concisely. A strong implementation makes its assumptions visible, narrows uncertainty at its boundaries, and leaves enough evidence, such as tests, types, constraints, metrics, or diagrams, to show why the design is safe.
A useful sequence for both interviews and production work is:
requirement -> constraints -> model -> implementation -> failure analysis -> verification
Do not jump from a requirement straight to a library call. First state what must remain true. Then choose the mechanism that enforces that condition. This makes the implementation easier to review and gives you something concrete to verify when behavior is wrong.
Deep dive
1. Code review pass
Read the solution as a reviewer: trace one feature, identify naming ambiguity, unnecessary abstractions, duplicated state, unsafe assertions, and hidden side effects. Start with one user-visible path rather than scanning every file at once. Following that path often reveals whether state has a clear owner and whether each transformation happens in the layer that should own it.
Decision rule: Use a code review pass deliberately when it makes a contract or invariant easier to prove. If a change only reduces typing while hiding an assumption, prefer the more explicit design.
2. Accessibility pass
Complete the entire workflow with the keyboard, then inspect accessible names and roles, visible focus, form errors, dialog focus, and status cues that are understandable without color. A screen that looks correct with a mouse can still be difficult or impossible to use when focus order, error association, or dialog focus is wrong.
Decision rule: Use an accessibility pass deliberately when it makes a contract or invariant easier to prove. If it only reduces typing while hiding an assumption, prefer the more explicit design.
3. Failure pass
Force a network error, a slow response, empty data, a duplicate submission, invalid storage, and content at the boundary of the expected size. Recovery behavior should be intentional. For each failure, determine whether the user can retry, whether the current state is preserved, and whether the error is shown at the layer that can actually resolve it.
Decision rule: Use a failure pass deliberately when it makes a contract or invariant easier to prove. If it only reduces typing while hiding an assumption, prefer the more explicit design.
4. Performance pass
Use evidence to check obvious large-list, repeated-request, and rendering hotspots. Do not add memoization simply because it sounds like a performance improvement; remove premature memoization when it adds complexity without a measurable benefit. In a machine-coding exercise, a clear implementation with a known bottleneck is usually easier to improve than an opaque implementation optimized by guesswork.
Decision rule: Use a performance pass deliberately when it makes a contract or invariant easier to prove. If it only reduces typing while hiding an assumption, prefer the more explicit design.
5. Testing pass
Keep a small set of reliable tests that proves the riskiest contracts. Flaky tests reduce confidence more than missing low-value tests, because a failure no longer tells you whether the implementation or the test is wrong. Prioritize behavior that crosses boundaries and the edge cases most likely to violate an invariant.
Decision rule: Use a testing pass deliberately when it makes a contract or invariant easier to prove. If it only reduces typing while hiding an assumption, prefer the more explicit design.
6. Explanation pass
Prepare a 5–10 minute walkthrough covering requirements, architecture, state ownership, data flow, trade-offs, known limitations, and what you would do next with more time. The explanation should follow the decisions in the code. Be able to say not only what you chose, but which requirement or constraint made that choice reasonable.
Decision rule: Use an explanation pass deliberately when it makes a contract or invariant easier to prove. If it only reduces typing while hiding an assumption, prefer the more explicit design.
Worked example
Consider a timed machine-coding exercise that must stay readable, accessible, testable, and easy to extend under interview pressure. Begin by writing the requirement in one sentence. Then list the input and output contracts and identify which of the concepts above owns each failure mode. The useful distinction here is separation of concerns: parsing or validation belongs at the boundary; domain rules belong in the domain or service layer; persistence rules belong in the database or repository; and presentation rules belong in the client. Mixing these concerns can make a happy-path demo look shorter, but it makes edge cases much harder to reason about.
export async function handleRequest(input: unknown) {
const command = parseCommand(input);
const result = await service.execute(command);
return toHttpResponse(result);
}
The example is intentionally small. input is unknown at the boundary, so parseCommand is responsible for turning untrusted input into a valid command. The service then applies the domain behavior, and toHttpResponse translates the result for the transport layer. Keeping those jobs separate gives each layer a clear contract and makes a failure easier to locate.
Walk through at least four cases: the normal path, an empty or missing value, a duplicate, retry, or concurrent path where relevant, and a dependency failure. For every case, state which layer detects the problem and what the caller observes. That is the level of explanation expected in a senior code review or technical interview: connect the visible behavior to the layer that owns the decision.
Production perspective
Production correctness is broader than “the code works on my machine.” Ask how the design behaves during deploys, retries, partial failure, stale clients, concurrent requests, malformed data, schema changes, and high-cardinality workloads. Prefer explicit contracts, bounded resource usage, structured errors, and measurable behavior. Optimize only after you can identify the bottleneck or risk with evidence.
When the topic involves an external dependency, define a timeout and cancellation strategy. When it involves persistence, define transaction and consistency expectations. When it involves user-visible state, define loading, empty, error, stale, and success states. When it involves security, assume that the client can be modified and that network input is untrusted. Client-side checks can improve the experience, but they do not replace server-side authorization, validation, or persistence guarantees.
Guided lab
Take one finished challenge and perform a formal hardening review using accessibility, failure, performance, security, testing, and code-quality checklists. Produce a short reviewer-ready README and walkthrough.
Complete the lab with this discipline:
- Write the requirement and two non-requirements.
- List input, output, and error contracts before implementation.
- Implement the smallest correct vertical slice.
- Add at least one invalid-input test and one edge-case test.
- Instrument or inspect the behavior instead of guessing.
- Refactor one hidden assumption into an explicit type, constraint, function, or configuration.
- Explain one alternative design and why you did not choose it.
- Record a short “what would break at 10× scale?” note.
Edge cases and failure modes
- Code review pass: Test absence, malformed input, duplicates, ordering or concurrency where applicable, and behavior at the smallest and largest credible sizes.
- Accessibility pass: Test absence, malformed input, duplicates, ordering or concurrency where applicable, and behavior at the smallest and largest credible sizes.
- Failure pass: Test absence, malformed input, duplicates, ordering or concurrency where applicable, and behavior at the smallest and largest credible sizes.
- Performance pass: Test absence, malformed input, duplicates, ordering or concurrency where applicable, and behavior at the smallest and largest credible sizes.
- Testing pass: Test absence, malformed input, duplicates, ordering or concurrency where applicable, and behavior at the smallest and largest credible sizes.
These categories overlap on purpose. The same duplicate request may be a correctness problem, an accessibility problem if the user receives no feedback, a performance problem if it triggers redundant work, and a testing problem if the contract is unprotected. Review the behavior from the perspective of the pass you are performing, but keep the underlying system behavior consistent.
Common mistakes and debugging
- Solving the example instead of the requirement: a copied pattern can be syntactically correct but architecturally wrong.
- Hiding uncertainty with assertions, broad exception handlers, permissive schemas, or “temporary”
anyvalues. - Testing only the happy path and therefore discovering contracts only after integration.
- Optimizing before measuring, or selecting a scalable mechanism without a scale requirement.
- Letting client-side behavior stand in for server-side authorization, validation, or persistence guarantees.
For debugging, reproduce the smallest failing case, inspect the actual value or execution plan, trace the boundary where the invariant first becomes false, and fix the owning layer instead of adding a downstream patch. This keeps the symptom from being masked while the original invalid state continues to move through the system.
Interview questions
- What problem does Code review pass solve, and what trade-off or failure mode would make you choose a different approach?
- What problem does Accessibility pass solve, and what trade-off or failure mode would make you choose a different approach?
- What problem does Failure pass solve, and what trade-off or failure mode would make you choose a different approach?
- What problem does Performance pass solve, and what trade-off or failure mode would make you choose a different approach?
- What problem does Testing pass solve, and what trade-off or failure mode would make you choose a different approach?
Checkpoint
Without notes, explain Machine-Coding Review, Refactoring, Hardening, and Explanation to another developer in five minutes. Your explanation must include one invariant, one edge case, one production failure mode, and one alternative design. Then implement a small example without copying the lesson code.
Mastery checklist
- I can define the core terms precisely.
- I can choose a design from requirements instead of from habit.
- I can implement and test the normal path and edge cases.
- I can explain the runtime, storage, or complexity cost.
- I can identify which layer owns validation, errors, and recovery.
- I can compare at least two reasonable alternatives.
- I can explain how the design changes at larger scale or stricter reliability.
