code-simplify
paulrberg/agent-skills
當使用者要求「簡化程式碼」、「整理程式碼」、「為清晰起見進行重構」、「降低複雜度」、「提升可讀性」、 「讓這段程式碼更容易維護」,或是要求簡化最近修改過的程式碼時,應使用此技能。
...展開全部關於code-simplify
「code-simplify 」這項技能旨在簡化現有程式碼,同時保留其執行時行為、公開介面、副作用及運作意圖。它能協助開發者降低複雜度、提升可讀性,並使程式碼更易於維護,且不會引入功能上的變更。 此技能著重於安全且針對性的重構,而非廣泛的重寫,確保輸入、輸出、錯誤行為以及外部依賴的合約保持穩定。它適用於程式碼變得難以理解、過度嵌套、重複或不必要地複雜的情況。
此技能會根據使用者提供的目標、近期修改的檔案或儲存庫變更,來確定適當的範圍。在進行變更之前,它會分析周邊上下文以建立行為基準線。 簡化過程透過結構化的流程進行,包括扁平化控制流程、提升命名清晰度、減少重複、將密集的資料轉換重構為更清晰的步驟,並在適當情況下改善類型或合約的清晰度。此工作流程強調微小且可逆的編輯,並遵循現有的專案規範、語法檢查工具、格式化工具及測試。 安全規則會防止任何改變行為的變更,例如將同步 API 轉換為異步 API、移除運作上的安全防護措施,或在未經明確指示的情況下變更外部介面。
這項技能對於開發人員、維護者以及正在處理不斷演進的程式碼庫的團隊而言非常有用,他們希望在不改變功能的前提下提升程式碼品質。 常見的應用情境包括:清理近期修改過的程式碼、為提升可讀性而進行重構、減輕複雜函式的認知負荷、簡化嵌套邏輯,以及為長期維護做好程式碼準備。此技能在以儲存庫為基礎的開發工作流程中尤為寶貴,因為在該類流程中,驗證與行為穩定性是重要的要求。
常見問題
何時該使用這項技能?
當您希望簡化程式碼、提升可讀性、降低複雜度、清理近期修改過的程式碼,或在不改變程式行為的前提下使程式碼更易於維護時,即可使用此技能。
這項技能會改變應用程式的行為或公開 API 嗎?
不會。此技能旨在保留執行時行為、公開合約、副作用、輸入、輸出,以及外部所依賴的錯誤語義。
這項技能是否需要 Git 儲存庫?
是的。工作流程會先驗證儲存庫的上下文。若當前目錄並非 Git 儲存庫,流程將停止並要求從 Git 儲存庫中執行。
此技能如何決定要修改哪些檔案?
若用戶已提供目標,則優先使用該目標;否則,該技能會優先處理會話中已修改的檔案,若無此類檔案,則可能回退至未提交的已追蹤檔案及未追蹤檔案。
執行的重構類型是否有任何限制?
是的。此技能會避免進行會改變行為的修改、大規模重寫、不必要的抽象化、同步與非同步 API 之間的轉換,以及移除日誌記錄、遙測、保護機制、重試或其他對運作至關重要的程式碼。
Code Simplify
Objective
Simplify code while preserving behavior, public contracts, and side effects. Favor explicit code and local clarity over clever or compressed constructs.
Arguments
- Paths, patterns, a commit/range, or a scope phrase: used in Scope Resolution step 2.
--no-report: Skip the full user-facing report and return terse working notes for the caller.--no-verify: Skip verification because a parent orchestrator will verify the final result separately.- Default: verify touched behavior and present the full report.
Scope Resolution
Resolve scope once, then treat the result as fixed for the rest of the run.
- Verify repository context:
git rev-parse --git-dir. If this fails, stop and tell the user to run from a git repository. - If the request names targets — file paths/patterns, a commit/range, a natural-language subset (e.g. "the parser changes"), or a
resolved-scopefenced block with one repo-relative path per line — scope is exactly those targets. Map natural-language subsets to concrete paths before continuing. - Otherwise, scope is only session-modified files: files created or edited earlier in this session. Do not include other uncommitted changes.
- If there are no session-modified files, or earlier conversation history is not visible in this context, fall back to all uncommitted files, running each command once:
- tracked:
git diff --name-only --diff-filter=ACMR - untracked:
git ls-files --others --exclude-standard - combine both lists and de-duplicate.
- tracked:
- Exclude generated/low-signal files unless explicitly requested: lockfiles, minified bundles, build outputs, vendored code.
- If scope resolves to zero files, report that and stop.
- Emit the scope as a fenced code block tagged
resolved-scope, one repo-relative path per line. The block is authoritative: do not re-run scope commands or revisit exclusions afterward.
Operating Rules
- Preserve runtime behavior exactly. Keep inputs, outputs, side effects, and error behavior stable.
- Prefer project conventions over personal preferences. Infer conventions from existing code, linters, formatters, and tests.
- Make small, reversible edits. Avoid broad rewrites when targeted simplifications solve the problem.
- Call out uncertainty immediately when behavior may change.
Workflow
1) Determine Scope
- Apply the Scope Resolution section.
2) Build a Behavior Baseline
- Read surrounding context, not only changed lines.
- Identify invariants that must not change:
- function signatures and exported APIs
- state transitions and side effects
- persistence/network behavior
- user-facing messages and error semantics where externally relied on
- Note available verification commands (lint, tests, typecheck).
3) Apply Simplification Passes (in this order)
- Control flow:
- Flatten deep nesting with guard clauses and early returns.
- Replace nested ternaries with clearer conditionals.
- Naming and intent:
- Rename ambiguous identifiers when local context supports safe renaming.
- Separate mixed concerns into small helpers with intent-revealing names.
- Duplication:
- Remove obvious duplication.
- Abstract only when at least two real call sites benefit and the abstraction reduces cognitive load.
- Data shaping:
- Break dense transform chains into named intermediate steps when readability improves.
- Keep hot-path performance characteristics stable unless improvement is explicit and measured.
- Type and contract clarity:
- Add or tighten type annotations when they improve readability and safety without forcing broad churn.
- Preserve external interfaces unless asked to change them.
4) Enforce Safety Constraints
- Do not convert sync APIs to async (or reverse) unless explicitly requested.
- Do not alter error propagation strategy unless behavior remains equivalent and verified.
- Do not remove logging, telemetry, guards, or retries that encode operational intent.
- Do not collapse domain-specific steps into generic helpers that hide intent.
5) Verify
Skip when --no-verify is set. Otherwise verify per the Verification section below.
6) Report
Produce the Report section below.
Simplification Heuristics
- Prefer explicit local variables over nested inline expressions when it reduces cognitive load.
- Prefer one clear branch per condition over compact but ambiguous condition trees.
- Keep function length manageable, but do not split purely for line count.
- Keep comments that explain intent, invariants, or non-obvious constraints.
- Remove comments that restate obvious code behavior.
- Optimize for the next maintainer's comprehension time, not minimum character count.
Anti-Patterns
- Do not perform speculative architecture rewrites.
- Do not introduce framework-wide patterns while simplifying a small local change.
- Do not replace understandable duplication with opaque utility layers.
- Do not bundle unrelated cleanups into one patch.
Verification
Run the narrowest checks that validate touched behavior:
- formatter/lint on touched files
- targeted tests for touched modules
- typecheck when relevant
Run broader checks only when risk warrants it. Name every skipped check and why.
Report
Skip when --no-report is set; return terse working notes instead: touched scope, key simplifications, residual risks.
Use these section headings, in this order. Omit sections that do not apply — do not number them and do not leave gaps or placeholders.
Scope
Files and regions changed.
Simplifications
One sentence per meaningful change, focused on the readability or maintainability gain. Confirm behavior-preservation assumptions explicitly.
Verification
Commands run and outcomes, including skipped checks.
Residual Risks
One line per risk: Assumed <assumption>; if wrong, <what breaks>; check via <command or inspection>. Plain language — expand or gloss domain-specific terms. Include questions that need a user decision, phrased directly. Write None. when there are none.
Stop Conditions
Stop and ask for direction when:
- simplification requires changing public API/contracts.
- behavior parity cannot be confidently verified.
- the code appears intentionally complex due to domain constraints.
- the requested scope implies a larger redesign rather than simplification.





首頁
