CommandOpen

Commands (extract-fn)

Extract a block of code into a well-named, reusable function with proper typing.

Extract a block of code into a well-named, reusable function with proper typing.

Steps

  1. Identify the code block to extract:
  • Accept file path with line range, or a description of the logic.
  • If no range given, detect the longest or most complex function and suggest extraction.
  1. Analyze the code block:
  • Variables read from outer scope become function parameters.
  • Variables written and used later become return values.
  • Side effects (I/O, mutations) are documented in the function's contract.
  1. Determine the function signature:
  • Name: verb + noun describing the action (e.g., calculateTotalPrice).
  • Parameters: typed, ordered by importance, grouped in object if more than 3.
  • Return type: explicit annotation.
  1. Extract the function:
  • Move the code block to the new function.
  • Add type annotations for parameters and return value.
  • Replace the original code with a function call.
  1. If the function is reusable across modules, move it to a shared utilities file.
  2. Run tests to verify behavior is preserved.

Format

Extracted: <functionName>
  From: <file>:<startLine>-<endLine>
  To: <destination file>
  Params: (<paramList>)
  Returns: <returnType>
  Tests: passing

Rules

  • The extraction must preserve identical behavior; run tests before and after.
  • Name functions based on purpose, not implementation.
  • Keep parameter count under 4; use an options object for more.
  • Add a doc comment explaining what the extracted function does.
  • Do not extract trivially short code (under 3 lines) unless it clarifies intent.

Source: https://github.com/rohitg00/awesome-claude-code-toolkit/blob/main/plugins/refactor-engine/commands/extract-fn.md

Author: rohitg00, Claude Code toolkit (rohitg00). License: Apache-2.0.

Shown here with formatting for the web. Keep the author and license notice when you reuse it.

Related entries

There are more like this in the members library

The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.