帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Naming Variables" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/naming-variables/SKILL.md 2. 保存为 ~/.claude/skills/naming-variables/SKILL.md 3. 装好后重载技能,告诉我可以用了
请检查这段代码中的变量命名,并把含糊不清的变量改成更准确、可读的名字,说明每个修改的理由: let d = new Date(); let x = users.filter(u => u.active); let n = x.length;
返回重命名后的代码,并逐项解释新变量名为何更贴合实际含义。
我在一个 JavaScript 项目里混用了缩写、拼音和不一致的命名方式。请根据语义清晰、风格统一的原则,给出变量命名规范,并示范如何修改下面的变量名:userNm、shijian、tmpVal、list2。
给出简明命名规范,并提供更统一、更具描述性的变量名建议。
我在写电商订单逻辑,需要为这些变量取名:用户首次下单时间、可退款金额、已发货商品数量、是否满足免邮条件。请给出简洁但完整准确的英文变量名,并说明命名思路。
输出一组适合业务代码使用的英文变量名,并附上命名依据说明。
The most important consideration in naming a variable is that the name fully and accurately describes the entity the variable represents.
Core principle: A variable and its name are essentially the same thing. The goodness or badness of a variable is largely determined by its name.
Name quality = Design quality. If you struggle to name something well, that's a warning sign about the design.
Always use when:
Warning signs you need better names:
x, x1, x2, temp, data, info, valProblem-oriented (what it represents) not solution-oriented (how it's implemented):
✅ Good: employeeData, printerReady, totalRevenue
❌ Bad: inputRec, bitFlag, calcVal
The bad names describe computing concepts (input, record, bit, calculation). Good names describe the problem domain (employee, printer, revenue).
Optimal: 10-16 characters average
| Too Long | Too Short | Just Right |
|---|---|---|
numberOfPeopleOnTheUsOlympicTeam | n, np, ntm | numTeamMembers, teamMemberCount |
numberOfSeatsInTheStadium | n, ns, nsisd | numSeatsInStadium, seatCount |
maximumNumberOfPointsInModernOlympics | m, mp, max | teamPointsMax, pointsRecord |
Exception: Short names OK for very limited scope (loop index i in 3-line loop)
i, j acceptableShort name says: "I'm a scratch value with limited scope, don't look for me elsewhere"
Effective technique: state in words what the variable represents. Often that statement IS the best name.
Example:
runningTotal or checkTotalvelocity, trainVelocity, velocityInMphcurrentDate, todaysDatePut qualifiers like Total, Sum, Average, Max, Min, Count at the END:
✅ Good: revenueTotal, expenseAverage, pointsMax, customerCount
❌ Bad: totalRevenue mixed with revenueTotal (inconsistent)
Why:
revenueTotal, revenueAverage, revenueMaxtotalRevenue vs revenueTotal - pick one convention)Exception: Num is ambiguous
numCustomers (count of all customers)customerNum (specific customer number)Count for total, Index for specific: customerCount, customerIndexDon't be clever. Use the ordinary, obvious words:
✅ currentDate, employeeName, accountBalance
❌ cd, empNm, acctBal
Programmers sometimes overlook using ordinary words, which is often the easiest solution.
Names that are too general can be used for anything = not informative:
❌ Bad: x, temp, data, info, value, variable
✅ Good: discriminant, oldRoot, errorMessage, customerInfo, totalRevenue
Generic names acceptable ONLY for very limited scope.
Standard patterns for consistency and clarity:
…
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。