Update Code Safely¶
Time: 10 minutes | Tools: update_symbol, rename_symbol | Tier: Community
Learn how to modify code safely with automatic backups and tracking.
What You'll Learn¶
- Replace functions/classes safely
- Rename symbols across files
- Automatic backup creation
- When updates fail and why
Prerequisites¶
- Completed Extract a Function
- Sample calculator.py file
The Problem with Manual Edits¶
When AI assistants edit code manually:
- Line numbers can be wrong
- Context may have changed
- Surrounding code might break
- No backup if something goes wrong
The Solution: Safe Symbol Updates¶
Code Scalpel's update_symbol:
- Finds the symbol by name (not line numbers)
- Creates a backup automatically
- Replaces only that symbol
- Preserves surrounding code
Step 1: Update a Function¶
Let's fix the divide function to handle zero:
"Update the divide function to check for division by zero"
The AI:
-
Extracts current code:
-
Creates improved version:
-
Applies the update:
{ "tool": "update_symbol", "parameters": { "file_path": "calculator.py", "target_type": "function", "target_name": "divide", "new_code": "def divide(a, b):\n \"\"\"Divide a by b.\n \n Raises:\n ValueError: If b is zero.\n \"\"\"\n if b == 0:\n raise ValueError(\"Cannot divide by zero\")\n return a / b" } }
Response¶
{
"data": {
"file_path": "calculator.py",
"target_name": "divide",
"status": "updated",
"backup_path": "calculator.py.bak",
"lines_changed": {
"before": [7, 9],
"after": [7, 14]
}
},
"tier_applied": "community",
"duration_ms": 35
}
Step 2: Verify the Update¶
Check the file to see the change:
# calculator.py (after update)
def add(a, b):
"""Add two numbers."""
return a + b
def divide(a, b):
"""Divide a by b.
Raises:
ValueError: If b is zero.
"""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
# ... rest of file unchanged
Notice:
- Only
dividechanged addfunction untouchedCalculatorclass untouched- Formatting preserved
Step 3: Rename a Symbol¶
Now let's rename compute to calculate:
"Rename the compute method to calculate"
{
"tool": "rename_symbol",
"parameters": {
"file_path": "calculator.py",
"target_type": "method",
"target_name": "Calculator.compute",
"new_name": "calculate"
}
}
Response¶
{
"data": {
"old_name": "compute",
"new_name": "calculate",
"file_path": "calculator.py",
"status": "renamed",
"usages_updated": 1,
"backup_path": "calculator.py.bak"
},
"tier_applied": "community",
"duration_ms": 28
}
About Backups¶
Every update creates a backup:
| Original | Backup |
|---|---|
calculator.py | calculator.py.bak |
utils/helper.py | utils/helper.py.bak |
To disable backups (not recommended):
{
"tool": "update_symbol",
"parameters": {
"file_path": "calculator.py",
"target_type": "function",
"target_name": "divide",
"new_code": "...",
"create_backup": false
}
}
When Updates Fail¶
Symbol Not Found¶
{
"error": {
"code": "SYMBOL_NOT_FOUND",
"message": "Function 'divde' not found in calculator.py",
"suggestions": ["divide"]
}
}
Code Scalpel suggests corrections for typos!
Syntax Error in New Code¶
{
"error": {
"code": "SYNTAX_ERROR",
"message": "Invalid Python syntax in new_code",
"line": 3,
"details": "expected ':'"
}
}
The update is rejected—original file untouched.
File Not Found¶
{
"error": {
"code": "FILE_NOT_FOUND",
"message": "File 'calculater.py' not found",
"suggestions": ["calculator.py"]
}
}
Update vs Rename¶
| Use Case | Tool |
|---|---|
| Change implementation | update_symbol |
| Change name only | rename_symbol |
| Change both | rename_symbol then update_symbol |
Try It Yourself¶
Exercise 1: Add Error Handling¶
"Update the add function to validate that inputs are numbers"
Exercise 2: Rename a Class¶
"Rename the Calculator class to MathCalculator"
Exercise 3: Update a Class¶
"Update the Calculator class to track operation counts"
Key Takeaways¶
update_symbolreplaces code safely by namerename_symbolrenames and tracks usages- Backups are created automatically
- Syntax errors are caught before writing
- Typos are corrected with suggestions
Next Tutorial¶
Now that you can update code, learn to find security issues: