Design Principles
Introduction
DRY: Don't Repeat Yourself
The Problem I Had
// ❌ BAD: Repeated error handling logic
async function searchWeb(query: string): Promise<SearchResult[]> {
try {
const response = await fetch(`https://api.duckduckgo.com/?q=${query}`);
if (!response.ok) {
console.error(`Search failed: ${response.status} ${response.statusText}`);
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
return data.results;
} catch (error) {
console.error('Error in searchWeb:', error);
if (error instanceof TypeError) {
throw new Error('Network error - check your connection');
}
throw error;
}
}
async function queryLLM(prompt: string): Promise<string> {
try {
const response = await fetch('http://localhost:8000/generate', {
method: 'POST',
body: JSON.stringify({ prompt })
});
if (!response.ok) {
console.error(`LLM failed: ${response.status} ${response.statusText}`);
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
return data.text;
} catch (error) {
console.error('Error in queryLLM:', error);
if (error instanceof TypeError) {
throw new Error('Network error - check your connection');
}
throw error;
}
}
// Same pattern repeated in 15+ functions!My Solution: Extract Common Logic
When DRY Goes Wrong
My DRY Rule
KISS: Keep It Simple, Stupid
The Problem I Had
My Solution: Start Simple
When Complexity is Justified
My KISS Rule
YAGNI: You Aren't Gonna Need It
The Problem I Had
My Solution: Build What's Needed
When to Plan Ahead
My YAGNI Rule
Separation of Concerns
The Problem I Had
My Solution: Separate Layers
Benefits I Gained
Composition Over Inheritance
The Problem I Had
My Solution: Use Composition
Benefits I Gained
Applying Principles Together
Conclusion
What's Next?
References
Last updated