Why bother about complexity?
The more complex a module is, the more likely it is to contain
errors, and the more difficult it is to test. The more complex it is,
the harder it is to maintain. The more it contains errors, the more it
needs to be maintained. The problem feeds itself! Some algorithms
are complex, regardless of the way they are implemented, but in
most cases complex implementation is due to bad design.
o McCabe metrics:
V(g) Cyclomatic number (control flow complexity)
Notice though, that the cyclomatic complexity is a measure of the
program's control complexity and not the data complexity. Furthermore,
the same weight is placed on nested and non-nested loops. However, deeply nested conditional structures are harder to understand than non-nested structures.
o Halstead's software science metrics:
V Program volume or information content
B Estimated number of bugs
T Implementation time (or time to understand)
D Difficulty level, error proneness
E Effort to implement
L Program level
N Program length,
N1 Number of operators
N2 Number of operands
n Vocabulary size
n1 Number of unique operators
n2 Number of unique operands
Some notes on CMTJava:
- CMTJava assumes code is syntactically correct, but it doesn't
check that.
- The McCabe complexity at file level is computed using
the following algorithm:
- Start calculations from 1.
- If the file contains function definitions or class/struct
declarations, whose McCabe complexity is over one, add of
each of them the amount how much they are over one. For
example, if some function has complexity 5, the file level complexity
measure is increased by 4.
- If there is some code between function definitions or
class/struct declarations (there might be conditional compilation
directives like #ifdef), add that complexity to the
file level measure.
Example of Complexity Computation
