重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
npx skills add https://github.com/yanko-belov/code-craft --skill dont-repeat-yourself系统中的每一条知识都必须有单一、明确的表示。
如果你发现自己两次编写相同的逻辑,请将其提取出来。重复是等待发生的错误。
NEVER duplicate logic. Extract and reuse.
没有例外:
如果你准备复制代码并修改它,请立即停止:
// ❌ 违规:重复的验证
function validateRegistrationEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function validateProfileEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); // 相同的逻辑!
}
// ✅ 正确:单一事实来源
function validateEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
// 随处复用
const isValidRegistration = validateEmail(regEmail);
const isValidProfile = validateEmail(profileEmail);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
如果修复一个错误需要更改多个位置,说明存在重复:
// ❌ 税收计算的错误需要在 3 个文件中更改
// cart.ts: const tax = price * 0.08;
// checkout.ts: const tax = price * 0.08;
// invoice.ts: const tax = price * 0.08;
// ✅ 单一事实来源
// tax.ts: export const calculateTax = (price: number) => price * TAX_RATE;
当代码"几乎相同"时,提取公共部分并将差异参数化:
// ❌ 违规:具有微小差异的相似函数
function formatUserName(user: User): string {
return `${user.firstName} ${user.lastName}`;
}
function formatAdminName(admin: Admin): string {
return `${admin.firstName} ${admin.lastName} (Admin)`;
}
// ✅ 正确:参数化
function formatName(person: { firstName: string; lastName: string }, suffix?: string): string {
const name = `${person.firstName} ${person.lastName}`;
return suffix ? `${name} (${suffix})` : name;
}
压力: "我直接复制这个然后修改一下"
回应: 复制会创建两个需要维护的地方。错误会分化。
行动: 首先提取共享逻辑,然后在两个地方都使用它。
压力: "这些函数几乎相同,但不完全一样"
回应: "几乎相同" = 提取公共部分,将差异参数化。
行动: 识别共享部分,提取它,将差异作为参数。
压力: "只有 3 行,不值得提取"
回应: 3 行代码重复 5 次 = 需要维护 15 行代码。错误会成倍增加。
行动: 即使是小的重复也要提取。给它们起个好名字。
压力: "先上线,以后再 DRY 化"
回应: 你不会的。重复会蔓延。现在 DRY 只需要 2 分钟。
行动: 在提交重复代码之前先提取。
如果你注意到以下任何一点,你即将违反 DRY 原则:
所有这些都意味着:提取到共享位置。
| 类型 | 示例 | 解决方案 |
|---|---|---|
| 代码 | 相同的函数体出现两次 | 提取函数 |
| 逻辑 | 相同的算法,不同的名称 | 提取并参数化 |
| 数据 | 相同的常量出现在多个文件中 | 集中管理常量 |
| 结构 | 重复相同的类结构 | 提取接口/基类 |
| 知识 | 业务规则出现在多个地方 | 单一事实来源 |
| 症状 | 行动 |
|---|---|
| 复制粘贴代码 | 提取共享函数 |
| 两次相同的验证 | 创建验证器模块 |
| 多个文件中有相同的常量 | 创建常量文件 |
| 相似的函数 | 提取 + 参数化 |
| 修复错误需要多处更改 | 整合到一个地方 |
| 借口 | 现实 |
|---|---|
| "复制更快" | 维护重复代码更慢。 |
| "它们略有不同" | 提取公共部分,将差异参数化。 |
| "只是几行代码" | 几行代码 × 多个地方 = 很多错误。 |
| "我稍后会重构" | 你不会的。现在就提取。 |
| "不同的上下文" | 相同的逻辑 = 相同的代码,无论上下文如何。 |
| "作为副本更易读" | 命名良好、提取出来的函数更易读。 |
一条知识。代码中的一个地方。
当编写相似代码时:停止,找到现有代码,如果需要则提取,然后复用。重复是维护噩梦的根源。
每周安装量
69
代码仓库
GitHub 星标数
7
首次出现
2026 年 1 月 22 日
安全审计
已安装于
codex62
github-copilot61
gemini-cli60
opencode60
cursor55
amp51
Every piece of knowledge must have a single, unambiguous representation in the system.
If you find yourself writing the same logic twice, extract it. Duplication is a bug waiting to happen.
NEVER duplicate logic. Extract and reuse.
No exceptions:
If you're about to copy code and modify it, STOP:
// ❌ VIOLATION: Duplicated validation
function validateRegistrationEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function validateProfileEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); // Same logic!
}
// ✅ CORRECT: Single source of truth
function validateEmail(email: string): boolean {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
// Reuse everywhere
const isValidRegistration = validateEmail(regEmail);
const isValidProfile = validateEmail(profileEmail);
If fixing a bug requires changing multiple locations, you have duplication:
// ❌ Bug in tax calculation requires changes in 3 files
// cart.ts: const tax = price * 0.08;
// checkout.ts: const tax = price * 0.08;
// invoice.ts: const tax = price * 0.08;
// ✅ Single source of truth
// tax.ts: export const calculateTax = (price: number) => price * TAX_RATE;
When code is "almost the same", extract the common part and parameterize the differences:
// ❌ VIOLATION: Similar functions with minor differences
function formatUserName(user: User): string {
return `${user.firstName} ${user.lastName}`;
}
function formatAdminName(admin: Admin): string {
return `${admin.firstName} ${admin.lastName} (Admin)`;
}
// ✅ CORRECT: Parameterized
function formatName(person: { firstName: string; lastName: string }, suffix?: string): string {
const name = `${person.firstName} ${person.lastName}`;
return suffix ? `${name} (${suffix})` : name;
}
Pressure: "I'll just copy this and modify it"
Response: Copying creates two places to maintain. Bugs will diverge.
Action: Extract shared logic first, then use it in both places.
Pressure: "The functions are almost the same but not quite"
Response: "Almost the same" = extract common part, parameterize differences.
Action: Identify what's shared, extract it, make differences parameters.
Pressure: "It's only 3 lines, not worth extracting"
Response: 3 lines duplicated 5 times = 15 lines to maintain. Bugs multiply.
Action: Extract even small duplications. Name them well.
Pressure: "Ship now, DRY it up later"
Response: You won't. Duplication spreads. DRY now takes 2 minutes.
Action: Extract before committing the duplication.
If you notice ANY of these, you're about to violate DRY:
All of these mean: Extract to a shared location.
| Type | Example | Solution |
|---|---|---|
| Code | Same function body twice | Extract function |
| Logic | Same algorithm, different names | Extract and parameterize |
| Data | Same constant in multiple files | Centralize constants |
| Structure | Same class shape repeated | Extract interface/base |
| Knowledge | Business rule in multiple places | Single source of truth |
| Symptom | Action |
|---|---|
| Copy-pasting code | Extract shared function |
| Same validation twice | Create validator module |
| Same constant in files | Create constants file |
| Similar functions | Extract + parameterize |
| Bug fix needs multiple changes | Consolidate to one place |
| Excuse | Reality |
|---|---|
| "It's faster to copy" | It's slower to maintain duplicates. |
| "They're slightly different" | Extract common, parameterize differences. |
| "Just a few lines" | Few lines × many places = many bugs. |
| "I'll refactor later" | You won't. Extract now. |
| "Different contexts" | Same logic = same code, regardless of context. |
| "More readable as copies" | Named, extracted functions are more readable. |
One piece of knowledge. One place in code.
When writing similar code: stop, find the existing code, extract if needed, reuse. Duplication is the root of maintenance nightmares.
Weekly Installs
69
Repository
GitHub Stars
7
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex62
github-copilot61
gemini-cli60
opencode60
cursor55
amp51
Perl 5.36+ 现代开发模式与最佳实践 | 构建健壮可维护应用程序指南
1,200 周安装