November 20, 2008, 02:52:13 pm *
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: lack of cohesion in methods  (Read 1181 times)
Oege de Moor
Newbie
*
Posts: 30


« on: April 06, 2007, 05:23:36 pm »

/*
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 properties
class 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

« Last Edit: April 06, 2007, 05:35:40 pm by Oege de Moor » Logged
Pages: [1]
  Print  
 
Jump to: