Back to Home

Combine Behavior + Complexity

Find true high-risk zones in your code

Behavior + Complexity

I love the movie The Perfect Storm. The scene when a massive wave crashes onto a fishing boat always sticks with me. I suspect some of your files look a lot like that boat; at least, some of mine certainly have. These files usually have many rows, lot of ifs and lack of tests and can bring your production down at any time.

I remember one project in particular where we had a moderately complex computation function that was changed very often. It involved a mix of services and a shared, scoped(!!) Entity Framework context. Each class mutated the context's internal state, and those changes cascaded through multiple layers. The result was that customers reported seemingly random bugs that were nearly impossible to reproduce as it depended on the shape of the data at that moment. Every time we tried to fix one bug, two more would appear.

To identify these annoying parts of your code, you can use a combination of behavior and complexity analysis.

Why it matters

Combining cyclomatic complexity with change frequency reveals your codebase's true danger zones. Complex code isn't necessarily bad if it rarely changes.
Frequently changed code isn't a problem if it's simple.
But when you find files that are both complex AND frequently modified, you've found technical debt that actively damages your team's velocity and quality of sleep.
By plotting complexity against change frequency, you can visualize exactly which files deserve immediate refactoring attention versus which can safely be left alone.

Cosmic Analogy

Your codebase can be seen as a cosmic landscape plotted by complexity (vertical) and change frequency (horizontal):

The Moon (High Complexity, Low Change)

Complex but rarely touched. Legacy code or stable algorithms that simply work. Monitor them, but they're not your immediate threat.

The Black Hole (High Complexity, High Change)

The event horizon. Complex AND constantly changing, sucking in developer time and generating bugs. These are the danger zones demanding immediate refactoring.

The Void (Low Complexity, Low Change)

Simple and stable. Config files, constants, utilities. Vast, predictable, requiring little attention. Ideal code, if you ask me.

The Sun (Low Complexity, High Change)

Frequently changing but simple. UI components, API endpoints, feature flags. Their simplicity makes frequent changes safe. Healthy, adaptive code.

Cosmic analogy

Behavior Complexity Visualization

How to Fix

Prioritization

  • Start with the worst offenders: focus on files in the high complexity + high change quadrant first.
  • Schedule regular refactoring sessions focused on hotspot files identified by this analysis.

Code Simplification

  • Break down complex functions into smaller, single-responsibility methods that are easier to test and modify.
  • Extract business logic into separate classes or modules to reduce cyclomatic complexity.
  • Consider splitting large files into multiple smaller files with clear boundaries and responsibilities.
  • Use design patterns (Strategy, Factory, Command) to eliminate nested conditionals and switch statements.

Testing & Refactoring Safety

  • Add comprehensive unit tests before refactoring to ensure you don't break existing behavior.
  • Introduce interfaces and dependency injection to reduce coupling and make code more testable.

Prevention & Governance

  • Set up code complexity gates in CI/CD to prevent new complex code from being merged.
  • Document architectural decisions and create clear module boundaries to prevent complexity creep.