Mark Pitt
claude-skills
  1. claude-skills
  2. Engineering practices
  3. kiss-principle
  4. resources/simplification-strategies.md

A file in kiss-principle

resources/simplification-strategies.md

Markdown 19 KB
simplification-strategies.mdMarkdown, rendered

Simplification Strategies: Proven Techniques for Reducing Complexity

This resource provides 10+ actionable strategies for eliminating unnecessary complexity from code, designs, and systems.

Contents

Strategy 1: Constraint-Based Design

The Idea: Artificially constrain your solution to force simpler approaches.

How It Works:

  • "Design this with 50% fewer lines of code"
  • "Build this with only one database table"
  • "Create this with no if statements"
  • Constraints force creative, simpler solutions

When to Use:

  • During initial design
  • When you're stuck in complexity
  • When reviewing over-engineered solutions

Example: Database Schema

Complex approach: 10 normalized tables with complex joins
├── users
├── user_profiles  
├── user_permissions
├── roles
├── role_permissions
├── accounts
├── account_users
├── subscriptions
├── billing_history
└── invoices

Constraint: "Use only 3 tables"
Simple approach:
├── accounts (id, name, created_at)
├── users (id, account_id, name, email, role, permissions_json, created_at)
└── billing (id, account_id, status, amount, created_at)

Example: Function Complexity

# Complex: 45 lines, nested conditionals
def calculate_price(order, user, region):
    base_price = sum(item.price for item in order.items)
    
    if user.is_vip:
        discount = 0.2
    elif user.is_repeat:
        discount = 0.1
    else:
        discount = 0.0
    
    if order.total > 100 and user.is_vip:
        discount = min(discount + 0.1, 0.5)
    
    # ... more complex logic
    return final_price

# Constraint: "What if all discounts were a single lookup?"
def calculate_price(order, user, region):
    base_price = sum(item.price for item in order.items)
    discount = DISCOUNT_MATRIX[user.tier][order.total > 100]
    return base_price * (1 - discount)

# DISCOUNT_MATRIX = {
#     'vip': {True: 0.3, False: 0.2},
#     'repeat': {True: 0.15, False: 0.1},
#     'new': {True: 0.05, False: 0.0}
# }

Strategy 2: Remove Features/Options

The Idea: Many features add complexity without proportional value. Remove them.

How It Works:

  1. List all features/options
  2. Identify which are rarely used
  3. Remove the least-used ones
  4. Measure impact

When to Use:

  • When feature count grows without proportional benefit
  • During API design
  • In configuration systems
  • In UI design

Example: HTTP Library

Complex: Supporting 20 different authentication methods
Simple: Support only 3 (API key, OAuth2, JWT)

Complex: Supporting 15 HTTP status codes
Simple: Support only 4 (200, 400, 401, 500)

Complex: 50 configuration options
Simple: 5 essential options, rest have sensible defaults

Example: Configuration System

Before:
{
  "logging.level": "debug",
  "logging.format": "json",
  "logging.output": "file",
  "logging.file_path": "/var/log/app.log",
  "logging.max_size": 10485760,
  "logging.max_backups": 5,
  "logging.compress": true,
  "logging.retention_days": 30,
  // ... 50 more options
}

After:
{
  "log_level": "debug",  // debug, info, warn, error
  "output": "stdout"     // stdout or file
}

Strategy 3: Consolidate Duplicate Logic

The Idea: When similar logic appears multiple times, consolidate into one place.

How It Works:

  1. Find duplicated patterns/logic
  2. Extract to shared function/class
  3. Call from multiple places
  4. Maintain single source of truth

When to Use:

  • When code is copy-pasted
  • When similar patterns appear in multiple places
  • During code review

Example:

# Before: Duplicated validation everywhere
def create_user(name, email):
    if not name or len(name) < 2:
        raise ValueError("Invalid name")
    if not email or "@" not in email:
        raise ValueError("Invalid email")
    # ... create user

def update_user(user_id, name, email):
    if name and (not name or len(name) < 2):
        raise ValueError("Invalid name")
    if email and (not email or "@" not in email):
        raise ValueError("Invalid email")
    # ... update user

# After: Consolidated validation
def validate_user_input(name=None, email=None):
    if name is not None and (not name or len(name) < 2):
        raise ValueError("Invalid name")
    if email is not None and (not email or "@" not in email):
        raise ValueError("Invalid email")

def create_user(name, email):
    validate_user_input(name, email)
    # ... create user

def update_user(user_id, name=None, email=None):
    validate_user_input(name, email)
    # ... update user

Strategy 4: Use Defaults and Conventions

The Idea: Instead of requiring explicit configuration, use sensible defaults.

How It Works:

  1. Identify most common usage pattern
  2. Make that the default behavior
  3. Allow override only when needed
  4. Reduces need for configuration

When to Use:

  • In API design
  • In frameworks and libraries
  • In configuration systems
  • In UI design

Example: API Design

Complex: Require all parameters
GET /users?page=1&per_page=20&sort_by=name&sort_order=asc&include=profile&include=posts

Simple: Sensible defaults
GET /users  # Uses page=1, per_page=20, sort by name asc
GET /users?page=2  # Only override what you need
GET /users?per_page=50&sort_by=created_at

# API defaults:
# - page: 1
# - per_page: 20
# - sort_by: created_at
# - sort_order: desc

Example: Configuration

Complex: Specify everything
server:
  host: 0.0.0.0
  port: 3000
  timeout: 30000
  max_connections: 100
  keepalive: true
  compression: true

Simple: Specify only what's different from defaults
server:
  port: 3000

Strategy 5: Flatten Architecture

The Idea: Reduce number of abstraction layers.

How It Works:

  1. Identify layers of indirection
  2. Remove unnecessary layers
  3. Go directly from client to implementation
  4. Maintain clear separation where it matters

When to Use:

  • When reviewing architecture
  • When tracing through code reveals unnecessary indirection
  • During refactoring

Example: Unnecessary Layers

Complex (5 layers):
UI → ViewController → Service → Repository → ORM → Database

Simple (3 layers):
UI → Service → Database

Complex (4 layers):
HTTP Request → Router → Controller → BusinessService → DataService

Simple (2 layers):
HTTP Request → Handler

Example: Dependency Injection

# Complex: Overly abstracted
class UserFactory:
    def __init__(self, config, logger):
        self.config = config
        self.logger = logger
    
    def create_repository(self):
        return UserRepositoryImpl(
            DatabaseConnection(self.config.db),
            self.logger
        )

class UserService:
    def __init__(self, factory):
        self.repository = factory.create_repository()

# Simple: Direct dependencies
class UserService:
    def __init__(self, db_connection):
        self.db = db_connection

# Usage is just as clear and less indirection

Strategy 6: Extract Single-Purpose Functions

The Idea: When a function does multiple things, split it into focused functions.

How It Works:

  1. Identify multiple responsibilities in a function
  2. Extract each responsibility to separate function
  3. Call from original location or refactor callers
  4. Improves reusability and testability

When to Use:

  • When functions exceed 30-40 lines
  • When functions have multiple reasons to change
  • When testing requires setting up complex state

Example:

# Before: Multiple responsibilities
def process_order(order):
    # Validation
    if not order.items:
        raise ValueError("Empty order")
    
    # Calculation
    total = sum(item.price * item.quantity for item in order.items)
    tax = total * 0.1
    
    # Persistence
    db.save_order(order)
    
    # Notification
    send_email(order.customer, f"Order placed: ${total + tax}")
    
    # Analytics
    track_event('order_created', {'amount': total, 'items': len(order.items)})
    
    return total + tax

# After: Single-purpose functions
def validate_order(order):
    if not order.items:
        raise ValueError("Empty order")

def calculate_total(order):
    subtotal = sum(item.price * item.quantity for item in order.items)
    return subtotal * 1.1  # Including 10% tax

def place_order(order):
    validate_order(order)
    total = calculate_total(order)
    db.save_order(order)
    send_order_confirmation(order, total)
    track_order_event(order, total)
    return total

Strategy 7: Use Simple Data Structures

The Idea: Complex data structures often indicate unclear thinking. Use simple structures (dicts, lists, tuples).

How It Works:

  1. Instead of custom classes, use built-in types
  2. Use dictionaries for key-value pairs
  3. Use lists for collections
  4. Create custom types only when it adds clarity

When to Use:

  • When designing data structures
  • When you find yourself writing getters/setters
  • When data is just passed through multiple functions

Example:

# Complex: Custom classes for everything
class UserProfile:
    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
    
    def get_full_name(self):
        return f"{self.first_name} {self.last_name}"
    
    def get_display_name(self):
        return self.first_name.upper()

# Simple: Use dictionaries
user = {
    'first_name': 'John',
    'last_name': 'Doe',
    'email': 'john@example.com'
}

full_name = f"{user['first_name']} {user['last_name']}"
display_name = user['first_name'].upper()

Exception: When it provides clarity or domain-specific behavior, use custom types:

# Makes sense to create custom type
class Money:
    def __init__(self, amount, currency):
        self.amount = amount
        self.currency = currency
    
    def add(self, other):
        if other.currency != self.currency:
            raise ValueError("Cannot add different currencies")
        return Money(self.amount + other.amount, self.currency)

# Simple dictionaries wouldn't make the constraints obvious

Strategy 8: Remove Layers of Indirection

The Idea: Every level of indirection makes code harder to follow. Remove unnecessary ones.

How It Works:

  1. Identify indirection (calling another function that just calls another)
  2. Determine if the indirection adds value
  3. If not, remove it (inline the intermediate function)
  4. If yes, document why it's there

When to Use:

  • During code review
  • When tracing through code requires following multiple function calls
  • When refactoring

Example:

# Unnecessarily indirect
def get_user(user_id):
    return fetch_user(user_id)

def fetch_user(user_id):
    return database.query("SELECT * FROM users WHERE id = ?", user_id)

# Direct and clearer
def get_user(user_id):
    return database.query("SELECT * FROM users WHERE id = ?", user_id)

# If the indirection had a purpose, keep it
def get_user(user_id):
    """Get user with permission check"""
    if not current_user.can_access(user_id):
        raise PermissionError()
    return database.query("SELECT * FROM users WHERE id = ?", user_id)

Strategy 9: Use Configuration Over Codification

The Idea: Behaviors defined in configuration are simpler than behaviors in code.

How It Works:

  1. Identify behaviors that differ based on context (environment, user type, etc.)
  2. Move to configuration instead of if/else branches
  3. Load configuration at startup
  4. Use configuration values in logic

When to Use:

  • When you have many if/else branches checking context
  • When behavior differs between environments
  • When behavior might change without code changes

Example:

# Complex: Business logic in code
def calculate_shipping(order, region):
    if region == 'US':
        if order.weight < 5:
            return 5.99
        elif order.weight < 10:
            return 8.99
        else:
            return 12.99
    elif region == 'EU':
        if order.weight < 5:
            return 8.99
        # ... more logic
    elif region == 'ASIA':
        # ... more logic

# Simple: Business logic in configuration
SHIPPING_RATES = {
    'US': {
        (0, 5): 5.99,
        (5, 10): 8.99,
        (10, float('inf')): 12.99
    },
    'EU': {
        (0, 5): 8.99,
        (5, 10): 12.99,
        (10, float('inf')): 15.99
    }
}

def calculate_shipping(order, region):
    rates = SHIPPING_RATES[region]
    for (min_weight, max_weight), rate in rates.items():
        if min_weight <= order.weight < max_weight:
            return rate

Strategy 10: Standardize Approaches

The Idea: When teams use different approaches for similar problems, pick one and standardize.

How It Works:

  1. Identify where teams use different patterns for similar problems
  2. Evaluate alternatives
  3. Choose the simplest that works
  4. Enforce through code review and standards
  5. Refactor existing code to standard

When to Use:

  • When growing teams have divergent practices
  • When similar functionality is implemented differently
  • During architectural decisions

Example:

Problem: How do we handle errors in our API?

Current: Multiple approaches mixed together
- Some endpoints return { error: "message" }
- Some return { errors: ["message1", "message2"] }
- Some return { status: "error", message: "..." }
- Some return HTTP status with plain text body

Simplified standard: Consistent error format
{
  "error": {
    "code": "ERROR_CODE",
    "message": "Human readable message"
  }
}

Strategy 11: Default to Library/Framework Features

The Idea: Before building custom solutions, use what your framework provides.

How It Works:

  1. Know your framework's capabilities
  2. Before custom code, check if framework has it
  3. Use built-in features where possible
  4. Only build custom when framework doesn't apply

When to Use:

  • When building infrastructure features
  • When designing common patterns
  • During architecture decisions

Example:

Instead of building custom:
- Logging → Use established logging library
- Error handling → Use framework's error handling
- Configuration → Use framework's config system
- Authentication → Use established auth library
- Caching → Use standard cache libraries
- Testing → Use framework's test utilities

Strategy 12: Incremental Generalization

The Idea: Don't generalize prematurely. Build specific solutions, generalize only when patterns emerge.

How It Works:

  1. Build specific solution for current problem
  2. When similar problem arises, build second specific solution
  3. When third similar problem appears, generalize
  4. Refactor the three implementations to common pattern

When to Use:

  • When designing reusable components
  • When building frameworks or libraries
  • During feature development

Example:

Wrong: Generalize immediately
// Guess at generalization before we know patterns
class Repository<T> {
    public T get(string id) { ... }
    public List<T> getAll() { ... }
    public void save(T item) { ... }
    public void delete(T item) { ... }
}

Right: Specific first, generalize when needed
// First: UserRepository (specific)
class UserRepository {
    public User get(string id) { ... }
    public void save(User user) { ... }
}

// Second: OrderRepository (specific)
class OrderRepository {
    public Order get(string id) { ... }
    public void save(Order order) { ... }
}

// Third: ProductRepository (specific)
// Now we see the pattern!

// Then: Extract common pattern
class Repository<T> {
    public T get(string id) { ... }
    public void save(T item) { ... }
}

Simplification Planning Template

When facing complexity, use this template to plan simplification:

COMPLEXITY ANALYSIS
===================
What's complex? 
- Component: ________________
- Measured complexity: ________ (CC, LOC, etc.)
- Why is it complex? ________

ESSENTIAL vs ACCIDENTAL
=======================
What complexity is essential (problem requires it)?
- _______________
- _______________

What complexity is accidental (implementation choice)?
- _______________
- _______________

STRATEGY SELECTION
==================
Which strategies apply? (check all that apply)
- [ ] Constraint-based design
- [ ] Remove features
- [ ] Consolidate duplicates
- [ ] Use defaults
- [ ] Flatten architecture
- [ ] Extract functions
- [ ] Simple data structures
- [ ] Remove indirection
- [ ] Use configuration
- [ ] Standardize approaches
- [ ] Use library features
- [ ] Incremental generalization

SIMPLE SOLUTION
===============
How would the simplest possible solution look?
- ________________________

What are we gaining?
- Reduced LOC: _______
- Reduced complexity: _____
- Faster onboarding: _____

What are we losing? (if anything)
- ________________________

EXECUTION PLAN
==============
Step 1: _______________
Step 2: _______________
Step 3: _______________

Verification:
- [ ] All tests pass
- [ ] Performance acceptable
- [ ] Team understands approach
- [ ] Documentation updated

Key Takeaways

  1. Multiple Strategies: No single strategy works for everything. Know all 12 and choose appropriately.
  1. Iterative: Simplification happens incrementally. Keep refactoring.
  1. Measurement: Know before and after metrics to prove improvement.
  1. Team Alignment: Ensure team understands why simplification is happening.
  1. Prevention: Better to design simple than to simplify later. Use constraints and defaults from the start.

Next Steps

  • Identify the most complex part of your system
  • Assess using complexity-analysis.md
  • Select 2-3 relevant strategies from this guide
  • Make incremental changes and measure results
  • Document what you learned for team