A little known – but very powerful feature of Clover is its built-in Expression Language.
The Expression Language allows developers or managers to combine any of Clover’s 30 report metrics using simple arithmetic expressions.
For example, the CRAP metric which is defined as:
CRAP(m) = comp(m)^2 * (1 — cov(m)/100)^3 + comp(m)
Where comp(m) is the cyclomatic complexity of method m, and cov(m) is the test code coverage provided by automated tests
can be embedded into a Clover report or IDE by simply using the following Clover EL:
complexity^2 * ((1 – %coveredElements/100)^3) + complexity
The CRAP metric is already visualised by Clover’s Project Risk Coverage Cloud, however charting this metric over time – or displaying it for every package, class and method in tabular format – is also possible thanks to Clover EL.
Syntax and Operators
The following arithmetic operators are available:
+ addition
– subtraction
* multiplication
/ division
^ power
) order of operation
Metrics
Any of Clover’s columns may be referenced by name. The following columns are currently defined:
avgClassesPerFile | filteredElements | totalClasses | uncoveredElements |
avgMethodComplexity | ncLineCount | totalElements | uncoveredStatements |
avgMethodsPerClass | lineCount | totalFiles | uncoveredMethods |
avgStatementsPerMethod | percentageCoveredContribution | totalMethods | coveredStatements |
complexity | percentageUncoveredContribution | totalPercentageCovered | coveredMethods |
complexityDensity | totalBranches | totalStatements | coveredElements |
coveredBranches | totalChildren | uncoveredBranches |
A percentage sign, ‘%’, before a column identifier will evaluate to the percentage of that column’s data, rather than its raw value. For example,
%CoveredElements
is shorthand for
(CoveredElements/TotalElements) * 100
Reporting Options
Any Clover Expression you define is actually a first-class “Column”. This means that any of Clover’s report configurations that take a Column name,
can also take a Clover Expression column.
A Column can be displayed in the following parts of a Clover HTML report:
Tabular
<clover-report> displays the column in a tabular format along side the other metrics. The following piece of Ant script
<!-- clover-columns is a type that can be used to define and label a set of commons for reuse across tasks. --> <clover-columns id="my.columns"> <expression title="CRAP">complexity^2 * ((1 - %coveredElements/100)^3) + complexity</expression> <totalPercentageCovered format="longbar"/> </clover-columns> <clover-report> <current outfile="clover_html"> <columns refid="my.columns"/> </current> ...
produces the following report:
Within any <movers> element of a <historical> report element:
<movers> <columns> <expression title="CRAP">complexity^2 * ((1 - %coveredElements/100)^3) + complexity</expression> </columns> </movers>
displays a special “CRAP” movers section in the Clover historical report so you can spot which source files to look at when the crap hits the fan.
Charts
Within a <chart> element of a <historical/> report element.
<chart title="Metrics" height="320" upperbound="1000"> <columns refid="my.columns"/> </chart>
This uses the “my.columns” refid defined above as the data points in the chart, which helps spot trends across time of the CRAP metric.
This chart shows exactly what you would expect – as the Code Coverage increases, the CRAP decreases.
Eclipse similarly allows arbitrary Columns to be defined in Clover EL, letting you also view the CRAP metric directly in your IDE.
For example, to view the CRAP metric in the Eclipse Coverage Explorer:
1. Select the ‘columns’ menu
2. Hit the “new” button

3. Define your Column in Clover EL

4. View your Column in the Coverage Explorer

Clover’s Expression Language, combined with its flexible reporting and IDE integration lets you monitor and view the metrics that you and your team decide are important.
The CRAP metric is a great way to quantify Clover’s Coverage Cloud and TreeMap visualisations – allowing you to track it over time, be alerted when it moves, or simply order by it to identify the crappiest class in your project.