بنقرة واحدة
code-refactor
Refactor code for improved quality without changing behavior
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Refactor code for improved quality without changing behavior
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Discover team members, delegate tasks, and track progress to completion
Prepare structured meeting agendas and pre-reads from task board, artifacts, and team context
Classify, prioritize, and route incoming incidents based on severity, category, and affected components
Classify incoming requests and route to the appropriate specialist agent
How to communicate with other agents on the system. Use when you need to ask questions, share information, or coordinate.
Compare options against weighted criteria with scored matrix, sensitivity analysis, and quantified recommendation
| name | code-refactor |
| description | Refactor code for improved quality without changing behavior |
Use this skill when the task calls for improving code quality, readability, or maintainability without changing external behavior.
Before changing anything, understand what the code does:
cd ~/workspace
# Read the target file(s) thoroughly
cat src/target-module.js
# Understand how it is used — find all callers
rg "require.*target-module" --type js
rg "import.*target-module" --type js
rg "target-module" --type js -l
# Run tests and save output as baseline
npm test 2>&1 | tee /tmp/baseline-tests.txt
BASELINE_EXIT=$?
echo "Baseline exit code: $BASELINE_EXIT"
# Count passing tests
grep -cE '(PASS|ok |passed|✓)' /tmp/baseline-tests.txt || true
If there are no tests, note this and be extra careful -- consider writing tests first.
Look for these specific code smells:
TARGET="src/"
# Long functions (>40 lines)
awk '/^(function|const.*=>|def |async function)/{name=$0; start=NR} NR-start==40{print FILENAME":"start": Long function: "name}' $TARGET*.js $TARGET*.py 2>/dev/null
# Duplicated code blocks (approximate)
awk 'NR>1{if($0==prev && length($0)>20) print FILENAME":"NR": Duplicate line: "$0; prev=$0}' $TARGET*.js $TARGET*.py 2>/dev/null
# Deep nesting (>4 levels)
rg "^(\s{16,}|\t{4,})\S" $TARGET --line-number | head -20
# Unclear variable names (single letters, except loop vars)
rg "\b(let|const|var)\s+[a-z]\s*=" $TARGET --type js | grep -v "for\s*(" | head -20
# Missing error handling
rg "\.catch\(\s*\(\s*\)\s*=>" $TARGET --type js # empty catch
rg "except:\s*$" $TARGET --type py # bare except
rg "catch\s*\(.*\)\s*\{\s*\}" $TARGET --type js # empty catch block
# Console.log used as error handling
rg "catch.*console\.(log|error)" $TARGET --type js | head -10
Work in small increments. After each change, re-run tests:
Step A — Extract function:
# Make the change
# ... edit the file ...
# Verify tests still pass
npm test 2>&1
echo "Exit code after extract: $?"
Step B — Rename for clarity:
# Rename variable/function across all files
OLD_NAME="processData"
NEW_NAME="parseAndValidateInput"
rg "$OLD_NAME" -l | while read f; do
sed -i "s/$OLD_NAME/$NEW_NAME/g" "$f"
done
# Verify tests still pass
npm test 2>&1
echo "Exit code after rename: $?"
Step C — Reduce nesting:
# Apply early returns / guard clauses
# ... edit the file ...
# Verify tests still pass
npm test 2>&1
echo "Exit code after de-nesting: $?"
Step D — Remove duplication:
# Extract common code into shared function
# ... edit the file ...
# Verify tests still pass
npm test 2>&1
echo "Exit code after dedup: $?"
Refactoring rules:
# If you find a bug, log it
cat >> ~/notes.md <<EOF
## Bug Found During Refactor (task $TASK_ID)
- File: src/module.js:42
- Description: Off-by-one in loop boundary
- Impact: Last item in array is skipped
- Note: Not fixed — behavior preservation required during refactor
EOF
# Run full test suite
npm test 2>&1 | tee /tmp/refactor-tests.txt
REFACTOR_EXIT=$?
# Compare with baseline
echo "Baseline exit: $BASELINE_EXIT, Refactor exit: $REFACTOR_EXIT"
diff <(grep -E '(PASS|FAIL|ok|not ok)' /tmp/baseline-tests.txt | sort) \
<(grep -E '(PASS|FAIL|ok|not ok)' /tmp/refactor-tests.txt | sort)
# Check no functional changes leaked in
git diff --stat
git add -A
git commit -m "Refactor: extract validation into helper functions
- Extract validateInput() from processRequest (was 80 lines, now 25)
- Rename 'x' to 'requestPayload' for clarity
- Remove duplicated error formatting (now in formatError())
- No behavior changes — all existing tests pass unchanged"