Expression Branch Density Analysis¶
It’s a normalization of branching constructs by code size. Instead of just saying “this function has 12 branches,” you ask: "how dense is the branching relative to how much code there is?"
Based on this simple formula:
Where tokens counts the main branching/decision constructs:
if(eachiforelse if)matchcases (eachcasecounts +1)- loops:
while,do … while,for/for … yield catchcases- short-circuit boolean ops:
&&,||
What it Measures¶
Branch tokens per 100 LOC:
How it's Measured¶
Reuses counts from cyclomatic token scan, divided by LOC.
Why it Helps¶
- Separates long vs. tangled: two 80-line functions may be very different; density highlights “branchy” logic.
- Refactoring targets: high density often means multiple intertwined decisions → candidates to split, table-drive, or push into ADTs/polymorphism.
- Performance hints: in hot paths, dense branching can inhibit JVM inlining/prediction; helps keep kernels straight-line where possible.
Expression Branch Density: Make Decisions Legible¶
Expression branch density highlights how many decisions are packed into a small stretch of code, not just how many there are. Density doesn’t always change complexity, but it changes readability and testability. Spread decisions out, give them names, and prefer structures that readers already recognize.
1. Name the logic¶
The original line requires mentally evaluating operator precedence and short-circuiting. By naming each sub-decision, we
turn an eye-chart into three testable statements. When the policy changes (e.g., add Auditor), there’s an obvious
place to edit and an obvious test to update.
Tip
Same decisions, but now they’re addressable in tests (roleOk, modeOk, originOk).