[Phoenix-pm] [jhietaniemi at gmail.com: code complexity measures / software metrics (Coverity)]

Scott Walters scott at illogics.org
Sat Apr 15 01:33:27 PDT 2006


----- Forwarded message from Jarkko Hietaniemi <jhietaniemi at gmail.com> -----

Return-Path: perl5-porters-return-111776-scott=slowass.net at perl.org
X-Original-To: scott at slowass.net
Delivered-To: scott at slowass.net
Received: from lists.develooper.com (x6.develooper.com [63.251.223.186])
	by slowass.net (Postfix) with SMTP id C79C1553AA
	for <scott at slowass.net>; Sat, 15 Apr 2006 07:25:18 +0000 (GMT)
Received: (qmail 5769 invoked by uid 514); 15 Apr 2006 07:18:56 -0000
Mailing-List: contact perl5-porters-help at perl.org; run by ezmlm
Precedence: bulk
list-help: <mailto:perl5-porters-help at perl.org>
list-unsubscribe: <mailto:perl5-porters-unsubscribe at perl.org>
list-post: <mailto:perl5-porters at perl.org>
X-List-Archive: <http://nntp.perl.org/group/perl.perl5.porters/111776>
List-Id: <perl5-porters.perl.org>
Delivered-To: mailing list perl5-porters at perl.org
Received: (qmail 5751 invoked from network); 15 Apr 2006 07:18:55 -0000
Delivered-To: perl5-porters at perl.org
X-Spam-Status: No, hits=-1.0 required=8.0
	tests=BAYES_00,RCVD_IN_BL_SPAMCOP_NET,SPF_PASS
X-Spam-Check-By: la.mx.develooper.com
Received-SPF: pass (x1.develooper.com: domain of jhietaniemi at gmail.com designates 64.233.182.190 as permitted sender)
DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws;
        s=beta; d=gmail.com;
        h=received:message-id:date:from:reply-to:user-agent:mime-version:to:subject:content-type:content-transfer-encoding;
        b=O5noV+6HOkivJunw8+XSYoO/ckH6PV2wNOMo/Yj7zeoYDqowURdHB0CQsDO57Ita+l0albo70egOFLv/HV065MX25+0drXqHi2p3GocnXEQn90jHCOcAtRyzJW7omAeCOsS3J3Ry8L+mf8b35SD3h5LvfOHCrdls+toqte9GBc0=
Message-ID: <44409E82.5010708 at gmail.com>
Date: Sat, 15 Apr 2006 10:19:30 +0300
From: Jarkko Hietaniemi <jhietaniemi at gmail.com>
Reply-To: jhi at iki.fi
User-Agent: Thunderbird 1.5 (Macintosh/20051201)
MIME-Version: 1.0
To: Perl 5 Porters <perl5-porters at perl.org>
Subject: code complexity measures  / software metrics (Coverity)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Length: 3988
Lines: 92

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
----- End forwarded message -----


More information about the Phoenix-pm mailing list