 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maciej Sobczak Guest
|
Posted: Tue Apr 13, 2004 3:57 pm Post subject: Code metrics tools for C++ |
|
|
Hi,
What tool would you suggest for measuring complexity of C++ code?
I'm most interested in the cyclomatic complexity.
If possible, free (I mean 0$, not necessarily public-domain) tools are
preferred.
Thank you very much for any suggestions.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alexander Malkis Guest
|
Posted: Wed Apr 14, 2004 6:39 am Post subject: Re: Code metrics tools for C++ |
|
|
Maciej Sobczak wrote:
| Quote: | Hi,
What tool would you suggest for measuring complexity of C++ code?
I'm most interested in the cyclomatic complexity.
If possible, free (I mean 0$, not necessarily public-domain) tools are
preferred.
Thank you very much for any suggestions.
Could you give the definition of the complexity and "cyclomatic |
complexity" of a program?
I'm really interested.
--
Best regards,
Alex.
PS. To email me, remove "loeschedies" from the email address given.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Radu Grigore Guest
|
Posted: Thu Apr 15, 2004 12:53 am Post subject: Re: Code metrics tools for C++ |
|
|
| Quote: | What tool would you suggest for measuring complexity of C++ code?
I'm most interested in the cyclomatic complexity.
|
Try cccc at http://cccc.sourceforge.net/.
regards,
radu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Apr 15, 2004 6:54 am Post subject: Re: Code metrics tools for C++ |
|
|
Alexander Malkis wrote:
| Quote: | Could you give the definition of the complexity and "cyclomatic
complexity" of a program?
I'm really interested.
|
Cyclomatic complexity is a very old measure of code complexity:
you build the flow graph, and then subtract the number of nodes
from the number of edges and add a constant. The more branches
you have, the higher the number.
Researchers then established a correlation between bug counts
and cyclomatic complexity. If I recally correctly, McCabe is
the researcher who invented this measure at IBM.
There are some issues with cyclomatic complexity:
- It is old: it worked well when we had if/goto and computed if.
With the common constructs of modern programming languages, it
is not clear how to count certain things. In particular:
multi-way braches (select-case statements as known to Pascal
programmers); the swich/case/break construct of C/C++ throws
a monkey wrench into the gears
- The measure focuses on the code, but leaves that data alone.
In many modern practices, the distinction between program and
data is not always clear; e.g.: is a parser table program or
data? an array of function pointers? virtual functions? a
regular expression? etc...
The good news is: once you establish a counting method for the
various constructs you use, the correlation between increased
cyclomatic complexity and potential bugs still holds.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Apr 15, 2004 7:01 am Post subject: Re: Code metrics tools for C++ |
|
|
Alexander Malkis <alexloeschediesmalk (AT) stone (DOT) cs.uni-sb.de> wrote in
message news:<c5hshk$b5ok$1 (AT) hades (DOT) rz.uni-saarland.de>...
| Quote: | Maciej Sobczak wrote:
What tool would you suggest for measuring complexity of C++ code?
I'm most interested in the cyclomatic complexity.
If possible, free (I mean 0$, not necessarily public-domain) tools are
preferred.
Could you give the definition of the complexity and "cyclomatic
complexity" of a program?
|
I suspect that he is using the standard definition: a measure of the
number of linearly-independent paths through a program module. See
http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Apr 15, 2004 7:01 am Post subject: Re: Code metrics tools for C++ |
|
|
Hi,
Alexander Malkis wrote:
| Quote: | What tool would you suggest for measuring complexity of C++ code?
I'm most interested in the cyclomatic complexity.
Could you give the definition of the complexity and "cyclomatic
complexity" of a program?
I'm really interested.
|
The complexity of code is a measure of how difficult it is to:
- write it
- test it
- modify it
- reuse it
- etc.
Many different metrics have been invented and the simplest possible is
the number of lines of code. The more lines of code a module has, the
more difficult it is to write it or to exhaustively test it.
That's the obvious observation.
The cyclomatic complexity is a metric based on the graph theory and
basically boils down to the number of branches in the code. Every if,
while, for, etc. adds to this metric. The exact numbers allow to
estimate the quality of the module (a function, for example) with regard
to its maintainability and testability. For example, if the cyclomatic
complexity of the function is more than 20, it is considered to be
impossible to test.
More:
http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html
http://yunus.hun.edu.tr/~sencer/complexity.html
I would like to have a tool that will compute the various metrics from
th given code. They can be used as the additional factor in
accepting/rejecting developers' work or to formalize parts of coding
standards.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Cesar Rabak Guest
|
Posted: Fri Apr 16, 2004 10:10 am Post subject: Re: Code metrics tools for C++ |
|
|
Maciej Sobczak escreveu:
| Quote: | Hi,
[snipped]
I would like to have a tool that will compute the various metrics from
th given code. They can be used as the additional factor in
accepting/rejecting developers' work or to formalize parts of coding
standards.
Have you looked at http://www.scitools.com/ucpp.html ? Never used itself |
myself so YMMV.
--
Cesar Rabak
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Fri Apr 16, 2004 10:20 am Post subject: Re: Code metrics tools for C++ |
|
|
Maciej Sobczak schrieb:
| Quote: |
The cyclomatic complexity is a metric based on the graph theory and
basically boils down to the number of branches in the code. Every if,
while, for, etc. adds to this metric. The exact numbers allow to
estimate the quality of the module (a function, for example) with regard
to its maintainability and testability. For example, if the cyclomatic
complexity of the function is more than 20, it is considered to be
impossible to test.
|
I might be naive, but in the face of exceptions, isn't the number 20 quite
low?
Or don't you count the various execution pathes possible thrown exceptions
might cause?
regards,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Roland Pibinger Guest
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sat Apr 17, 2004 6:12 am Post subject: Re: Code metrics tools for C++ |
|
|
Hi,
Thomas Mang wrote:
| Quote: | For example, if the cyclomatic
complexity of the function is more than 20, it is considered to be
impossible to test.
I might be naive, but in the face of exceptions, isn't the number 20 quite
low?
Or don't you count the various execution pathes possible thrown exceptions
might cause?
|
Of course, the "literal" 20 was taken as a citation from one of the
pages I've seen.
It is true that the cyclomatic complexity was invented almost 30 years
ago and its applicability to modern C++ code may be questionable.
This is just a metric that made me interested, that's why I asked. A
good tool will for sure be able to generate various metrics, so that the
code can be assessed from different perspectives.
The tools proposed by other posters seem to be promising and I'll try
them out.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Apr 20, 2004 8:39 pm Post subject: Re: Code metrics tools for C++ |
|
|
Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote
| Quote: | The complexity of code is a measure of how difficult it is to:
- write it
- test it
- modify it
- reuse it
- etc.
Many different metrics have been invented and the simplest possible is
the number of lines of code. The more lines of code a module has, the
more difficult it is to write it or to exhaustively test it.
That's the obvious observation.
|
It's also one of the worst.
My first point is that whatever method you use to count complexity,
it better find at least the first 3 to be equivalent (and in my opinion
the fourth one should be as well):
template<class T>void foo1(T t) {
if (t.valid()) cout << "validn";
else cout << "invalidn";
}
template
void foo2(T t) {
if (t.valid())
cout << "validn";
else
cout << "invalidn";
}
// Works for all T
// that have a member function named valid()
// which returns a convertible-to-bool
template
// Check validity of the t object
// and displays the appropriate English-language message
// Note: This has not (yet) been language-localized
void foo3(T t) {
// Is the t object valid?
if (t.valid())
{
// The object was valid, so we say so
cout << "validn";
}
else
{
// We detected an invalid object!
cout << "invalidn";
}
// End of function template
}
// Works for all T
// that have a member function named valid()
// which returns a convertible-to-bool
template<class T>
// Check validity of the t object
// and displays the appropriate English-language message
// Note: This has not (yet) been language-localized
//
// Copyright (C) 2004 Allan_W All Rights Reserved
// Permission is hereby given to use this program
// for any purpose whatsoever without remuneration.
// However, use it at your own risk:
// the author does not declare the program
// to be suitable for any purpose whatsoever.
//
void foo4(T t) {
// Holds the message we will display
const char *text;
// Is the t object valid?
if (t.valid())
{
// The object was valid, so we say so
text = "valid";
}
else
{
// We detected an invalid object!
text"invalid";
}
// Tell the user what we found
cout << text
// And be sure to go to the next line!
<< endl;
// End of function template
}
Second: If you ever did find the perfect complexity metric, there ought
to be a law that says nobody is allowed to gather these statistics nor
view the results unless they've attended a training class on what the
heck it means!
* Some people assume that complexity is the same thing as
productivity.
"What? You only wrote 2,000 lines of code last month? The average
was
well over 10,000! What were you doing, surfing the web?"
No -- I was busy making my code less complex. My 2000 lines of code
accomplish just as much as the 10,000 lines everyone else wrote, but
it has a smaller memory footprint, runs more quickly, and is MUCH
easier to document, debug and maintain...
* Other people assume that complexity is the same thing as buggy.
"Your report has 10,000 lines of code. This is much too complex!
The other reports need only 2,000 lines of code!"
No -- I spent time developing reusable components that were re-used
in those other reports, but you counted them against mine only.
Furthermore, by breaking up the logic into small routines I made my
line count go up, but the "real" complexity went down. And please
remember that not all reports are created equally -- some of them
can read most of their data from a database, while others need to
perform complex calculations before they can display the values.
Neither extreme is correct often enough to be useful. The fact that so
many managers THINK they understand it but are wrong, has subjected more
than one good developer to bad performance reviews.
What we really need is a way to measure the REAL complexity of a piece
of code (how convoluted was your logic) and compare it to the complexity
REQUIRED to accomplish those same goals, taking in to account various
measures of performance (i.e. a more complex sort algorithm is
preferable
to a simple one if it has better performance for your typical data or if
the other one exceeds hardware requirements). As far as I know, there is
no objective way of establishing either of these values in absolute
terms.
The alternative, asking multiple development teams to write the code and
then selecting the best one after-the-fact, is much too expensive for
any
real-world project (except perhaps for critical mission-sensitive
projects,
i.e. NASA). You can analyze small sections of code and critique it, but
thorough code complexity analysis will have to remain subjective for
now.
I wish all managers understood this.
When complexity is used partway through a project and used to improve
estimates about when the project will be complete, it can be very
useful.
When it is used for almost anything else, it is confusing at best and
misleading at worst.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Wed Apr 21, 2004 2:03 pm Post subject: Re: Code metrics tools for C++ |
|
|
Hi,
Allan W wrote:
[useful thoughts snipped]
I understand your concerns and I totally agree.
It is not my intention to use any of the metrics as a mean to review
developer's performance. What's more, we now have to face a review
system that is much worse than the scenarios you've described and I seem
to have no influence on my managers to improve it.
The intent is, however, to show the other programmers what they do and
educate them about their own work.
Consider, for example, that one of the programmers writes happily his
code so that almost everything is inside one big function, with massive
code duplication, spaghetti with various spices, and all other evil. If
you just tell him that that's not the way to do things, you'll here "but
look, it just works".
Showing him the various metrics and educating him that he should keep
some numbers within reasonable limits, or at least justify the cases
where the quantitative guidelines are broken, can be a way to improve
his output in the long term.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Apr 21, 2004 3:46 pm Post subject: Re: Code metrics tools for C++ |
|
|
[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote in message
news:<7f2735a5.0404191332.76604df7 (AT) posting (DOT) google.com>...
| Quote: | Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote
The complexity of code is a measure of how difficult it is to:
- write it
- test it
- modify it
- reuse it
- etc.
Many different metrics have been invented and the simplest possible
is the number of lines of code. The more lines of code a module has,
the more difficult it is to write it or to exhaustively test it.
That's the obvious observation.
It's also one of the worst.
|
It depends on context. If I need 1000 lines to implement something,
that something is more complex than if I only need 500 lines. IF an
organism is well structured, you can replace the "I" with "we", and the
statement is still pretty accurate.
[..]
| Quote: | Second: If you ever did find the perfect complexity metric, there
ought to be a law that says nobody is allowed to gather these
statistics nor view the results unless they've attended a training
class on what the heck it means!
|
That's even more true if the metric is imperfect (and they all are). If
you don't know the signification of a metric, you shouldn't use it.
[...]
| Quote: | What we really need is a way to measure the REAL complexity of a piece
of code (how convoluted was your logic) and compare it to the
complexity REQUIRED to accomplish those same goals, taking in to
account various measures of performance (i.e. a more complex sort
algorithm is preferable to a simple one if it has better performance
for your typical data or if the other one exceeds hardware
requirements). As far as I know, there is no objective way of
establishing either of these values in absolute terms.
|
There is some value in estabilishing the overall complexity,
independantly of the supposed required complexity. (In fact, at the
coding level, one could argue that there isn't normally a "required"
complexity. The design should have taken care of that -- if a function
would have been too complex, the design would have factored it out into
less complex parts. But that's begging the question.) The overall
complexity should allow you to make some sort of estimates concerning
future maintenance costs, for example.
| Quote: | The alternative, asking multiple development teams to write the code
and then selecting the best one after-the-fact, is much too expensive
for any real-world project (except perhaps for critical
mission-sensitive projects, i.e. NASA). You can analyze small sections
of code and critique it, but thorough code complexity analysis will
have to remain subjective for now. I wish all managers understood
this.
|
There will always be a subjective element, concerning the meaning or the
relevance of the measures. (That's why there are different measures.)
The measures themselves are objective.
In most real-world use that I've seen, the measures have been used by
the developers, to measure their own progress. Although this isn't
totally without its dangers either -- the goal is to write the simplest
code possible that is still correct and gets the job done, and not to
reduce the measures -- I've found it to be very useful in this context.
| Quote: | When complexity is used partway through a project and used to improve
estimates about when the project will be complete, it can be very
useful. When it is used for almost anything else, it is confusing at
best and misleading at worst.
|
As I say, the most effective use I've seen made of it was for personal
improvement of the programmers. And for measuring the impact of changes
in the procedure. And for estimating future maintenance costs.
Of course, in all of these, it is necessary to understand exactly what
you are doing and why. If the only goal is to reduce the measures, that
can typically be done by such things as ignoring error conditions (which
definitly reduces the complexity). But used intelligently, as part of a
total software quality process, I've found that they are a useful tool.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kevin Cline Guest
|
Posted: Fri Apr 23, 2004 11:47 pm Post subject: Re: Code metrics tools for C++ |
|
|
Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote
| Quote: | Hi,
Allan W wrote:
[useful thoughts snipped]
I understand your concerns and I totally agree.
It is not my intention to use any of the metrics as a mean to review
developer's performance. What's more, we now have to face a review
system that is much worse than the scenarios you've described and I seem
to have no influence on my managers to improve it.
The intent is, however, to show the other programmers what they do and
educate them about their own work.
|
| Quote: | Consider, for example, that one of the programmers writes happily his
code so that almost everything is inside one big function, with massive
code duplication, spaghetti with various spices, and all other evil. If
you just tell him that that's not the way to do things, you'll here "but
look, it just works".
|
My answer to that is "how do you know?" Show me a 100-line function
and I can almost guarantee that I can find a bug.
I don't think cyclomatic complexity is the number you want. Instead,
just establish limits on function length and on control nesting within
a single function. Two is plenty, and three is more than enough.
The other thing you want is to keep duplication to a bare minimum.
Your policy should be that if anyone on the team can show how
duplication can be reduced, then it should be reduced.
The cowboys need to be trained to work in a team. Get away from
individual code ownership and move to team code ownership. Then let
the team decide what is acceptable.
Rather than worrying about whether code can be tested, consider coding
test-first. Then simply reject any code that hasn't been tested.
A lot of programmers hate commenting, so maybe you could show them
that writing understandable code eliminates the need for most
comments.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Apr 26, 2004 2:13 pm Post subject: Re: Code metrics tools for C++ |
|
|
[email]kcline17 (AT) hotmail (DOT) com[/email] (Kevin Cline) wrote in message
news:<ba162549.0404222256.2cb4af7b (AT) posting (DOT) google.com>...
| Quote: | Maciej Sobczak <no.spam (AT) no (DOT) spam.com> wrote in message
news:<c654dg$g3q$1 (AT) nemesis (DOT) news.tpi.pl>...
Allan W wrote:
[useful thoughts snipped]
I understand your concerns and I totally agree.
It is not my intention to use any of the metrics as a mean to review
developer's performance. What's more, we now have to face a review
system that is much worse than the scenarios you've described and I
seem to have no influence on my managers to improve it. The intent
is, however, to show the other programmers what they do and educate
them about their own work.
Consider, for example, that one of the programmers writes happily
his code so that almost everything is inside one big function, with
massive code duplication, spaghetti with various spices, and all
other evil. If you just tell him that that's not the way to do
things, you'll here "but look, it just works".
My answer to that is "how do you know?" Show me a 100-line function
and I can almost guarantee that I can find a bug.
I don't think cyclomatic complexity is the number you want. Instead,
just establish limits on function length and on control nesting within
a single function. Two is plenty, and three is more than enough.
|
One doesn't prevent the other. I would tend to agree that if the
functions are short enough, measuring cyclomatic complexity starts to
become less interesting -- you can't get very complex in just ten lines
and one or two levels of nesting. (Or can you. There's always
goto:-).)
On the other hand, it's hard to say really what is cause and effect. Do
we like small functions because they have low cyclomatic complexity, or
do we like low cyclomatic complexity because it leads to smaller and
simpler functions?
Also, there are exceptions. I've seen functions of more than 100 lines
which didn't have a bug -- the function was just one big switch on the
input character with 256 cases), and very trivial processing for each
case. Any attempt to break it up would have increased complexity. Of
course, it failed both the line count and the cyclomatic complexity
measures.
| Quote: | The other thing you want is to keep duplication to a bare minimum.
Your policy should be that if anyone on the team can show how
duplication can be reduced, then it should be reduced.
The cowboys need to be trained to work in a team. Get away from
individual code ownership and move to team code ownership. Then let
the team decide what is acceptable.
|
That is, of course, what is really important. The big switch function
is perfectly acceptable, because I've never seen a programmer who had
trouble understanding it, or was bothered by its complexity. Cyclomatic
complexity, line count or any other measure, are just tools. They can
give indications, but they cannot replace human judgement. Show me a
hundred lines of code (in one or more functions) that hasn't been
reviewed, and I can almost guarantee that I can find a bug.
| Quote: | Rather than worrying about whether code can be tested, consider coding
test-first. Then simply reject any code that hasn't been tested.
|
The order of coding isn't important. The fact that 1) the unit tests
are reviewed for completeness as part of the code review process, and 2)
the code check-in process runs the tests, and doesn't check the code in
if they fail, is what is important.
| Quote: | A lot of programmers hate commenting, so maybe you could show them
that writing understandable code eliminates the need for most
comments.
|
Eliminates the need for most *internal* comments. There's still no
effective substitute for comments in defining an interface (pre- and
postconditions, etc.).
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|