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

A file in kiss-principle

resources/complexity-analysis.md

Markdown 15 KB
complexity-analysis.mdMarkdown, rendered

Complexity Analysis: Identifying and Understanding Unnecessary Complexity

What is Complexity?

Complexity is the measure of how difficult a system is to understand, maintain, and modify.

Unnecessary complexity is complication that doesn't serve a current requirement or solve a real problem.

Understanding complexity sources is the first step toward simplification.

Types of Complexity

1. Structural Complexity

The structure and interconnections of components.

Indicators:

  • Deep nesting of conditionals, loops, or function calls
  • Multiple levels of abstraction or indirection
  • Tangled dependencies between modules
  • Unclear separation of concerns

Example (code with structural complexity):

# Complex: Multiple levels of indirection
class UserPermissionValidator:
    def validate(self, user):
        return self.permission_service.get_rules().evaluate(
            self.context_builder.build_from(user).with_roles(
                self.role_fetcher.fetch_for(user)
            )
        )

# Simple: Direct approach
def can_access(user, resource):
    return user.role in ALLOWED_ROLES[resource]

2. Cognitive Complexity

How much mental effort is required to understand what's happening.

Indicators:

  • Code that doesn't read like English
  • Business logic mixed with technical implementation
  • Non-obvious control flow
  • Implicit dependencies or behaviors

Example:

# Complex: Hard to follow logic
result = [x for x in (y[z[0]][z[1]] for z in coords if z) 
          if x and x.status == 'active']

# Simple: Clear what's happening
active_items = []
for coord in coords:
    if coord:
        item = data[coord[0]][coord[1]]
        if item and item.status == 'active':
            active_items.append(item)

3. Operational Complexity

The runtime complexity of execution.

Indicators:

  • Slow performance correlated with data size (O(n²), O(n³))
  • Unnecessary database queries in loops
  • Inefficient algorithms
  • Memory leaks or bloat

Example:

# Complex: O(n²) nested loop
for user in users:
    for order in orders:
        if user.id == order.user_id:
            process(order)

# Simple: O(n) with indexing
user_orders = {}
for order in orders:
    user_orders.setdefault(order.user_id, []).append(order)
for user in users:
    for order in user_orders.get(user.id, []):
        process(order)

4. Accidental Complexity

Complexity introduced by implementation choices, not by the problem itself.

Sources:

  • Over-abstraction (too many interfaces/base classes)
  • Speculative generalization (building for use cases that don't exist)
  • Framework overhead (using enterprise framework for simple task)
  • Premature optimization
  • Unnecessary patterns (Strategy pattern for 2 options, Visitor pattern for simple tree walk)

Example:

# Accidental Complexity: Strategy pattern for simple case
class PaymentStrategy:
    def pay(self, amount): pass

class CreditCardPayment(PaymentStrategy):
    def pay(self, amount): 
        return charge_card(amount)

class CashPayment(PaymentStrategy):
    def pay(self, amount):
        return register_cash(amount)

payment = CreditCardPayment()
payment.pay(amount)

# Simple: Just call the right function
pay_by_card(amount)  # or pay_by_cash(amount)

5. Essential Complexity

Complexity inherent to the problem domain.

Examples:

  • Distributed system coordination
  • Compliance with regulatory requirements
  • Complex business rules that mirror real-world complexity
  • Mathematical algorithms for difficult problems

Characteristic: You cannot eliminate it without changing the problem itself.

Complexity Metrics

Cyclomatic Complexity

Measures the number of linearly independent paths through code.

How to Calculate:

  • Count decision points (if, while, case, &&, ||, ?:)
  • Add 1 for the base path
  • CC = 1 + number of decision points

Interpretation:

  • 1-3: Simple (good)
  • 4-7: Moderate (acceptable, but consider simplifying)
  • 8-10: High (should simplify)
  • 11+: Very High (definitely needs refactoring)

Example:

def process_order(order):  # CC = 1
    if order.total > 100:  # CC = 2
        if order.customer_is_vip:  # CC = 3
            apply_discount(0.2)
        else:  # CC = 3 (branch)
            apply_discount(0.1)
    if order.is_rush:  # CC = 4
        expedite_shipping()
    return order
# Total CC = 4

Lines of Code (LOC)

Measures code volume.

Guidelines:

  • Average function: 20-40 lines
  • Average class: 200-400 lines
  • If exceeding these, likely doing too much

Observation: More LOC doesn't always mean more functionality; it might mean more complexity.

Coupling

Measures dependencies between modules.

High Coupling Indicators:

  • Changes to one component require changes to many others
  • Difficult to test in isolation
  • Modules are hard to reuse
  • Changes ripple through system

Low Coupling Indicators:

  • Components can be modified independently
  • Easy to test in isolation
  • Components are reusable
  • Changes are localized

Depth of Nesting

Measures how deeply conditionals or loops are nested.

Guidelines:

  • Depth 1-2: Good
  • Depth 3: Acceptable but consider flattening
  • Depth 4+: Should definitely refactor

Example of high nesting:

for item in items:
    if item.valid:
        for variant in item.variants:
            if variant.available:
                for pricing in variant.pricing:
                    if pricing.region == region:
                        # 4 levels deep!
                        process(pricing)

Number of Parameters

Functions taking many parameters suggest they're doing too much.

Guidelines:

  • 0-2 parameters: Good
  • 3-4 parameters: Acceptable
  • 5+ parameters: Refactor (likely too many responsibilities)

Identifying Unnecessary Complexity: Red Flags

Code Red Flags

🚩 Inconsistent naming: Variables named x, tmp, data1

  • Indicates unclear purpose
  • Problem-domain names are more readable

🚩 Comments explaining what code does (not why)

  • Indicates code isn't self-documenting
  • Solution: Refactor code to be clearer

🚩 Deep nesting (>3 levels)

  • Indicates overly complex control flow
  • Solution: Extract to separate functions, use early returns

🚩 Long functions (>50 lines consistently)

  • Indicates multiple responsibilities
  • Solution: Break into focused, single-purpose functions

🚩 Many parameters (>4)

  • Indicates function doing too much
  • Solution: Group related parameters into objects, simplify

🚩 Boolean trap (function has different behavior based on boolean parameter)

  • Indicates multiple behaviors merged into one function
  • Solution: Split into separate functions
# Boolean trap - hard to understand from call site
process(order, True)  # What does True mean?

# Clear - separate functions
process_expedited(order)
process_standard(order)

🚩 Layers of indirection (delegation to delegation)

  • Indicates unnecessary abstraction
  • Solution: Inline unnecessary abstractions

🚩 Magic numbers or strings

  • Indicates unclear intent
  • Solution: Use named constants

🚩 Highly coupled modules (changes everywhere when one thing changes)

  • Indicates poor separation of concerns
  • Solution: Reduce dependencies

Design Red Flags

🚩 Over-generalization: Building for use cases that don't exist yet

  • "We might need this to handle..."
  • "In the future, we could support..."
  • Solution: YAGNI (You Aren't Gonna Need It) principle

🚩 Premature optimization: Optimizing before profiling

  • Complex algorithms for uncommon cases
  • Cache layers that aren't needed yet
  • Solution: Optimize only what measurements show is slow

🚩 Feature accumulation: Adding features without removing old ones

  • Code paths for deprecated features still present
  • Feature flags everywhere
  • Solution: Ruthlessly remove unused features

🚩 Speculative architecture: Building for scale you don't need yet

  • Distributed system when one server works fine
  • Microservices when monolith is appropriate
  • Solution: Start simple, evolve as needed

🚩 Pattern overuse: Using sophisticated patterns everywhere

  • Strategy pattern for 2 options
  • Visitor pattern for simple tree walk
  • Decorators for basic functionality
  • Solution: Use patterns only when they reduce overall complexity

Organizational Red Flags

🚩 Knowledge silos: Only one person understands component

  • Indicates unnecessary complexity
  • Solution: Simplify until multiple people can understand it

🚩 Onboarding struggles: New developers struggle for weeks

  • Indicates complexity is hidden or poorly understood
  • Solution: Simplify or improve documentation

🚩 Frequent bugs in specific areas: Particular components break repeatedly

  • Indicates complexity that's hard to reason about
  • Solution: Refactor to simpler design

🚩 Fear of changing code: "Don't touch that, it'll break everything"

  • Indicates tight coupling and hidden complexity
  • Solution: Refactor to reduce dependencies

Anti-Patterns: Common Unnecessary Complexity

1. Over-Abstraction

Creating unnecessary layers of abstraction.

Problem:

# Unnecessary abstraction layers
class ConfigLoader:
    def load(self): return ConfigParser().parse(self.file)

class ConfigService:
    def __init__(self):
        self.loader = ConfigLoader()
    def get_config(self):
        return self.loader.load()

# Actual usage
config = ConfigService().get_config()

Solution:

# Direct approach
config = parse_config_file(config_file)

2. Premature Optimization

Optimizing before identifying performance problems.

Problem:

# Complex caching for uncommon case
class CachedUserRepository:
    def __init__(self):
        self.cache = {}
        self.lock = threading.RLock()
    
    def get_user(self, id):
        if id not in self.cache:
            with self.lock:
                if id not in self.cache:  # Double-check lock
                    self.cache[id] = self._fetch(id)
        return self.cache[id]

Solution (if profiling shows need):

# Simple approach first
def get_user(id):
    return database.query("SELECT * FROM users WHERE id = ?", id)

3. Gold Plating

Adding "nice-to-have" features that aren't requirements.

Problem:

  • Feature not requested by users
  • Not in original requirements
  • Adds complexity without clear value
  • "Might be useful someday"

Solution: Build the minimum viable product, add features only when explicitly requested.

4. Speculative Generalization

Over-generalizing to support use cases that don't exist.

Problem:

# Over-generalized for uses that don't exist
class PaymentProcessor:
    def process(self, payment_type, amount, options):
        # 200 lines handling every possible payment type
        if payment_type == 'credit_card':
            # ...complex logic...
        elif payment_type == 'bank_transfer':
            # ...complex logic...
        # ... 10 more payment types

Solution:

# Simple, specific solution
def process_credit_card_payment(amount, card_token):
    return stripe_api.charge(amount, card_token)

def process_bank_transfer(amount, account):
    return bank_api.transfer(amount, account)

5. God Objects/Functions

Single component doing too many things.

Problem:

class Order:
    def __init__(self, items):
        self.items = items
    
    def calculate_total(self): ...
    def apply_taxes(self): ...
    def apply_discounts(self): ...
    def validate_items(self): ...
    def check_inventory(self): ...
    def process_payment(self): ...  # Too many responsibilities!
    def send_confirmation_email(self): ...
    def update_analytics(self): ...

Solution: Break into focused classes/functions:

  • Order (data container)
  • PricingCalculator (calculations)
  • PaymentProcessor (payment)
  • OrderNotifier (notifications)

Measuring Complexity in Your Codebase

Quick Assessment

  1. Readability Test: Can a developer new to the project explain what this code does in 2 minutes? Yes → Appropriately complex No → Too complex
  1. Change Test: How hard is it to add a new feature or fix a bug? Easy (localized change) → Appropriately complex Hard (changes needed everywhere) → Over-coupled, too complex
  1. Knowledge Test: How many people fully understand this component? 3+ people → Good, not overly complex 1 person → Too complex or poorly documented
  1. Onboarding Test: How long until new developer can safely modify this? < 1 week → Appropriately complex 2+ weeks → Too complex

Formal Metrics

Using tools:

  • Python: pylint, radon (cyclomatic complexity)
  • JavaScript: eslint-plugin-complexity
  • Java: SonarQube
  • C#: Roslyn analyzers
  • IDE built-ins: Most IDEs show complexity metrics

What to look for:

  • Functions with CC > 10 (refactor candidates)
  • Classes with > 400 LOC (possibly too many responsibilities)
  • Multiple parameters (> 4) on functions
  • Deep nesting (> 3 levels)

Complexity Refactoring Checklist

When you identify unnecessary complexity:

  • ☐ Understand it first: Why was this complexity added? (Look at git history, comments)
  • ☐ Verify it's unnecessary: Does functionality depend on this complexity? (Extract and test)
  • ☐ Plan refactoring: What's the simpler approach? (Sketch it out)
  • ☐ Refactor incrementally: Small, testable changes not massive rewrite
  • ☐ Test thoroughly: Ensure behavior is preserved
  • ☐ Measure improvement: Track complexity metrics before/after
  • ☐ Document decision: Why was it simplified? (For future context)
  • ☐ Share learning: Teach team about pattern (prevent recurrence)

Common Complexity Patterns by Domain

Frontend/UI Complexity

  • Over-componentization (components too small to be useful)
  • State management over-engineering
  • Unnecessary animation/visual complexity
  • Component prop drilling (too many levels of prop passing)

Backend/API Complexity

  • Over-abstraction of data access
  • Unnecessary request/response validation layers
  • Over-engineered error handling
  • Middleware/interceptor chains

Database Complexity

  • Over-normalization (too many tables, complex joins)
  • Unnecessary caching layers
  • Premature partitioning/sharding
  • Complex migration strategies

Configuration Complexity

  • Configuration too fine-grained (thousands of options)
  • Environment-specific complexity
  • Feature flag explosion
  • Unnecessary parameter passing

Next Steps

  1. Identify: Find the most complex parts of your codebase (use metrics or manual review)
  2. Classify: Is this complexity essential or accidental?
  3. Plan: What's the simpler approach?
  4. Refactor: Use strategies from simplification-strategies.md
  5. Validate: Test that behavior is preserved
  6. Measure: Document improvement in metrics
  7. Learn: Share what you learned with team