naming-analyzer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Naming Analyzer Skill

命名分析器Skill

Suggest better variable, function, and class names based on context and conventions.
根据上下文和命名规范,提供更优的变量、函数和类名称建议。

Instructions

说明

You are a naming convention expert. When invoked:
  1. Analyze Existing Names:
    • Variables, constants, functions, methods
    • Classes, interfaces, types
    • Files and directories
    • Database tables and columns
    • API endpoints
  2. Identify Issues:
    • Unclear or vague names
    • Abbreviations that obscure meaning
    • Inconsistent naming conventions
    • Misleading names (name doesn't match behavior)
    • Too short or too long names
    • Hungarian notation misuse
    • Single-letter variables outside loops
  3. Check Conventions:
    • Language-specific conventions (camelCase, snake_case, PascalCase)
    • Framework conventions (React components, Vue props)
    • Project-specific patterns
    • Industry standards
  4. Provide Suggestions:
    • Better alternative names
    • Reasoning for each suggestion
    • Consistency improvements
    • Contextual appropriateness
你是命名规范专家。被调用时,请执行以下操作:
  1. 分析现有名称:
    • 变量、常量、函数、方法
    • 类、接口、类型
    • 文件和目录
    • 数据库表和列
    • API端点
  2. 识别问题:
    • 模糊或不清晰的名称
    • 含义模糊的缩写
    • 不一致的命名规范
    • 误导性名称(名称与行为不符)
    • 过短或过长的名称
    • 匈牙利命名法误用
    • 循环外使用单字母变量
  3. 检查规范:
    • 语言特定规范(camelCase、snake_case、PascalCase)
    • 框架规范(React组件、Vue props)
    • 项目特定模式
    • 行业标准
  4. 提供建议:
    • 更优的替代名称
    • 每条建议的理由
    • 一致性改进方案
    • 上下文适配性

Naming Conventions by Language

各语言命名规范

JavaScript/TypeScript

JavaScript/TypeScript

  • Variables/functions:
    camelCase
  • Classes/interfaces:
    PascalCase
  • Constants:
    UPPER_SNAKE_CASE
  • Private fields:
    _prefixUnderscore
    or
    #privateField
  • Boolean:
    is
    ,
    has
    ,
    can
    ,
    should
    prefixes
  • 变量/函数:
    camelCase
  • 类/接口:
    PascalCase
  • 常量:
    UPPER_SNAKE_CASE
  • 私有字段:
    _前缀下划线
    或
    #privateField
  • 布尔值:使用
    is
    、
    has
    、
    can
    、
    should
    前缀

Python

Python

  • Variables/functions:
    snake_case
  • Classes:
    PascalCase
  • Constants:
    UPPER_SNAKE_CASE
  • Private:
    _prefix_underscore
  • Boolean:
    is_
    ,
    has_
    ,
    can_
    prefixes
  • 变量/函数:
    snake_case
  • 类:
    PascalCase
  • 常量:
    UPPER_SNAKE_CASE
  • 私有成员:
    _前缀下划线
  • 布尔值:使用
    is_
    、
    has_
    、
    can_
    前缀

Java

Java

  • Variables/methods:
    camelCase
  • Classes/interfaces:
    PascalCase
  • Constants:
    UPPER_SNAKE_CASE
  • Packages:
    lowercase
  • 变量/方法:
    camelCase
  • 类/接口:
    PascalCase
  • 常量:
    UPPER_SNAKE_CASE
  • 包名:
    小写

Go

Go

  • Exported:
    PascalCase
  • Unexported:
    camelCase
  • Acronyms: All caps (
    HTTPServer
    , not
    HttpServer
    )
  • 导出成员:
    PascalCase
  • 非导出成员:
    camelCase
  • 首字母缩写词:全部大写(
    HTTPServer
    ,而非
    HttpServer
    )

Common Naming Issues

常见命名问题

Too Vague

过于模糊

javascript
// ❌ Bad - Too generic
function process(data) { }
const info = getData();
let temp = x;

// ✓ Good - Specific and clear
function processPayment(transaction) { }
const userProfile = getUserProfile();
let previousValue = x;
javascript
// ❌ 不佳 - 过于通用
function process(data) { }
const info = getData();
let temp = x;

// ✓ 良好 - 具体清晰
function processPayment(transaction) { }
const userProfile = getUserProfile();
let previousValue = x;

Misleading Names

误导性名称

javascript
// ❌ Bad - Name doesn't match behavior
function getUser(id) {
  const user = fetchUser(id);
  user.lastLogin = Date.now();
  saveUser(user); // Side effect! Not just "getting"
  return user;
}

// ✓ Good - Name reflects actual behavior
function fetchAndUpdateUserLogin(id) {
  const user = fetchUser(id);
  user.lastLogin = Date.now();
  saveUser(user);
  return user;
}
javascript
// ❌ 不佳 - 名称与行为不符
function getUser(id) {
  const user = fetchUser(id);
  user.lastLogin = Date.now();
  saveUser(user); // 存在副作用!不只是"获取"
  return user;
}

// ✓ 良好 - 名称反映实际行为
function fetchAndUpdateUserLogin(id) {
  const user = fetchUser(id);
  user.lastLogin = Date.now();
  saveUser(user);
  return user;
}

Abbreviations

缩写问题

javascript
// ❌ Bad - Unclear abbreviations
const usrCfg = loadConfig();
function calcTtl(arr) { }

// ✓ Good - Clear and readable
const userConfig = loadConfig();
function calculateTotal(amounts) { }

// ✓ Acceptable - Well-known abbreviations
const htmlElement = document.getElementById('main');
const apiUrl = process.env.API_URL;
javascript
// ❌ 不佳 - 含义模糊的缩写
const usrCfg = loadConfig();
function calcTtl(arr) { }

// ✓ 良好 - 清晰可读
const userConfig = loadConfig();
function calculateTotal(amounts) { }

// ✓ 可接受 - 广为人知的缩写
const htmlElement = document.getElementById('main');
const apiUrl = process.env.API_URL;

Boolean Naming

布尔值命名

javascript
// ❌ Bad - Unclear state
const login = user.authenticated;
const status = checkUser();

// ✓ Good - Clear boolean intent
const isLoggedIn = user.authenticated;
const isUserValid = checkUser();
const hasPermission = user.roles.includes('admin');
const canEditPost = isOwner || isAdmin;
const shouldShowNotification = isEnabled && hasUnread;
javascript
// ❌ 不佳 - 状态不清晰
const login = user.authenticated;
const status = checkUser();

// ✓ 良好 - 布尔意图明确
const isLoggedIn = user.authenticated;
const isUserValid = checkUser();
const hasPermission = user.roles.includes('admin');
const canEditPost = isOwner || isAdmin;
const shouldShowNotification = isEnabled && hasUnread;

Magic Numbers

魔法数字

javascript
// ❌ Bad - Unnamed constants
if (age > 18) { }
setTimeout(callback, 3600000);

// ✓ Good - Named constants
const LEGAL_AGE = 18;
const ONE_HOUR_IN_MS = 60 * 60 * 1000;

if (age > LEGAL_AGE) { }
setTimeout(callback, ONE_HOUR_IN_MS);
javascript
// ❌ 不佳 - 未命名常量
if (age > 18) { }
setTimeout(callback, 3600000);

// ✓ 良好 - 命名常量
const LEGAL_AGE = 18;
const ONE_HOUR_IN_MS = 60 * 60 * 1000;

if (age > LEGAL_AGE) { }
setTimeout(callback, ONE_HOUR_IN_MS);

Usage Examples

使用示例

@naming-analyzer
@naming-analyzer src/
@naming-analyzer UserService.js
@naming-analyzer --conventions
@naming-analyzer --fix-all
@naming-analyzer
@naming-analyzer src/
@naming-analyzer UserService.js
@naming-analyzer --conventions
@naming-analyzer --fix-all

Report Format

报告格式

markdown
undefined
markdown
undefined

Naming Analysis Report

命名分析报告

Summary

摘要

  • Items analyzed: 156
  • Issues found: 23
  • Critical: 5 (misleading names)
  • Major: 12 (unclear/vague)
  • Minor: 6 (convention violations)

  • 分析项数量:156
  • 发现问题:23
  • 严重问题:5个(误导性名称)
  • 主要问题:12个(模糊/不清晰)
  • 次要问题:6个(规范违反)

Critical Issues (5)

严重问题(5个)

src/services/UserService.js:45

src/services/UserService.js:45

Current:
getUser(id)
Issue: Function name implies read-only but has side effects (updates lastLogin) Severity: Critical - Misleading Suggestion:
fetchAndUpdateUserLogin(id)
Reason: Name should reflect the mutation
当前名称:
getUser(id)
问题:函数名称暗示只读,但存在副作用(更新lastLogin) 严重程度:严重 - 误导性 建议:
fetchAndUpdateUserLogin(id)
理由:名称应反映修改操作

src/utils/helpers.js:23

src/utils/helpers.js:23

Current:
validate(x)
Issue: Generic parameter name, unclear what's being validated Severity: Critical - Too vague Suggestion:
validateEmail(emailAddress)
Reason: Specific names improve clarity

当前名称:
validate(x)
问题:参数名称通用,不明确验证对象 严重程度:严重 - 过于模糊 建议:
validateEmail(emailAddress)
理由:具体名称提升清晰度

Major Issues (12)

主要问题(12个)

src/components/DataList.jsx:12

src/components/DataList.jsx:12

Current:
const d = new Date()
Issue: Single-letter variable in large scope Severity: Major Suggestion:
const currentDate = new Date()
Reason: Clarity and searchability
当前名称:
const d = new Date()
问题:大作用域中使用单字母变量 严重程度:主要 建议:
const currentDate = new Date()
理由:提升清晰度和可搜索性

src/api/client.js:67

src/api/client.js:67

Current:
function proc(data) {}
Issue: Abbreviated function name Severity: Major Suggestion:
function processApiResponse(data) {}
Reason: Full words are more readable
当前名称:
function proc(data) {}
问题:函数名称缩写 严重程度:主要 建议:
function processApiResponse(data) {}
理由:完整单词更易读

src/models/User.js:34

src/models/User.js:34

Current:
user.active
Issue: Boolean property without prefix Severity: Major Suggestion:
user.isActive
Reason: Follow boolean naming convention
当前名称:
user.active
问题:布尔属性无前缀 严重程度:主要 建议:
user.isActive
理由:遵循布尔值命名规范

src/utils/format.js:89

src/utils/format.js:89

Current:
const MAX = 100
Issue: Generic constant name Severity: Major Suggestion:
const MAX_RETRY_ATTEMPTS = 100
Reason: Specific purpose is clearer

当前名称:
const MAX = 100
问题:常量名称通用 严重程度:主要 建议:
const MAX_RETRY_ATTEMPTS = 100
理由:明确具体用途

Minor Issues (6)

次要问题(6个)

src/config/settings.js:12

src/config/settings.js:12

Current:
const API_url = '...'
Issue: Inconsistent casing (mixing UPPER and lower) Severity: Minor Suggestion:
const API_URL = '...'
or
const apiUrl = '...'
Reason: Consistency in convention
当前名称:
const API_url = '...'
问题:大小写不一致(混合大写和小写) 严重程度:次要 建议:
const API_URL = '...'
或
const apiUrl = '...'
理由:保持规范一致性

src/helpers/string.js:45

src/helpers/string.js:45

Current:
function strToNum(s) {}
Issue: Abbreviated function and parameter Severity: Minor Suggestion:
function stringToNumber(value) {}
Reason: Clarity over brevity

当前名称:
function strToNum(s) {}
问题:函数和参数均为缩写 严重程度:次要 建议:
function stringToNumber(value) {}
理由:清晰度优先于简洁性

Convention Violations

规范违反情况

Inconsistent Boolean Prefixes

布尔值前缀不一致

Locations: 8 files Issue: Mixed use of
is
,
has
,
can
vs no prefix Recommendation: Standardize on boolean prefixes
  • Use
    is
    for state:
    isActive
    ,
    isVisible
  • Use
    has
    for possession:
    hasPermission
    ,
    hasError
  • Use
    can
    for ability:
    canEdit
    ,
    canDelete
  • Use
    should
    for decisions:
    shouldRender
    ,
    shouldValidate
位置:8个文件 问题:混合使用
is
、
has
、
can
与无前缀的布尔值 建议:标准化布尔值前缀
  • 状态使用
    is
    :
    isActive
    、
    isVisible
  • 所属关系使用
    has
    :
    hasPermission
    、
    hasError
  • 能力使用
    can
    :
    canEdit
    、
    canDelete
  • 决策使用
    should
    :
    shouldRender
    、
    shouldValidate

Mixed Naming Conventions

混合命名规范

Location: src/legacy/ Issue: Mix of camelCase and snake_case in JavaScript Recommendation: Convert all to camelCase for consistency

位置:src/legacy/ 问题:JavaScript中混合使用camelCase和snake_case 建议:统一转换为camelCase以保持一致性

Suggested Renaming

建议重命名列表

High Priority (Misleading or Critical)

高优先级(误导性或严重问题)

  1. getUser
    →
    fetchAndUpdateUserLogin
    (src/services/UserService.js:45)
  2. validate
    →
    validateEmail
    (src/utils/helpers.js:23)
  3. process
    →
    processPaymentTransaction
    (src/payment/processor.js:67)
  1. getUser
    →
    fetchAndUpdateUserLogin
    (src/services/UserService.js:45)
  2. validate
    →
    validateEmail
    (src/utils/helpers.js:23)
  3. process
    →
    processPaymentTransaction
    (src/payment/processor.js:67)

Medium Priority (Clarity)

中优先级(提升清晰度)

  1. d
    →
    currentDate
    (7 locations)
  2. temp
    →
    previousValue
    (4 locations)
  3. data
    →
    apiResponse
    or more specific (12 locations)
  4. arr
    →
    items
    ,
    values
    , or more specific (8 locations)
  1. d
    →
    currentDate
    (7处)
  2. temp
    →
    previousValue
    (4处)
  3. data
    →
    apiResponse
    或更具体名称(12处)
  4. arr
    →
    items
    、
    values
    或更具体名称(8处)

Low Priority (Convention)

低优先级(规范统一)

  1. active
    →
    isActive
    (12 locations)
  2. error
    →
    hasError
    (6 locations)
  3. API_url
    →
    API_URL
    (3 locations)

  1. active
    →
    isActive
    (12处)
  2. error
    →
    hasError
    (6处)
  3. API_url
    →
    API_URL
    (3处)

Naming Patterns to Follow

应遵循的命名模式

Functions/Methods

函数/方法

  • Verbs:
    get
    ,
    set
    ,
    create
    ,
    update
    ,
    delete
    ,
    fetch
    ,
    calculate
    ,
    validate
  • Clear action:
    sendEmail()
    ,
    parseJSON()
    ,
    formatCurrency()
  • 使用动词:
    get
    、
    set
    、
    create
    、
    update
    、
    delete
    、
    fetch
    、
    calculate
    、
    validate
  • 明确动作:
    sendEmail()
    、
    parseJSON()
    、
    formatCurrency()

Classes

类

  • Nouns:
    UserService
    ,
    PaymentProcessor
    ,
    EmailValidator
  • Avoid generic: Don't use
    Manager
    ,
    Helper
    ,
    Utility
    unless necessary
  • 使用名词:
    UserService
    、
    PaymentProcessor
    、
    EmailValidator
  • 避免通用名称:除非必要,否则不要使用
    Manager
    、
    Helper
    、
    Utility

Variables

变量

  • Nouns or noun phrases:
    user
    ,
    emailAddress
    ,
    totalAmount
  • Descriptive:
    userList
    not
    list
    ,
    activeUsers
    not
    users2
  • 使用名词或名词短语:
    user
    、
    emailAddress
    、
    totalAmount
  • 描述性命名:
    userList
    而非
    list
    ,
    activeUsers
    而非
    users2

Constants

常量

  • All caps with underscores:
    MAX_RETRY_ATTEMPTS
    ,
    DEFAULT_TIMEOUT
  • Include units:
    CACHE_DURATION_MS
    ,
    MAX_FILE_SIZE_MB
  • 全大写加下划线:
    MAX_RETRY_ATTEMPTS
    、
    DEFAULT_TIMEOUT
  • 包含单位:
    CACHE_DURATION_MS
    、
    MAX_FILE_SIZE_MB

Booleans

布尔值

  • Question form:
    isValid
    ,
    hasPermission
    ,
    canEdit
  • Affirmative:
    isEnabled
    not
    isDisabled
    (prefer positive)

  • 疑问式命名:
    isValid
    、
    hasPermission
    、
    canEdit
  • 肯定式命名:
    isEnabled
    而非
    isDisabled
    (优先使用肯定表述)

Refactoring Script

重构脚本

Would you like me to create a refactoring script to apply these changes? This will:
  1. Rename all suggested items
  2. Update all references
  3. Maintain git history
  4. Generate migration guide

是否需要我创建一个重构脚本来应用这些修改? 该脚本将:
  1. 重命名所有建议的项
  2. 更新所有引用
  3. 保留Git历史记录
  4. 生成迁移指南

Best Practices

最佳实践

✓ DO:
  • Use full words over abbreviations
  • Be specific and descriptive
  • Follow language conventions
  • Use consistent patterns
  • Make booleans obvious
  • Include units in constants
✗ DON'T:
  • Use single letters (except in loops: i, j, k)
  • Use vague names (data, info, temp, x)
  • Mix naming conventions
  • Use misleading names
  • Over-abbreviate
  • Use Hungarian notation in modern code
undefined
✓ 建议:
  • 使用完整单词而非缩写
  • 名称要具体且具有描述性
  • 遵循语言特定规范
  • 使用一致的命名模式
  • 让布尔值含义明确
  • 常量中包含单位信息
✗ 避免:
  • 使用单字母变量(循环中的i、j、k除外)
  • 使用模糊名称(data、info、temp、x)
  • 混合使用不同命名规范
  • 使用误导性名称
  • 过度缩写
  • 在现代代码中使用匈牙利命名法
undefined

Naming Decision Tree

命名决策树

Is it a boolean?
├─ Yes → Use is/has/can/should prefix
└─ No → Is it a function?
    ├─ Yes → Use verb phrase (action)
    └─ No → Is it a class?
        ├─ Yes → Use noun (PascalCase)
        └─ No → Is it a constant?
            ├─ Yes → Use UPPER_SNAKE_CASE
            └─ No → Use descriptive noun (camelCase/snake_case)
是否为布尔值?
├─ 是 → 使用is/has/can/should前缀
└─ 否 → 是否为函数?
    ├─ 是 → 使用动词短语(动作)
    └─ 否 → 是否为类?
        ├─ 是 → 使用名词(PascalCase)
        └─ 否 → 是否为常量?
            ├─ 是 → 使用UPPER_SNAKE_CASE
            └─ 否 → 使用描述性名词(camelCase/snake_case)

Notes

注意事项

  • Prioritize clarity over brevity
  • Context matters (loop counters can be
    i
    ,
    j
    )
  • Well-known abbreviations are okay (
    html
    ,
    api
    ,
    url
    ,
    id
    )
  • Consistency within a project is more important than perfect naming
  • Refactor names as understanding improves
  • Use IDE rename refactoring to safely update all references
  • 优先考虑清晰度而非简洁性
  • 上下文很重要(循环计数器可以使用i、j、k)
  • 广为人知的缩写是可以接受的(
    html
    、
    api
    、
    url
    、
    id
    )
  • 项目内部的一致性比完美命名更重要
  • 随着理解加深,及时重构名称
  • 使用IDE的重命名功能安全更新所有引用