develooper Front page | perl.perl5.porters | Postings from April 2006

code complexity measures / software metrics (Coverity)

Thread Next
From:
Jarkko Hietaniemi
Date:
April 15, 2006 00:18
Subject:
code complexity measures / software metrics (Coverity)
Message ID:
44409E82.5010708@gmail.com
Another good thing that Coverity (and other static code inspection
tools) finds out for us are code complexity measures.  They are numbers
that tell how perv^Wcomplex some piece of code is, usually (rather
naturally) at the granularity of functions.

The exact formulation, derivation, comparison, and interpretation of
the various measures is of course subject to much heated debate and
academic name-calling, but I think the general guide is that the higher
the number, the scarier the code, and the more likely it is to contain
and accumulate errors because of the sheer complexity.

The obvious thing to reducde the complexity is to slice, dice, and
refactor the scary big pieces into smaller cuddlier pieces, that might
even fit to one editor window, and one's brain.  Of course, these are
just numbers, and one shouldn't immediately fire up an editor because
of just some numbers, but they do give an idea of where scary code lies.

The code complexity measures are computed from the structure of the
code: the flow control graphs, the number of operators, the size of
expressions, and the like.

I am actually rather surprised that C compilers don't do give
these various code complexity measures, they would be by far the
most natural place to put these computations, a natural side product
of what compilers do.  (Of course compilers have their limitations:
Unix compilers don't look at Win32 code, for example.) I don't use
Eclipse but I've heard that it might have some plugins that compute
some of these metrics.

Coverity computes the cyclomatic (McCabe) complexity [1],
the Halstead effort [2] and the Halstead error estimate [3].
If you don't have Coverity access, here is one tool available
for McCabe complexity [4]:

Where one does draw the line of "bad" is tricky (see above
about academic name-calling), but here I chose >= 25 for McCabe [5]
and for Halstead effort >= 5000 [6] (there are almost 3400 functions
in the Perl source, so the complete list would be long):

[1] http://en.wikipedia.org/wiki/Cyclomatic_complexity
    http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html
[2] http://www.sei.cmu.edu/str/descriptions/halstead_body.html
[3] http://mdp.ivv.nasa.gov/halstead_metrics.html
[4] http://packages.debian.org/unstable/devel/pmccabe


[5]
Perl_keyword perl/toke.c 878
walk perl/x2p/walk.c 136
prewalk perl/x2p/walk.c 108
Perl_magic_get perl/mg.c 107
Perl_magic_set perl/mg.c 94
Perl_is_gv_magical perl/gv.c 79
Perl_moreswitches perl/perl.c 61
S_looks_like_bool perl/opmini.c 59
S_looks_like_bool perl/op.c 59
S_scalar_mod_type perl/opmini.c 37
S_scalar_mod_type perl/op.c 37
constant perl/ext/Compress/Raw/Zlib/constants.h 35
constant_7 perl/ext/I18N/Langinfo/const-c.inc 33
Perl_magic_len perl/mg.c 33
Perl_get_vtbl perl/util.c 32
constant perl/ext/File/Glob/const-c.inc 32
constant_5 perl/ext/I18N/Langinfo/const-c.inc 31
constant perl/ext/GDBM_File/const-c.inc 30
cc_opclass perl/ext/B/B.xs 30
S_reg perl/regcomp.c 29
S_reg perl/ext/re/re_comp.c 29
store_scalar perl/ext/Storable/Storable.xs 29
Perl_is_utf8_char perl/utf8.c 25

[6]
Perl_keyword perl/toke.c 1,280,530.00
walk perl/x2p/walk.c 333,967.00
prewalk perl/x2p/walk.c 140,888.00
Perl_magic_set perl/mg.c 61,968.50
Perl_magic_get perl/mg.c 35,199.00
store_scalar perl/ext/Storable/Storable.xs 20,516.30
S_reg perl/regcomp.c 13,092.40
S_reg perl/ext/re/re_comp.c 13,092.40
Perl_pp_leavesublv perl/pp_hot.c 12,462.50
deflateCopy perl/ext/Compress/Raw/Zlib/deflate.c 11,863.90
Perl_pp_leavesub perl/pp_hot.c 10,714.70
Perl_moreswitches perl/perl.c 10,437.20
XS_Digest__MD5_md5 perl/ext/Digest/MD5/MD5.c 9,662.28
Perl_boot_core_UNIVERSAL perl/universal.c 9,182.77
XS_Internals_SvREADONLY perl/universal.c 8,950.71
constant_7 perl/ext/I18N/Langinfo/const-c.inc 8,313.48
constant_5 perl/ext/I18N/Langinfo/const-c.inc 7,501.25
store_tied perl/ext/Storable/Storable.xs 6,494.45
retrieve perl/ext/Storable/Storable.xs 6,476.36
Perl_newSVhek perl/sv.c 6,194.19
Perl_pp_exists perl/pp.c 5,269.37

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About