Skills

Coding Standards

TypeScript、JavaScript、React、Node.js 开发的通用编码标准、最佳实践和模式。

Install

npx skills add xu-xiang/everything-claude-code-zh --skill coding-standards -g -a claude-code -y
Category
Engineering
Updated
8mo ago
License
MIT
Context cost
Mediumabout 2,318 tokens
Runs scripts
No, instructions only

What it does

TypeScript、JavaScript、React、Node.js 开发的通用编码标准、最佳实践和模式。

编码标准与最佳实践

适用于所有项目的通用编码标准。

代码质量原则

1. 可读性优先 (Readability First)

  • 代码被阅读的次数远多于编写的次数
  • 明确的变量名和函数名
  • 优先考虑自描述代码而非注释
  • 保持格式一致

2. KISS (Keep It Simple, Stupid)

  • 采用能运行的最简单方案
  • 避免过度设计
  • 避免过早优化
  • 易于理解 > 巧妙的代码

3. DRY (Don't Repeat Yourself)

  • 将通用逻辑提取到函数中
  • 创建可复用组件
  • 在模块间共享工具函数 (Utility functions)
  • 避免复制粘贴式编程

4. YAGNI (You Aren't Gonna Need It)

  • 不要预先构建不需要的功能
  • 避免臆测性的泛化
  • 仅在必要时增加复杂度
  • 从简单开始,按需进行重构 (Refactoring)

TypeScript/JavaScript 标准

变量命名

// ✅ 推荐:描述性名称
const marketSearchQuery = 'election'
const isUserAuthenticated = true
const totalRevenue = 1000

// ❌ 错误:不明确的名称
const q = 'election'
const flag = true
const x = 1000

函数命名

// ✅ 推荐:动词-名词模式
async function fetchMarketData(marketId: string) { }
function calculateSimilarity(a: number[], b: number[]) { }
function isValidEmail(email: string): boolean { }

// ❌ 错误:不明确或仅使用名词
async function market(id: string) { }
function similarity(a, b) { }
function email(e) { }

不变性模式 (Immutability Patterns)(重要)

// ✅ 始终使用展开运算符 (Spread operator)
const updatedUser = {
  ...user,
  name: 'New Name'
}

const updatedArray = [...items, newItem]

// ❌ 绝不直接修改(Mutate)
user.name = 'New Name'  // 错误
items.push(newItem)     // 错误

错误处理 (Error Handling)

// ✅ 推荐:全面的错误处理
async function fetchData(url: string) {
  try {
    const response = await fetch(url)

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`)
    }

    return await response.json()
  } catch (error) {
    console.error('Fetch failed:', error)
    throw new Error('Failed to fetch data')
  }
}

// ❌ 错误:缺乏错误处理
async function fetchData(url) {
  const response = await fetch(url)
  return response.json()
}

Async/Await 最佳实践

// ✅ 推荐:尽可能并行执行
const [users, markets, stats] = await Promise.all([
  fetchUsers(),
  fetchMarkets(),
  fetchStats()
])
Read the whole SKILL.md on GitHub

More in Engineering