mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 20:47:52 +01:00
- Fix currency defaults (SATS → CHF) across forms, configs, and API routes - Remove @ts-nocheck from socialService.ts and improve type safety - Replace console.log statements with proper logger utility in production code - Additional type safety improvements and code quality enhancements - Documentation updates for improved maintainability All changes follow DRY, SSOT, and separation of concerns principles.
236 lines
5.4 KiB
Markdown
236 lines
5.4 KiB
Markdown
# Code Simplicity & Reliability Principles
|
|
|
|
**Created:** 2025-01-28
|
|
**Last Modified:** 2025-01-28
|
|
**Last Modified Summary:** Initial creation of code simplicity principles
|
|
|
|
## 🎯 Philosophy
|
|
|
|
> **Beautiful code is simple, reliable, and maintainable.**
|
|
>
|
|
> Every line should serve a purpose. Every abstraction should reduce complexity, not add it. Every pattern should make the system easier to understand and modify.
|
|
|
|
## ✨ Principles
|
|
|
|
### 1. **Extract Common Patterns**
|
|
|
|
**Before:**
|
|
```typescript
|
|
const cacheControl = userId
|
|
? 'private, no-cache, no-store, must-revalidate'
|
|
: 'public, s-maxage=60, stale-while-revalidate=300';
|
|
// ... repeated in 10+ files
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
import { getCacheControl } from '@/lib/api/helpers';
|
|
const cacheControl = getCacheControl(!!userId);
|
|
```
|
|
|
|
**Benefit:** Single source of truth, easier to change, less code
|
|
|
|
---
|
|
|
|
### 2. **Use Helper Functions for Complex Logic**
|
|
|
|
**Before:**
|
|
```typescript
|
|
// Inline date normalization (repeated, error-prone)
|
|
start_date: typeof data.start_date === 'string'
|
|
? data.start_date
|
|
: data.start_date?.toISOString(),
|
|
end_date: data.end_date
|
|
? (typeof data.end_date === 'string'
|
|
? data.end_date
|
|
: data.end_date.toISOString())
|
|
: null,
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
import { normalizeDates } from '@/lib/api/helpers';
|
|
const normalized = normalizeDates(data, ['start_date', 'end_date', 'rsvp_deadline']);
|
|
```
|
|
|
|
**Benefit:** Reusable, testable, less error-prone
|
|
|
|
---
|
|
|
|
### 3. **Simplify Conditionals**
|
|
|
|
**Before:**
|
|
```typescript
|
|
if (includeOwnDrafts) {
|
|
query = query.in('status', draftStatuses);
|
|
} else {
|
|
query = query.in('status', publicStatuses);
|
|
}
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
const statuses = includeOwnDrafts ? draftStatuses : publicStatuses;
|
|
query = query.in('status', statuses);
|
|
```
|
|
|
|
**Benefit:** More readable, less nesting, easier to understand
|
|
|
|
---
|
|
|
|
### 4. **Extract Constants**
|
|
|
|
**Before:**
|
|
```typescript
|
|
// Magic values scattered throughout
|
|
publicStatuses: ['published', 'open', 'full', 'ongoing', 'completed'],
|
|
draftStatuses: ['draft', 'published', 'open', 'full', 'ongoing', 'completed'],
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
// Constants at top of file
|
|
const EVENT_PUBLIC_STATUSES = ['published', 'open', 'full', 'ongoing', 'completed'] as const;
|
|
const EVENT_DRAFT_STATUSES = ['draft', ...EVENT_PUBLIC_STATUSES] as const;
|
|
|
|
// Used in config
|
|
publicStatuses: [...EVENT_PUBLIC_STATUSES],
|
|
draftStatuses: [...EVENT_DRAFT_STATUSES],
|
|
```
|
|
|
|
**Benefit:** Single source of truth, easier to maintain, type-safe
|
|
|
|
---
|
|
|
|
### 5. **Use Descriptive Variable Names**
|
|
|
|
**Before:**
|
|
```typescript
|
|
const rl = rateLimitWrite(user.id);
|
|
if (!rl.success) { ... }
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
const rateLimit = rateLimitWrite(user.id);
|
|
if (!rateLimit.success) { ... }
|
|
```
|
|
|
|
**Benefit:** Self-documenting code, easier to read
|
|
|
|
---
|
|
|
|
### 6. **Simplify Data Transformation**
|
|
|
|
**Before:**
|
|
```typescript
|
|
const entityData = {
|
|
...(transformData ? transformData(ctx.body, user.id) : { ...ctx.body, user_id: user.id }),
|
|
...defaultFields,
|
|
};
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
const transformedData = transformData
|
|
? transformData(ctx.body, user.id)
|
|
: { ...ctx.body, user_id: user.id };
|
|
const entityData = { ...transformedData, ...defaultFields };
|
|
```
|
|
|
|
**Benefit:** Clearer flow, easier to debug, more readable
|
|
|
|
---
|
|
|
|
### 7. **Consistent Error Handling**
|
|
|
|
**Before:**
|
|
```typescript
|
|
if (error) {
|
|
logger.error('Error fetching events', { error });
|
|
return apiInternalError('Failed to fetch events');
|
|
}
|
|
```
|
|
|
|
**After:**
|
|
```typescript
|
|
if (error) {
|
|
logger.error(`Error fetching ${entityType}`, { error, table });
|
|
return apiInternalError(`Failed to fetch ${meta.namePlural.toLowerCase()}`);
|
|
}
|
|
```
|
|
|
|
**Benefit:** Consistent messages, better debugging, uses entity metadata
|
|
|
|
---
|
|
|
|
## 📊 Impact
|
|
|
|
### Code Quality Improvements
|
|
|
|
1. **Helper Functions** (`helpers.ts`, `authHelpers.ts`)
|
|
- Extracted 5+ common patterns
|
|
- ~50 lines of reusable utilities
|
|
- Used across 10+ API routes
|
|
|
|
2. **Simplified Conditionals**
|
|
- Reduced nesting
|
|
- Clearer logic flow
|
|
- Easier to test
|
|
|
|
3. **Constants Extraction**
|
|
- Single source of truth
|
|
- Type-safe with `as const`
|
|
- Easier to maintain
|
|
|
|
4. **Better Variable Names**
|
|
- Self-documenting code
|
|
- Easier to understand
|
|
- Less cognitive load
|
|
|
|
### Reliability Improvements
|
|
|
|
1. **Date Normalization**
|
|
- Handles all edge cases
|
|
- Consistent behavior
|
|
- Less error-prone
|
|
|
|
2. **Cache Control**
|
|
- Consistent headers
|
|
- Easy to change globally
|
|
- Better caching strategy
|
|
|
|
3. **Auth Helpers**
|
|
- Consistent auth checks
|
|
- Less duplication
|
|
- Easier to audit
|
|
|
|
## 🎨 Code Beauty Checklist
|
|
|
|
When reviewing code, ask:
|
|
|
|
- [ ] **Is it simple?** Can a junior developer understand it?
|
|
- [ ] **Is it reliable?** Does it handle edge cases?
|
|
- [ ] **Is it DRY?** Is logic duplicated anywhere?
|
|
- [ ] **Is it consistent?** Does it follow established patterns?
|
|
- [ ] **Is it maintainable?** Will it be easy to change later?
|
|
- [ ] **Is it testable?** Can it be tested in isolation?
|
|
- [ ] **Is it documented?** Are complex parts explained?
|
|
|
|
## 🚀 Next Steps
|
|
|
|
Continue applying these principles:
|
|
|
|
1. **Extract more helpers** - Look for repeated patterns
|
|
2. **Simplify conditionals** - Reduce nesting
|
|
3. **Extract constants** - Remove magic values
|
|
4. **Improve naming** - Make code self-documenting
|
|
5. **Add type safety** - Catch errors at compile time
|
|
|
|
---
|
|
|
|
**Remember:** Beautiful code is not clever code. Beautiful code is simple, clear, and reliable.
|
|
|
|
|
|
|