Complexity Metrics
And You
"so complicated or intricate as to be hard to understand or deal with"
- dictionary.com
cost of complexity
Complex code is...
-
Harder to read
-
Harder to test
-
Harder to debug
Cheap to produce
Costly to maintain
http://www.trashsociety.com/2010/11/26/the-junk-drawer-trashogram/
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."
http://en.wikipedia.org/wiki/Lehman's_laws_of_software_evolution
Usage --> 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
http://ariya.ofilabs.com/2012/12/quality-code-via-multiple-layers-of-defense.html
Feedback
Linting = cheap but limited
--------------------------------- the gap -----------------------------------
Testing = cheap-ish but incomplete
Version Control = indirect and incomplete
Code Reviews = super expensive and inconsistent
Metrics
We can use to numbers to track things!
source: http://measuringupblog.com/2013/11/13/are-you-covering-all-of-your-measurement-bases/
This is not dogma, it's engineering.
Halstead Metrics
"The Hydra"
Maurice H. Halstead
1973
Given:
r = # of distinct operators
n = # of distinct operands
R = # of total operators
N = # of total operands
then...
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
1976
-->
-
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
1991
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
JSHint
- maxcomplexity
- maxdepth
- maxparams
- maxstatements
Plato
- visualization across a project
- visualization over time
-
github project
- examples
-
JSComplexity.org
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