Complexity Metrics

And You

"so complicated or intricate as to be hard to understand or deal with"


cost of complexity

Complex code is...

  1. Harder to read
  2. Harder to test
  3. Harder to debug

Cheap to produce
Costly to maintain

Your code has at least one junk drawer

( probably several )

Things aspire to junkiness
Second Law of Thermodynamics (loosely translated)

Lehman's Laws

"[a system] must be continually adapted or it becomes progressively less satisfactory."

"As [a system] evolves its complexity increases unless work is done to maintain or reduce it."'s_laws_of_software_evolution

 -->  Modification  -->  Complexity


code metaphors

"Technical Debt"
( but no technical interest )

"Code smell"

"Broken Windows"

Bad News

Your code must change

Change increases complexity

"... unless work is done to maintain
or reduce [complexity]."

Multiple Layers

of Defense

  • Linting
  • Version control
  • Automated testing
  • Code Reviews


Linting = cheap but limited
--------------------------------- the gap -----------------------------------
Testing = cheap-ish but incomplete

Version Control  = indirect and incomplete 

Code Reviews = super expensive and inconsistent


We can use to numbers to track things!


This is not dogma, it's engineering.

Halstead Metrics

"The Hydra"

Maurice H. Halstead



r = # of distinct operators
n = # of distinct operands
R = # of total operators
N = # of total operands

The Chase

  • Defines measurable properties as formulas
  • They build on each other to describe relationships
  • Not all of these measure complexity
  • Some worth mentioning:
    • Volume
    • Difficulty
    • Effort = Difficulty * Volume
    • Estimated Bugs = Volume / 3000

Cyclomatic Complexity

"Old Reliable"
(never image search for "old reliable" when preparing for a talk)

Thomas J. McCabe


  • Based only on control flow
  • Measures unique paths
  • Gives an ideal number of tests

Maintainability Index

"One [metric] to Rule Them All"

Paul Oman &
Jack R. Hagemeister


171 - 
5.2 * ln( Halstead Volume ) -
0.23 * ( Cyclomatic complexity ) -
16.2 * ln( Line count )

  • Aggregate of several other metrics
  • Scale from -Infinity to 171 (higher is better)
  • Two variants
    • original
    • comment-sensitive


  • maxcomplexity
  • maxdepth
  • maxparams
  • maxstatements

complexity report


Phil Booth & Perry Harlock

It covers other common metrics not seen here.

"Basically a strictly better  version
of this talk in website form. "
- me, just now

The wrap up

Your code loses value over time

Mind the complexity of changing code

Metrics can help

This is not a solved problem.
Use what helps;  ignore the rest


Complexity Metrics and You

By Jared Stilwell

Complexity Metrics and You

A brief introduction to the concept of complexity in software, the effects it has over time, and several tools to help you spot and contain it.

  • 1,056
Loading comments...

More from Jared Stilwell