一键导入
refactor-decompose-function
For long functions: break into smaller pieces, extract helper functions, reduce nesting, improve testability and readability.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
For long functions: break into smaller pieces, extract helper functions, reduce nesting, improve testability and readability.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Conducts iterative deep research on any topic using web search, progressive exploration, and structured synthesis. Use when asked for comprehensive research, deep investigation, thorough analysis, or multi-source exploration of any topic. Triggers: research, investigate, deep dive, comprehensive analysis, explore thoroughly, find everything about.
For cross-cutting concerns: add behavior without modifying functions, caching, timing, logging, validation wrappers.
For performance work: measure before changing, profile to find bottlenecks, compare before and after.
For symbolic computation: ASTs, mathematical expressions, code that manipulates code structure, expression transformations.
For ordered processing: A* search, Dijkstra, event simulation, task scheduling. Efficient min/max extraction with heap-based queue.
For dynamic programming: overlapping subproblems, recursive solutions with repeated computations, memoization to avoid redundant work.
| name | refactor-decompose-function |
| description | For long functions: break into smaller pieces, extract helper functions, reduce nesting, improve testability and readability. |
Extract cohesive chunks into named helper functions. Each function should do one thing.
# BEFORE: Long, hard to test
def process_data(data):
# Validate
if not data:
raise ValueError("Empty data")
if not all(isinstance(x, int) for x in data):
raise TypeError("Non-integer data")
# Transform
result = []
for x in data:
if x > 0:
result.append(x * 2)
else:
result.append(0)
# Summarize
total = sum(result)
avg = total / len(result)
return {'data': result, 'total': total, 'average': avg}
# AFTER: Decomposed, each part testable
def process_data(data):
validate(data)
transformed = transform(data)
return summarize(transformed)
def validate(data):
if not data:
raise ValueError("Empty data")
if not all(isinstance(x, int) for x in data):
raise TypeError("Non-integer data")
def transform(data):
return [x * 2 if x > 0 else 0 for x in data]
def summarize(data):
total = sum(data)
return {'data': data, 'total': total, 'average': total / len(data)}
# Spelling correction (spell.py) - beautifully decomposed
def correction(word):
"""Most probable spelling correction for word."""
return max(candidates(word), key=P)
def candidates(word):
"""Generate possible spelling corrections for word."""
return known([word]) or known(edits1(word)) or known(edits2(word)) or [word]
def known(words):
"""The subset of words that appear in the dictionary."""
return set(w for w in words if w in WORDS)
def edits1(word):
"""All edits one edit away from word."""
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R) > 1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
def edits2(word):
"""All edits two edits away from word."""
return (e2 for e1 in edits1(word) for e2 in edits1(e1))
f(g(h(x))) beats deeply nested code