Contents > 9 Extending the Metrics and Rule Engine > 9.1 Metric Procedures > 9.1.1 Conception of a New Metric Procedure

9.1.1 Conception of a New Metric Procedure

Assume we wish to calculate a "lack of cohesion" style metric similar to LCOM proposed by Chidamber and Kemerer in [CK94]. These metrics count pairs of elements that have or have not something in common, for instance, the number of pairs of methods in a class that use no common attributes, or, with regards to applicability to UML designs, the number of pairs of operations of a class that do not have at least one parameter type in common. In the latter case, just obtaining the set of parameter types of an operation is easy enough:
<set name="ParaTypeSet" domain="operation">
<description>The set of parameter types of an operation.</description>
<projection relation="context" target="parameter" element="parametertype" />
What we would need then is a metric that takes each pair of operations of a class, and counts those pairs that have a parameter type in common. None of the existing metric procedures in SDMetrics accomplish this, but if we had such a procedure, the definition of the metric could look something like this:
<metric name="LCOM_Parametertypes" domain="class">
<description>Lack of cohesion in operations based on operation 
   parameter types.</description>
<pairwise relation="context" target="operation" 
   paircondition="size(_first.ParaTypeSet * _second.ParaTypeSet)=0" />
The idea is that the metric obtains the set of operations of a class (as we would in a standard projection), and then evaluate the "paircondition" expression for each operation pair. The variables "_first" and "_second" in the pair condition refer to the first and second operation of the pair. The given expression evaluates to true if the intersection of the parameter types of the operations at hand is empty.

To make the procedure more flexible, we could have it support a few more attributes: