Code Simplifying
Analyze and simplify code to improve clarity, reduce complexity, and enhance maintainability.
Process
- Identify the target code - Read the file(s) to be simplified
- Analyze complexity indicators:
- Deeply nested conditionals (>3 levels)
- Long functions (>50 lines)
- Duplicated logic
- Complex boolean expressions
- Unclear variable/function names
- Mixed concerns in single functions
- Apply simplification techniques
- Verify behavior preserved - Run tests if available
Simplification Techniques
Extract and Name
// Before: unclear intent
if (user.role === 'admin' || (user.role === 'editor' && resource.authorId === user.id)) {
// After: named condition
const canEdit = user.role === 'admin' || (user.role === 'editor' && resource.authorId === user.id);
if (canEdit) {
Early Returns
// Before: nested
function process(data) {
if (data) {
if (data.valid) {
return transform(data);
}
}
return null;
}
// After: early returns
function process(data) {
if (!data) return null;
if (!data.valid) return null;
return transform(data);
}
Extract Functions
// Before: inline logic
const articles = data.map(item => ({
id: item.id,
title: item.title.trim(),
slug: item.title.toLowerCase().replace(/\s+/g, '-'),
date: new Date(item.created_at).toISOString(),
}));
// After: extracted transformer
function toArticle(item) {
return {
id: item.id,
title: item.title.trim(),
slug: slugify(item.title),
date: formatDate(item.created_at),
};
}
const articles = data.map(toArticle);
Reduce Nesting with Object Lookups
// Before: switch statement
switch (status) {
case 'draft': return 'gray';
case 'published': return 'green';
case 'archived': return 'red';
default: return 'gray';
}
// After: object lookup
const statusColors = { draft: 'gray', published: 'green', archived: 'red' };
return statusColors[status] ?? 'gray';
Consolidate Conditionals
// Before: repeated conditions
if (type === 'image') handleImage(file);
if (type === 'video') handleVideo(file);
if (type === 'document') handleDocument(file);
// After: handler map
const handlers = { image: handleImage, video: handleVideo, document: handleDocument };
handlers[type]?.(file);
Project-Specific Patterns
For this Next.js/Cloudflare project:
- API routes: Use the auth middleware pattern consistently (
requireAdmin,requireEditor, etc.) - Database queries: Prefer parameterized queries, extract common query patterns
- React components: Keep components focused, extract hooks for shared logic
- Error handling: Use consistent error response format
{ success: false, error: string }
Output
After simplifying, provide:
- Summary of changes made
- Complexity reduction metrics (if measurable)
- Any trade-offs or considerations