The Spaghetti Factor: A Software Complexity Metric Proposal
I've had to review code that has spaghetti-level complexity in control flow (too high cyclomatic complexity). And I've had to review code that has spaghetti-level complexity its data flow (too many global variables mixed together into a single computation). And I've had to review procedures that just go on for page after page with no end in sight. But the stuff that will really make your brain hurt is code that has all of these problems.
There are many complexity metrics out there. But I haven't seen a single metric that directly tries to help balance three key points of complexity into a single intuitive number: code complexity, data complexity, and module size. So here is a proposal that could help drive improvement in a lot of the terrible embedded control code I've seen:
The Spaghetti Factor metric (SF):
SF = SCC + (Globals*5) + (SLOC/20)
SCC = Strict Cyclomatic Complexity
Globals = # of read/write global variables
SLOC = # source lines of non-comment code (e.g., C statements)
Scoring:
- 5-10 - This is the sweet spot for most code except simple helper functions
- 15 - Don't go above this for most modules
- 20 - Look closely; review to see if refactoring makes sense
- 30 - Refactor the design
- 50 - Untestable; throw the module away and fix the design
- 75 - Unmaintainable; throw the module away; throw the design away; start over
- 100 - Nightmare; probably you need to throw the whole subsystem away and re-architect it