affaan-m/ECC · coding-standards · SKILL.md
编码规范
适用于TypeScript、JavaScript、React和Node.js开发的通用编码标准、最佳实践和模式。
这是公开 Agent Skill 的中文说明,由采集翻译或编辑润色,完整内容以上游 SKILL.md 为准。适用于 Cursor / Claude Code / Codex / Gemini。 打开上游 SKILL.md
安装
通用(npx skills)
npx skills add affaan-m/ECC -s coding-standards
Cursor
把该 Skill 复制到项目的 .cursor/skills/coding-standards/ 目录
Claude Code
/plugin marketplace add affaan-m/ECC
中文说明
# 编码标准与最佳实践
适用于所有项目的通用编码标准。
## 何时激活
* 开始新项目或新模块时
* 审查代码质量和可维护性时
* 重构现有代码以遵循约定时
* 强制执行命名、格式或结构一致性时
* 设置代码检查、格式化或类型检查规则时
* 引导新贡献者熟悉编码规范时
## 代码质量原则
### 1. 可读性优先
* 代码被阅读的次数远多于被编写的次数
* 清晰的变量和函数名
* 优先选择自文档化代码,而非注释
* 一致的格式化
### 2. KISS (保持简单,傻瓜)
* 采用能工作的最简单方案
* 避免过度设计
* 不要过早优化
* 易于理解 > 聪明的代码
### 3. DRY (不要重复自己)
* 将通用逻辑提取到函数中
* 创建可复用的组件
* 跨模块共享工具函数
* 避免复制粘贴式编程
### 4. YAGNI (你不会需要它)
* 不要预先构建不需要的功能
* 避免推测性泛化
* 仅在需要时增加复杂性
* 从简单开始,需要时再重构
## TypeScript/JavaScript 标准
### 变量命名
```typescript
// PASS: GOOD: Descriptive names
const marketSearchQuery = 'election'
const isUserAuthenticated = true
const totalRevenue = 1000
// FAIL: BAD: Unclear names
const q = 'election'
const flag = true
const x = 1000
```
### 函数命名
```typescript
// PASS: GOOD: Verb-noun pattern
async function fetchMarketData(marketId: string) { }
function calculateSimilarity(a: number[], b: number[]) { }
function isValidEmail(email: string): boolean { }
// FAIL: BAD: Unclear or noun-only
async function market(id: string) { }
function similarity(a, b) { }
function email(e) { }
```
### 不可变性模式 (关键)
```typescript
// PASS: ALWAYS use spread operator
const updatedUser = {
...user,
name: 'New Name'
}
const updatedArray = [...items, newItem]
// FAIL: NEVER mutate directly
user.name = 'New Name' // BAD
items.push(newItem) // BAD
```
### 错误处理
```typescript
// PASS: GOOD: Comprehensive 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')
}
}
// FAIL: BAD: No error handling
async function fetchData(url) {
const response = await fetch(url)
return response.json()
}
```
### Async/Await 最佳实践
```typescript
// PASS: GOOD: Parallel execution when possible
const [users, markets, stats] = await Promise.all([