帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Single Purpose Variables" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/single-purpose-variables/SKILL.md 2. 保存为 ~/.claude/skills/single-purpose-variables/SKILL.md 3. 装好后重载技能,告诉我可以用了
请检查这段代码,找出同时承担多种含义或在不同阶段复用的变量,并重构为单一用途变量。请解释每个修改的原因,并给出重构后的代码。
返回问题变量清单、重构理由,以及更清晰的重构后代码。
请为团队编写一份“单一用途变量”编码规范,说明什么是混合职责变量、为什么要避免、命名建议、反例与正例,并给出可执行的审查清单。
输出一份结构清晰的团队规范文档,包含原则、示例和代码审查要点。
请从代码评审角度分析这段代码,重点检查变量是否一义一用,是否存在隐藏状态、复用旧变量承载新含义、或临时变量职责膨胀的问题,并提出修改建议。
得到一份面向评审的诊断意见,指出风险点并给出可落地的改进建议。
Each variable should represent exactly ONE thing. No reusing for different purposes. No hidden meanings.
Core principle: If variable represents count sometimes and error other times, use two variables.
From baseline, agents use special values to indicate errors:
❌ Hybrid coupling (baseline):
def process_file_pages(filename):
try:
pages_processed = 0 # Count (integer purpose)
# ... processing ...
return pages_processed
except:
return -1 # Error flag (boolean purpose as -1)
Problem: pages_processed represents TWO things:
This is hybrid coupling: Variable moonlights as different type.
✅ Separate concerns:
def process_file_pages(filename):
try:
pages_processed = 0
# ... processing ...
return (True, pages_processed) # Success, count
except Exception as e:
return (False, str(e)) # Failure, error message
Or raise exception:
def process_file_pages(filename):
# Let exceptions propagate - no hybrid variable needed
pages_processed = 0
# ... processing (raises on error) ...
return pages_processed # Always a count, never an error
❌ What agents naturally do:
page_count = 15 # Number of pages
page_count = -1 # Wait, now it means error!
customer_id = 1234 # Customer number
customer_id = 500001 # Wait, > 500000 means delinquent (subtract 500000)!
bytes_written = 1024 # Bytes written
bytes_written = -5 # Wait, negative means disk drive number!
✅ Separate variables:
page_count = 15
processing_failed = True # Separate boolean for error state
customer_id = 1234
is_delinquent = False # Separate boolean for status
bytes_written = 1024
disk_drive = 5 # Separate variable for drive number
Good reuse (same purpose, same meaning):
# ✅ GOOD: total_sales used for multiple related calculations
total_sales = sum(sales)
average = total_sales / len(sales) # Same value, same meaning
percentage = (total_sales / target) * 100 # Same value, same meaning
Bad reuse (different purposes):
# ❌ BAD: temp reused for unrelated purposes
temp = sqrt(b*b - 4*a*c) # Discriminant
root1 = (-b + temp) / (2*a)
# ...
temp = root1 # Now reused for swapping (different purpose!)
root1 = root2
root2 = temp
✅ Separate variables:
discriminant = sqrt(b*b - 4*a*c) # Clear purpose
root1 = (-b + discriminant) / (2*a)
# ...
old_root = root1 # Clear purpose (swapping)
root1 = root2
root2 = old_root
| Violation | Example | Fix |
|---|---|---|
| Hybrid coupling | count=-1 means error | Separate: count + error_occurred boolean |
| Hidden meanings | id > 500000 means delinquent | Separate: id + is_delinquent |
| Temp reuse | temp for discriminant, then swapping | Use: discriminant, old_root |
| State changes | Variable means X, then means Y | Two variables with clear names |
temp, result, value for unrelated purposesFix: Create separate variable with clear name for each purpose.
From Code Complete:
From baseline:
-1 to indicate error in count variable (hybrid coupling)With this skill: Separate variables for separate purposes.
…
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。