You are on page 1of 2

Lunds tekniska högskola

Datavetenskap, Feb, 2018


Ulf Asklund
EDAF45 Programvaruutveckling i grupp—projekt

Lab Exercise
Software Metrics

1 Introduction
This is not a full, normal, lab where you are supposed to reflect over the results etc. Instead, this lab will
give you hands-on guidelines for some basic metrics. The measurements we will do are counting source
lines of code (SLOC), test coverage, duplicated code, and code complexity. Software metrics in general
and what measurements should be made to get reliable results is hard, and the actual correlation between
the metrics and what we want to know, for example the code quality, is not 100. However, ”You cannot
control what you cannot measure” (DeMarco) is also true.

2 Lines Of Code (SLOC)


The Metric is to count the number of lines of code. The tools have different settings to remove lines not
containing code, e.g. blank lines and comment lines. It can be interesting to count both production code
and test code.
The command line based tool cloc (http://cloc.sourceforge.net) is installed at /usr/local/cs and is
run by the command /usr/local/cs/EDAF45/cloc-1.72/cloc
If you run initcs your PATH will be extended with
/usr/local/cs/EDAF45/cloc-1.72:/usr/local/cs/EDAF45/pmd/bin, i.e. it will be enough to write
cloc only.
cloc -help will give you help, and of course info at the sourceforge-pages.

3 Test Coverage
You should use the tool EClEmma which is an Eclipse plugin. You need to install the plug in:
• In Eclipse select the menu Help/Install new software
• Add software from http://update.eclemma.org/

To do the actual measurements


• Select the project, do Coverage as... / Junit Test (Instead of Run as ... )
Make sure you you measure the coverage you want to. Include/exclude GUI to see what results you
get.

4 Duplicated Code
Here we use the tool PMD, also run from the command line. See https://sourceforge.net/projects/pmd
Installed at /usr/local/cs/EDAF45/pmd/bin/pmd (or just pmd if you run initcs once first)

1
How much code (how many tokens) needed to be equal to be counted as ”duplicated code” is set as
input. Use 50 tokens. The command will then be:
pmd cpd --minimum-tokens 50 --files src
where ”src” is your directory containing the source code
A hint is then to use ”grep” to find relevant info, e.g.
• ... |grep ”Found a” (all found and how long they are)
• ... |grep ”Found a” — wc (the number of duplicates)

• ... |grep -E ”Starting—Found” (see all duplicates and their instanses)


• ... |grep Starting — grep test (see all instanses including test)
Do this both for production code and for your test code.

5 Code Complexity
We will use two metrics for complexity, ”Cyclomatic complexity” and ”NPath”.

Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a pro-
gram. It is a quantitative measure of the number of linearly independent paths through a program’s
source code. It was developed by Thomas J. McCabe, Sr. in 1976.
Read more at https://en.wikipedia.org/wiki/Cyclomatic complexity.

The NPath complexity of a method is the number of acyclic execution paths through that method.

The tool pmd can be used.


pmd pmd Teamxx/src text java-codesize
and then grep to find relevant info
... |grep Cyclomatic — grep method
... |grep NPath — grep method

You might also like