Extract a Function¶
Time: 5 minutes | Tools: extract_code | Tier: Community
Learn how to surgically extract functions by name—no line number guessing.
What You'll Learn¶
- How
extract_codefinds code by name - Extracting functions, classes, and methods
- Including dependencies automatically
Prerequisites¶
- Completed Analyze Your First File
- Sample code from previous tutorial
The Problem with Line Numbers¶
Traditional approach (fragile):
This breaks if:
- Someone adds a comment above line 7
- The file is reformatted
- Imports are added at the top
The Solution: Extract by Name¶
With Code Scalpel:
"Extract the divide function from calculator.py"
The AI calls:
{
"tool": "extract_code",
"parameters": {
"file_path": "calculator.py",
"target_type": "function",
"target_name": "divide"
}
}
Step 1: Extract a Function¶
Ask your AI:
"Show me the divide function from calculator.py"
Response¶
{
"data": {
"target_type": "function",
"target_name": "divide",
"code": "def divide(a, b):\n \"\"\"Divide a by b.\"\"\"\n return a / b",
"line_start": 7,
"line_end": 9,
"dependencies": [],
"token_estimate": 25
},
"tier_applied": "community",
"duration_ms": 18
}
Notice:
- Exact code extracted
- Line numbers provided (but you didn't need them)
- Dependencies listed
- Token cost estimated
Step 2: Extract a Class¶
Try extracting the Calculator class:
"Extract the Calculator class from calculator.py"
{
"tool": "extract_code",
"parameters": {
"file_path": "calculator.py",
"target_type": "class",
"target_name": "Calculator"
}
}
Response¶
{
"data": {
"target_type": "class",
"target_name": "Calculator",
"code": "class Calculator:\n \"\"\"Simple calculator class.\"\"\"\n \n def __init__(self):\n self.history = []\n \n def compute(self, operation, *args):\n \"\"\"Perform a calculation.\"\"\"\n result = add(*args) if operation == \"add\" else divide(*args)\n self.history.append(result)\n return result",
"line_start": 11,
"line_end": 21,
"dependencies": ["add", "divide"],
"token_estimate": 85
}
}
Note the dependencies field shows this class uses add and divide.
Step 3: Extract a Method¶
Extract a specific method from a class:
"Extract the compute method from the Calculator class"
{
"tool": "extract_code",
"parameters": {
"file_path": "calculator.py",
"target_type": "method",
"target_name": "Calculator.compute"
}
}
Step 4: Include Context¶
If you need the dependencies too:
"Extract the Calculator class with all its dependencies"
{
"tool": "extract_code",
"parameters": {
"file_path": "calculator.py",
"target_type": "class",
"target_name": "Calculator",
"include_context": true
}
}
This includes the add and divide functions along with the class.
Target Types¶
| Type | Example | Use Case |
|---|---|---|
function | divide | Extract a function |
class | Calculator | Extract a class |
method | Calculator.compute | Extract a method |
Why This Matters¶
Token Efficiency¶
Instead of sending 1000 lines to the AI, extract just what's needed:
| Approach | Tokens |
|---|---|
| Full file read | ~2000 |
| Extract function | ~25 |
| Savings | 98.75% |
Precision¶
The AI works with exactly the code it needs—no confusion from unrelated code.
Reliability¶
Extraction by name works even when:
- Files are reformatted
- Comments are added
- Code is moved around
Try It Yourself¶
Exercise 1: Your Own Code¶
Extract a function from your own project:
"Extract the [function_name] function from [your_file.py]"
Exercise 2: React Components¶
If you have React code:
"Extract the Button component from Button.tsx"
Code Scalpel supports JSX and TSX.
Exercise 3: Check Dependencies¶
"Extract [function] and show what it depends on"
See what other code your function needs.
Common Errors¶
"Symbol not found"
The function name doesn't exist. Check spelling or use analyze_code first.
"File not found"
The file path is wrong. Use validate_paths to check.
Key Takeaways¶
extract_codefinds code by name, not line numbers- It automatically identifies dependencies
- Token-efficient: only get what you need
- Works with functions, classes, and methods
- Supports Python, JS, TS, Java, JSX, TSX
Next Tutorial¶
Now that you can extract code, learn to update it safely: