帮助开发者将变量限定在最小作用域内,提升代码可读性与可维护性。
这是一个仅提供编程风格与变量作用域建议的提示型开源 Skill,材料中未见密钥需求、网络外发、本地执行或数据访问行为。整体风险较低,主要需留意其仓库社区采用度低、许可证未声明、维护状态未知带来的供应链不确定性。
材料明确注明无需密钥或环境变量,未见 OAuth、API token 或其他敏感凭证配置,也未描述凭证存储、传输或滥用场景。
材料注明无远程端点,且该 Skill 被判定为 prompt-only;未见调用外部 API、上传用户数据或向第三方主机发送内容的描述。
从 README 看其内容是代码风格原则说明,不是可执行代理或自动化工具;未声明会在本机启动进程、执行脚本、调用 shell 或请求系统权限。
未描述读取、写入或枚举本地文件、项目目录、数据库、剪贴板或其他资源的能力;作为提示型 Skill,其数据访问面看起来不存在或极小。
正面因素是 GitHub 开源、内容可审计;但仓库 star 为 0、维护状态未知、许可证未声明,说明生态验证和持续维护证据较弱,存在一定供应链不确定性,不过基于当前材料未见明确恶意红旗。
复制安装指令,让 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 对话,快速找回事实、决策与上下文线索。
帮助用户用接口封装实现细节,从业务层面设计与理解系统
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
为工程师生成分步实施计划,帮助在陌生代码库中快速落地任务。
帮助你在实施前先比较2到3种方案,选出更优设计与执行路径。
帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。
帮助开发者用先写测试再实现代码的方式提升质量与可维护性。
帮助开发者识别并避免常见测试反模式,提升单元测试可维护性与可靠性。
帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。