Skip to content

Commit 1c8a48b

Browse files
committed
chore: add local claude skills
1 parent 29a5888 commit 1c8a48b

19 files changed

Lines changed: 4765 additions & 0 deletions

File tree

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
---
2+
name: best-practices
3+
description: Universal software engineering best practices for any language or project. Use when programming, refactoring code, designing systems, reviewing code, or ensuring production-ready quality. Covers fundamental principles (SOLID, DRY, KISS, YAGNI), architectural patterns (SoC, SSOT, LoD, CQS, DbC), and operational reliability (ETC, PoLP, CoC, Idempotency). Apply when building new features, cleaning up codebases, improving maintainability, or ensuring code follows industry standards.
4+
---
5+
6+
# Universal Best Practices
7+
8+
This skill provides comprehensive guidance on software engineering best practices that apply across all programming languages and project types. Follow these principles to ensure clean, maintainable, production-ready code.
9+
10+
## Core Workflow
11+
12+
When writing or reviewing code:
13+
14+
1. Start with KISS and YAGNI - implement the simplest thing that solves the current need
15+
2. Apply SOLID principles to structure your classes and modules
16+
3. Ensure DRY by eliminating duplication
17+
4. Use SoC to separate concerns into distinct responsibilities
18+
5. Follow LoD to minimize coupling between components
19+
6. Apply remaining principles as relevant to your specific context
20+
21+
## Fundamental Principles
22+
23+
### SOLID
24+
25+
Five core object-oriented design principles that make code maintainable and flexible:
26+
27+
**S - Single Responsibility Principle (SRP)**
28+
- Each class/module should have only ONE reason to change
29+
- One class = one job or responsibility
30+
- Example: Separate `UserRepository` (data access) from `UserValidator` (validation logic)
31+
- Benefit: Changes to one responsibility don't affect unrelated code
32+
33+
**O - Open/Closed Principle (OCP)**
34+
- Open for extension, closed for modification
35+
- Add new functionality without changing existing code
36+
- Use interfaces, abstract classes, or inheritance to extend behavior
37+
- Example: Plugin systems, strategy patterns
38+
- Benefit: Add features without risking existing functionality
39+
40+
**L - Liskov Substitution Principle (LSP)**
41+
- Subtypes must be substitutable for their base types
42+
- Child classes shouldn't break parent class contracts
43+
- Example: If `Bird` has `fly()`, don't create `Penguin extends Bird` - penguins can't fly
44+
- Benefit: Polymorphism works correctly, no surprises
45+
46+
**I - Interface Segregation Principle (ISP)**
47+
- No class should implement methods it doesn't use
48+
- Many specific interfaces > one general interface
49+
- Example: Split `IWorker` into `IWorkable` and `IEatable` instead of forcing robots to implement `eat()`
50+
- Benefit: Lean interfaces, no unnecessary dependencies
51+
52+
**D - Dependency Inversion Principle (DIP)**
53+
- Depend on abstractions, not concrete implementations
54+
- High-level modules shouldn't depend on low-level modules
55+
- Both should depend on abstractions
56+
- Example: `PaymentProcessor` depends on `IPaymentGateway` interface, not concrete `StripeGateway`
57+
- Benefit: Loose coupling, easy to swap implementations
58+
59+
### DRY (Don't Repeat Yourself)
60+
61+
- Every piece of knowledge has ONE authoritative representation
62+
- Avoid duplicating logic, even if written differently
63+
- Example: Extract common tax calculation logic into single function instead of repeating across products
64+
- Benefit: Changes happen in one place, consistency guaranteed
65+
66+
### KISS (Keep It Simple, Stupid)
67+
68+
- Prefer the simplest solution that works
69+
- Avoid unnecessary complexity and cleverness
70+
- Simple code is easier to understand, maintain, and debug
71+
- Example: Use straightforward conditionals over complex nested ternaries
72+
- Benefit: Code is readable and less error-prone
73+
74+
### YAGNI (You Aren't Gonna Need It)
75+
76+
- Don't build features you don't need right now
77+
- No speculative functionality for hypothetical future needs
78+
- Focus on current requirements
79+
- Example: Don't build a recommendation engine if you only need product listing
80+
- Benefit: Less code to maintain, faster delivery
81+
82+
## Architectural Principles
83+
84+
### SoC (Separation of Concerns)
85+
86+
- Divide code into distinct sections, each addressing a separate concern
87+
- Each module focuses on ONE aspect of functionality
88+
- Example: Separate data layer, business logic, and presentation
89+
- Benefit: Changes to one concern don't affect others
90+
91+
### SSOT (Single Source of Truth)
92+
93+
- Every data element is mastered in only ONE place
94+
- All other references point to or derive from this source
95+
- Example: User profile data lives in one database table, not duplicated across systems
96+
- Benefit: No data inconsistency, one place to update
97+
98+
### LoD (Law of Demeter) - Principle of Least Knowledge
99+
100+
- Object should only talk to immediate friends
101+
- Don't access nested objects: `a.getB().getC().doSomething()` violates LoD
102+
- Instead: `a.doSomethingWithC()` where A delegates internally
103+
- **Rule**: Method can only call methods on:
104+
- Itself (`this`)
105+
- Its parameters
106+
- Objects it creates
107+
- Its direct properties
108+
- Benefit: Reduced coupling, easier refactoring
109+
110+
### CQS (Command Query Separation)
111+
112+
- Methods should either:
113+
- **Command**: Change state (return void)
114+
- **Query**: Return data (don't change state)
115+
- Never both in same method
116+
- Example: `getBalance()` reads only, `withdraw()` changes only
117+
- Exception: Sometimes `pop()` or `incrementAndGet()` violates this for practical reasons
118+
- Benefit: Predictable behavior, easier reasoning
119+
120+
### DbC (Design by Contract)
121+
122+
- Define explicit preconditions, postconditions, and invariants
123+
- **Preconditions**: What must be true before method runs
124+
- **Postconditions**: What must be true after method completes
125+
- **Invariants**: What must always be true for the object
126+
- Example: `withdraw(amount)` requires `amount > 0` (precondition) and `balance >= amount` (precondition), ensures `new_balance = old_balance - amount` (postcondition)
127+
- Benefit: Clear contracts, fail-fast validation
128+
129+
## Operational Principles
130+
131+
### ETC (Easier to Change)
132+
133+
- Optimize for change cost, not cleverness
134+
- Ask: "Will this be easy to modify later?"
135+
- Prefer composition over inheritance
136+
- Keep coupling loose, cohesion high
137+
- Benefit: Code adapts to evolving requirements
138+
139+
### PoLP (Principle of Least Privilege)
140+
141+
- Grant minimum permissions needed for the task
142+
- Processes run with minimal privileges
143+
- Functions access only what they need
144+
- Example: Read-only database connection for queries, no write access
145+
- Benefit: Reduced attack surface, limited blast radius
146+
147+
### CoC (Convention over Configuration)
148+
149+
- Use sensible defaults over explicit configuration
150+
- Follow standard conventions to reduce decisions
151+
- Only configure when deviating from convention
152+
- Example: Rails assumes `User` class maps to `users` table - no config needed
153+
- Benefit: Less boilerplate, faster development, easier onboarding
154+
155+
### Idempotency
156+
157+
- Operation produces same result whether executed once or multiple times
158+
- Safe to retry without side effects
159+
- Critical for distributed systems and APIs
160+
- Example: `PUT /users/123` with same data always results in same user state
161+
- Implementation: Use idempotency keys, request IDs, or natural idempotency
162+
- Benefit: Safe retries, fault tolerance, consistency
163+
164+
### POLA (Principle of Least Astonishment)
165+
166+
- System behavior should match user expectations
167+
- Code does what it looks like it does
168+
- No surprises or unexpected behavior
169+
- Example: `delete()` should delete, not archive
170+
- Benefit: Intuitive code, fewer bugs from misunderstanding
171+
172+
## Principle Application Priority
173+
174+
When multiple principles conflict:
175+
176+
1. **Safety first**: PoLP, DbC preconditions
177+
2. **Simplicity**: KISS, YAGNI
178+
3. **Maintainability**: DRY, SoC, SRP
179+
4. **Flexibility**: OCP, DIP, ETC
180+
5. **Consistency**: POLA, CoC
181+
182+
## Common Anti-Patterns to Avoid
183+
184+
- **Premature optimization**: Violates YAGNI and KISS
185+
- **God classes**: Violates SRP and SoC
186+
- **Deep nesting**: Violates LoD
187+
- **Copy-paste code**: Violates DRY
188+
- **Magic numbers/strings**: Violates SSOT and maintainability
189+
- **Mixing commands and queries**: Violates CQS
190+
- **Over-engineering**: Violates KISS and YAGNI
191+
192+
## Quick Reference
193+
194+
For detailed examples and language-specific implementations, see:
195+
- `references/solid-examples.md` - SOLID principle examples
196+
- `references/patterns-reference.md` - Design pattern applications of principles
197+
198+
## When Principles Conflict
199+
200+
**DRY vs KISS**: If abstraction becomes complex, duplicate similar code
201+
**YAGNI vs Future-proofing**: Build for today, refactor when tomorrow arrives
202+
**LoD vs Performance**: Sometimes direct access is needed - document why
203+
**CQS vs Pragmatism**: Database `pop()` operations can violate CQS when needed
204+
205+
Remember: Principles are guidelines, not laws. Apply with judgment based on context.

0 commit comments

Comments
 (0)