I went straight for a command registry pattern, basically a map from command strings to handler functions, and pulled the parsing into its own pure function.
Start by clarifying the existing behavior and test coverage, then propose a refactor that separates parsing, command dispatch, and side effects into distinct modules. Emphasize incremental, test-driven changes that preserve the public API and keep the existing test green.
Pro tip: Demonstrate that you treat the existing test as a contract: run it first, add characterization tests for edge cases, and refactor in small commits so you can always revert. This shows maturity and reduces risk.
Read the function and its test, then write down the current behavior for each command and edge cases. Add characterization tests if coverage is thin.
List the mixed concerns (parsing, validation, command handling, side effects) and duplicated logic. Group them into potential modules or functions.
Propose a clear separation: a parser that returns a command object, a dispatcher that routes to handlers, and handlers that perform side effects. Define interfaces between them.
Extract functions step by step, running tests after each change. Use techniques like extract method, replace conditional with polymorphism, and dependency injection for testability.
Confirm all tests pass, then discuss trade-offs like added abstraction vs. simplicity, and how the new structure improves testability and future extensibility.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.