/*
A popular metric is "lack of cohesion in methods". A good explanation can be found at
http://eclipse-metrics.sourceforge.net/descriptions/LackOfCohesionInMethods.html
Here we code the "Henderson-Sellers" variant described on that page. Pleasingly, it
is a direct translation of the relevant formula. How cool is that, uh?
*/// does m access field f defined in the same type?predicate accessesLocalField(Method m,Field f) {
m.accesses(f)
and m.getDeclaringType() = f.getDeclaringType()
}
// class for new metrics propertiesclass MyClass
extends Class {
// returns any method that accesses some local field Method getAccessingMethod() {
result=
this.getAMethod()
and accessesLocalField(
result,_)
}
// returns any field that is accessed by a local method Field getAccessedField() {
result=
this.getAField()
and accessesLocalField(_,
result)
}
// compute lack of cohesion metric float lackOfCohesion() {
exists(
int m,
float r |
// m = number of methods that access some field m =
count(
this.getAccessingMethod())
and // r = average (over f) of number of methods that access field f r =
avg(Field f |
f =
this.getAccessedField() |
count(Method x | accessesLocalField(x,f)))
and // avoid division by zero m != 1
and // compute LCOM result = ((r-m)/(1-m))
)
}
}
from MyClass c, float f
where c.fromSource()
and f=c.lackOfCohesion()
and f > 0.9
select c, f
order by f
desc