Contents > 8 Defining Custom Design Metrics and Rules > 8.1 Definition of Metrics > 8.1.8 Element Comparisons

8.1.8 Element Comparisons

The procedure compare performs pairwise comparisons of model elements. Consider the following example:

<metric name="DupNameCount" domain="class">
  <description>The number of data types that have the same name as this class.</description>
  <compare term="name" target="datatype" with="name" comp="term=with" />
</metric>
Evaluation of the compare procedure is as follows:
  1. First, we evaluate the metric expression term for the current model element. If the term expression is missing, the model element itself is used as comparison value.
    In our example, term yields the name of the model element.
  2. Next, we have to specify the comparison set - the set of elements to which the current element will be compared. We have two alternatives to do this: In the above example, the comparison set is the set of all datatype elements.
  3. Next, we iterate over each element in the comparison set:
  4. We add up the number of elements for which the comp expression evaluates to true. The total is the value of the metric.
    In the above example, the result of the metric is the number of data types that have the same name as the current class.
The following example shows the use of the set attribute and skips the with and term expressions. The metric counts, for a class, the number of inner classes that have more methods than the class itself. In the example we assume that
<metric name="BiggerInnerCls" domain="class">
  <description>The number of inner classes that have more methods than the class itself.</description>
  <compare set="InnerClasses" comp="term.NumMth<with.NumMth" />
</metric>
There are two more optional attributes which control the behavior of the compare procedure: