Skip to content

Calculate more metrics about nodes and graph #73

@tarolling

Description

@tarolling

chat gave me 5 metrics to start out with, and i think these would be a good starting point for metrics, given the end goal of this tool. i have modified what it gave me to target an improved version of some of these metrics, e.g. instead of Robert Martin's Ca/Ce, i found a more stable metric:

Modularity Score (Newman–Girvan)

What it shows:
Overall “clean separation” of the codebase into coherent clusters.
Why it’s important:
A high score means dependencies mostly stay inside modules, indicating good architecture.
How to compute:

  • Treat files, classes, or functions as nodes
  • Run a community detection algorithm (e.g., Louvain)
  • Calculate modularity from the partition

BU stability software metric (based on Robert C. Martin’s Ca/Ce)

What it shows:
Balance between incoming vs outgoing dependencies for each module.
Why it’s important:
Near 1.0 → highly volatile module, depends on many others.
Near 0.0 → stable, many depend on it but it changes rarely.
How to compute:

  • Ca = number of incoming edges from other modules
  • Ce = number of outgoing edges to other modules
  • Instability = Ce/(Ca+Ce)Ce/(Ca+Ce)

Largest Strongly Connected Component (SCC) Size

What it shows:
How many items are tangled in mutual dependencies.
Why it’s important:
Big SCCs = “can’t change without touching everything” areas.
How to compute:

  • Collapse cycles into SCCs
  • Track largest SCC size and membership list

Betweenness Centrality

What it shows:
Which nodes act as chokepoints in dependency paths.
Why it’s important:
Changing high-betweenness nodes impacts many other components.
How to compute:

  • Calculate shortest paths between all pairs
  • Count fraction of those paths passing through each node

Cohesion/Coupling Ratio (fine-grained)

What it shows:
For each file/module, how much internal vs external linkage it has.
Why it’s important:
Good code has high cohesion (internal calls) and low coupling (external calls).
How to compute:

  • Internal edges: dependencies between functions/classes within the same file
  • External edges: dependencies to functions/classes in other files
  • Cohesion–coupling ratio = $\frac{internal edges}{external edges+1}​$

Why this core set works

  • 1 & 2 → Big-picture architecture quality
  • 3 & 4 → Risk and refactoring impact
  • 5 → Micro-level design quality, enabled by fine-grained function/class info

This combo of metrics allows users to:

  • Spot spaghetti code via low modularity or large SCCs
  • Identify risky chokepoints via high betweenness
  • Catch unstable modules before they break something downstream
  • Track cohesion vs coupling over time for healthy growth

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    ENHANCEMENTNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions