Extract a Function
Workflow for safely extracting code into a reusable function
CLAUDE.md
When extracting code into a function:
- Identify the block of code to extract. It should have a single, clear purpose.
- Verify the code block has tests covering its behavior. If not, add tests first.
- Identify all inputs the block needs (parameters) and its output (return value).
- Create the new function with a descriptive name, clear parameters, and return type.
- Replace the original code block with a call to the new function.
- Run the tests to verify behavior is unchanged.
- Check if the extracted function is called from anywhere else — if so, replace those copies too.
Copy this workflow into your CLAUDE.md or agent config file so your agent follows this process automatically.