一键导入
refactor
TDD Refactor Phase - Improve code while keeping tests green using Simple Design Rules and APP mass
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
TDD Refactor Phase - Improve code while keeping tests green using Simple Design Rules and APP mass
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | refactor |
| description | TDD Refactor Phase - Improve code while keeping tests green using Simple Design Rules and APP mass |
You are now in the Refactor Phase of TDD. Follow these instructions to improve the code while keeping tests green.
Guide developers through the Refactor phase of TDD by helping them:
Total Mass = (constants x 1) + (bindings x 1) + (invocations x 2) +
(conditionals x 4) + (loops x 5) + (assignments x 6)
5, "hello", true)amount, result)calculate(), Math.max())if, switch, ?:)for, forEach, map)x = 5, count++)Before anything else, evaluate the naming:
**Naming Evaluation**:
- Current name: `calculate`
- Function purpose: "adds numbers from an array"
- Question: Does "calculate" clearly reveal this intent?
- Assessment: Too generic - "calculate" could mean anything
- Recommendation: Rename to `sumNumbers` or keep if name fits
Decision: [Rename to X] or [Keep current name because Y]
Before making changes, calculate current code mass:
**Current Code Mass**:
function calculate(numbers: number[]): number {
return numbers.reduce((sum, num) => sum + num, 0);
}
Component Count:
- Constants: 1 (literal 0) = 1
- Bindings: 3 (numbers, sum, num) = 3
- Invocations: 2 (reduce, +) = 4
- Conditionals: 0 = 0
- Loops: 1 (reduce is iteration) = 5
- Assignments: 0 = 0
Total Mass: 13
Systematically evaluate each rule:
Potential improvements:
Potential improvements:
Potential improvements:
After refactoring, recalculate mass:
**Refactored Code Mass**:
[refactored code]
Component Count:
[detailed breakdown]
Total Mass: [new total]
Mass Change: [old mass] -> [new mass] (delta [difference])
Explain the refactoring outcome:
If Improvements Made:
**Refactoring Applied**:
- Naming: Renamed `calculate` to `sumNumbers` (better reveals intent)
- Mass: Reduced from 13 to 11 (removed conditional)
- Rule 2: Improved clarity with explaining variable
Benefits:
- Code now clearly states it sums numbers
- Reduced complexity (lower mass)
- More maintainable
If No Improvements Possible:
**Refactoring Evaluation**:
- Naming: `calculate` already clearly describes purpose
- Duplication: No duplicated code found
- Mass: Current implementation already minimal (mass: 13)
- Simplification: No unnecessary complexity
Reasoning:
Current implementation is already optimal because:
1. Name clearly reveals intent
2. No duplication exists
3. Mass is minimal for this functionality
4. No unnecessary abstractions
No refactoring performed - code is already clean.
Refactor Phase Complete:
**Refactoring**: [improvements made or "none possible"]
**Mass Change**: [before -> after] (if calculated)
**Tests**: All passing
Proceeding to the next test.
// Before (mass: 13)
function calc(nums: number[]): number {
return nums.reduce((s, n) => s + n, 0);
}
// After (mass: 13, clarity improved)
function sumNumbers(numbers: number[]): number {
return numbers.reduce((sum, num) => sum + num, 0);
}
Refactoring:
- Naming: Renamed for clarity (Rule 2)
- Mass unchanged: 13 -> 13
- Benefit: Better reveals intent
// Before (mass: 22, duplication)
function differsByOneLetter(a: string, b: string): boolean {
if (a.length !== b.length) return false;
let diffs = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) diffs++;
}
return diffs === 1;
}
function isAdjacent(a: string, b: string): boolean {
if (a.length !== b.length) return false;
let count = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) count++;
}
return count === 1;
}
// After (mass: reduced, no duplication)
const countDifferences = (a: string, b: string): number => {
if (a.length !== b.length) return Infinity;
return a.split('').reduce((count, char, i) =>
char !== b[i] ? count + 1 : count, 0
);
};
const differsByOneLetter = (a: string, b: string): boolean =>
countDifferences(a, b) === 1;
Refactoring:
- Removed duplication (Rule 3)
- Reduced mass
- Improved maintainability
// Current code (mass: 8)
function isEmpty(str: string): boolean {
return str.length === 0;
}
Evaluation:
- Naming: "isEmpty" clearly reveals intent
- Duplication: No duplication
- Mass: Already minimal (8)
- Simplification: No unnecessary complexity
No refactoring performed - code is already optimal.
Watch for these violations:
Your goal is to systematically improve code quality using established principles, measure improvements objectively with APP, and maintain transparency through comprehensive documentation.
Generates an experiment-overview snapshot of all research questions under research/_archive/. Invoke when a new point-in-time report across all RQs should be produced.
Use this skill to drive a research question (RQ) end-to-end: validate the RQ README, generate a fill batch-plan, start the Docker batch in the background, monitor progress, run aggregation, and propose findings updates. Trigger when the user says "RQ-N voranbringen", "run-rq", "fill RQ-N", "run RQ-N", "Forschungsfrage N starten", or names a specific RQ-N directory in research/.
Re-run the analysis pipeline on all runs matching an RQ, reaggregate metrics, and propose findings updates against the fresh data. Trigger when the user says "reanalyze RQ-N", "reanalyse RQ-N", "Runs neu analysieren", or wants to refresh metrics/findings after a pipeline change (analyze-run.sh fix, adapter added, ESLint config).
TDD Green Phase - Implement minimal code to make the failing test pass
TDD Red Phase - Activate ONE test from the test list and make it fail with explicit predictions
TDD Test List Phase - Create a comprehensive test list covering every example and rule from the specification