clean-typescript-general
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGeneral Clean Code Principles
通用整洁代码原则
Critical Rules
核心规则
G1: No Duplicated Knowledge
Every piece of knowledge has one authoritative representation.
ts
// Bad - duplication
const taxRate = 0.0825;
const caTotal = subtotal * 1.0825;
const nyTotal = subtotal * 1.07;
// Good - single source of truth
const TAX_RATES: Record<string, number> = { CA: 0.0825, NY: 0.07 };
function calculateTotal(subtotal: number, state: string): number {
return subtotal * (1 + TAX_RATES[state]);
}G2: No Obscured Intent
Don't be clever. Be clear.
ts
// Bad - what does this do?
return ((x & 0x0f) << 4) | (y & 0x0f);
// Good - obvious intent
return packCoordinates(x, y);G3: Replace Magic Values with Named Constants
ts
// Bad
if (elapsedTime > 86400) {
// ...
}
// Good
const SECONDS_PER_DAY = 86400;
if (elapsedTime > SECONDS_PER_DAY) {
// ...
}G4: Functions Should Do One Thing
If you can extract another function, your function does more than one thing.
G5: Delete Dead Code
Delete unused branches, helpers, exports, and unreachable code. Git preserves history; dead code makes future design decisions harder to read.
G1: 无重复知识
每一处知识都有唯一的权威表述。
ts
// Bad - duplication
const taxRate = 0.0825;
const caTotal = subtotal * 1.0825;
const nyTotal = subtotal * 1.07;
// Good - single source of truth
const TAX_RATES: Record<string, number> = { CA: 0.0825, NY: 0.07 };
function calculateTotal(subtotal: number, state: string): number {
return subtotal * (1 + TAX_RATES[state]);
}G2: 意图清晰明确
不要耍小聪明,要清晰直白。
ts
// Bad - what does this do?
return ((x & 0x0f) << 4) | (y & 0x0f);
// Good - obvious intent
return packCoordinates(x, y);G3: 用命名常量替换魔法值
ts
// Bad
if (elapsedTime > 86400) {
// ...
}
// Good
const SECONDS_PER_DAY = 86400;
if (elapsedTime > SECONDS_PER_DAY) {
// ...
}G4: 函数应单一职责
如果能从中提取出另一个函数,说明你的函数承担了不止一项职责。
G5: 删除无用代码
删除未使用的分支、辅助函数、导出项和不可达代码。Git会保留历史记录;无用代码会增加未来阅读和决策的难度。
Review Checklist
审查检查清单
When reviewing AI-generated code, verify:
- No duplicated knowledge (G1)
- Clear intent, no magic values (G2, G3)
- Functions do one thing (G4)
- Dead code removed (G5)
审查AI生成的代码时,请验证:
- 无重复知识(G1)
- 意图清晰,无魔法值(G2、G3)
- 函数单一职责(G4)
- 已删除无用代码(G5)