帮助开发者将变量限定在最小作用域内,提升代码可读性与可维护性。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Localizing Variables" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/localizing-variables/SKILL.md 2. 保存为 ~/.claude/skills/localizing-variables/SKILL.md 3. 装好后重载技能,告诉我可以用了
请检查这段代码中的变量声明,把变量移动到最小可见作用域,并尽量在首次使用附近初始化,同时说明这样修改的原因。
返回作用域更小的重构代码,并附上每处变量调整的简要说明。
分析下面函数中哪些局部变量声明过早或存活时间过长,重写代码以缩短变量生命周期,并指出潜在风险是否降低。
给出优化后的函数版本,并说明哪些变量被延后声明或更贴近使用位置。
请作为代码评审者,针对这段代码从变量局部化角度提出具体修改建议,标出不必要的提前声明、过大的作用域和可改进写法。
输出结构化评审意见,指出问题位置并给出可执行的修改建议。
The Principle of Proximity: Keep related actions together. Declare variables in the smallest scope possible, initialize them close to where they're first used, and keep all references to a variable close together.
Core principle: Minimize the window of vulnerability. The smaller the scope and the closer the references, the less can go wrong and the easier code is to understand.
Goal: Reduce what you must keep in mind at any one time.
Apply to every variable you declare:
Warning signs:
How widely visible a variable is:
{} or indented block (smallest)Rule: Start with smallest scope. Expand only if necessary.
Distance between successive references to a variable:
a = 0 # First reference
b = 0 # 1 line between references to a
c = 0 # 2 lines between references to a
a = b + c # Second reference
# Span of a: 2 lines
Goal: Minimize span. Keep references close together.
Total statements between first and last reference:
recordIndex = 0 # Line 2 - first reference
# ... 24 lines of other code ...
recordIndex += 1 # Line 28 - last reference
# Live time: 28 - 2 + 1 = 27 statements
Goal: Minimize live time. Reduce window of vulnerability.
Keep related actions together:
❌ Bad (declarations far from use):
def process_data():
# All declarations at top
index = 0
total = 0
done = False
result = []
# 20 lines later...
while index < count:
index += 1
# 30 lines later...
while not done:
if total > threshold:
done = True
# 40 lines later...
result.append(final_value)
return result
Live times: index=25, total=35, done=35, result=40. Average: 34 lines.
✅ Good (declare close to use):
def process_data():
# Declare index right before loop that uses it
index = 0
while index < count:
index += 1
# Declare total and done right before loop that uses them
total = 0
done = False
while not done:
if total > threshold:
done = True
# Declare result right before use
result = []
result.append(final_value)
return result
Live times: index=3, total=5, done=5, result=2. Average: 4 lines.
Improvement: 34 → 4 average live time (8.5x better)
Languages like C++, Java, Python, JavaScript allow this:
❌ Bad:
def calculate_report():
total = 0 # Declared at top
count = 0
average = 0.0
# 10 lines later...
total = sum(values)
count = len(values)
average = total / count if count > 0 else 0.0
✅ Good:
def calculate_report():
# 10 lines of other work...
# Declare right before use
total = sum(values)
count = len(values)
average = total / count if count > 0 else 0.0
In languages supporting block scope, use it:
# Process old data - variables scoped to this block
{
old_data = get_old_data()
…
先用伪代码梳理方案与迭代思路,再高效转成可执行代码。
帮助开发者用早返回或表驱动方式简化嵌套条件分支,提升代码可读性。
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助用户检索过往 Claude Code 对话,快速找回事实、决策与上下文线索。
帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。
帮助开发者用先写测试再实现代码的方式提升质量与可维护性。
帮助开发者识别并避免常见测试反模式,提升单元测试可维护性与可靠性。
帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。