dont-repeat-yourself
Use when writing similar code in multiple places. Use when copy-pasting code. Use when making the same change in multiple locations.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when writing similar code in multiple places. Use when copy-pasting code. Use when making the same change in multiple locations.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when writing tests. Use when test structure is unclear. Use when arrange/act/assert phases are mixed.
Use when designing or modifying APIs. Use when adding breaking changes. Use when clients depend on API stability.
Use when implementing authentication. Use when storing passwords. Use when asked to store credentials insecurely.
Use when same data is fetched repeatedly. Use when database queries are slow. Use when implementing caching without invalidation strategy.
Use when tempted to use class inheritance. Use when creating class hierarchies. Use when subclass needs only some parent behavior.
Use when acquiring multiple locks. Use when operations wait for each other. Use when system hangs without crashing.
| name | dont-repeat-yourself |
| description | Use when writing similar code in multiple places. Use when copy-pasting code. Use when making the same change in multiple locations. |
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.