Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.2 Compound Metrics

8.1.2 Compound Metrics

A compound metric is a metric that is defined in terms of other metrics. As an example for a compound metric, assume we have two metrics defined for packages We can then define a metric for the average number of operations in the classes of the package as follows:
<metric name="AvgOps" domain="package"> 
  <description>The average number of operations in the 
  classes of the package.</description>
  <compoundmetric term="NumOps/NumCls" fallback="0"/>
The term attribute defines the metric expression to be evaluated, which is the return value of the metric. The optional fallback expression is used if the metric expression returns a "NaN" (not a number) or infinity. In that case, the fallback expression is evaluated and returned as metric result. This is useful to catch, e.g., divisions by zero and still have a well-defined metric result.

A second version of the compound metric allows the conditional evaluation of expressions:

<metric name="accessedOps" domain="class">
  <description>The number of public operations of the class,
    if it has any clients, or 0 else.</description>
  <compoundmetric condition="NumClients>0" 
                  term="NumPubOps" alt="0" />
For the conditional version of the compound metric, a conditional expression (condition) and an alternative metric expression (alt) is specified in addition to the term attribute. Their meaning is as follows. First, the condition expression is evaluated. If it evaluates to "true", the term expression is evaluated and returned as value of the metric (value 1 is returned if no term expression is specified). If the condition expression is "false", the alt metric expression is evaluated and returned as result of the metric (value 0 is returned if no alt expression is specified).