Robert Smyth's Blog

Code Metrics

 

My real home (Turrif)

 

Contents

Lines Of Code (LOC)

Lines of code (LOC) is a count of each line that has any code element.  It is easy to measure but almost impossible to interpret as is highly dependent on coding standard, language, environment, and coders skill (good coders may do the same with fewer lines). 

For example, the table below shows the same code written with different coding standards.  The difference is 70%.

 

if (iValue > 42)

{

if (iAnotherValue != 0)

{

doSomethingClever();

}

}

if (iValue > 42 && iAnotherValue != 0)

doSomethingClever();

LOC = 7

LOC = 2

 

A better measure of effort may be effective lines of code (eLOC).  For a more detailed discussion of LOC go to http://c2.com/cgi/wiki?LinesOfCode.

Effective Lines Of Code (eLOC)

Effective lines of code (eLOC) is a count of code statements.  It provides a better measurement of effort than lines of code (LOC) and is a complexity indicator.

For example the following code has a 4 effective lines of code.

 

if (iValue > 42)

{

doSomethingClever();

doSomethingElse();

a = b + c;

}

 

See also:

Lines of code (LOC)

Cyclomatric Complexity (Vg)

Kiviat Charts

Cyclomatic Complexity (Vg)

Cyclomatic complexity (Vg) is a calculation of function/method complexity.

Vg Evaluation

1-10

Simple, low complexity

11-20

Moderate risk

21-50

High risk

> 50

Untestable

 

Cyclomatic complexity is a count of the number of basic decisions in a function by assigning 1 point to each of the following:

  • The function.
  • Each if, while, switch, case, and for statement.
  • Each || or && within a conditional.

For example the following function has a Vg of 4.  Each point is shown in bold.

void foo(int iLimit)

{

int iFooVar1;

for (iFooVar1 = 0; (iFooVar1 < iLimit) && (iFoovar1 < 1000); iFooVar++)

{

if (iSomeGlobal > 42)

{

doSomeThingClever();

}

}

}

 

For more information on cyclomatic complexity go to http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html.

Kiviat Charts

A kiviat chart visual displays a set of metrics that provides easy viewing of multiple metrics against minimum and maximum thresholds.  Each radial of chart is a metric.  All metrics are scaled so that all maximums are on a common circle and all minimums are on a common circle.

In the charts below the red circle is the maximum threshold and the blue band is the minimum threshold.  The white band between the two is the acceptable range.  The chart on the left shows that all metrics are well within the acceptable range.  The chart on the right shows an example where all metrics are above maximum limits.

    

Other Code Metrics Tools & Information

Below is a list of other code metrics tools available on sourceforge.

Others (not sourceforge):

General metrics information:

 

 

 

 

 

SourceForge.net Logo

 

Last updated 18th July, 2004, Email: robsmyth at bigpond.net.au