Class CohesionTracker

java.lang.Object
org.ek9lang.compiler.phase5.CohesionTracker

class CohesionTracker extends Object
Tracks method-field relationships to calculate LCOM4 (Lack of Cohesion of Methods v4). Based on Hitz, M. & Montazeri, B. (1995) "Measuring Coupling and Cohesion In Object-Oriented Systems".

LCOM4 counts connected components of methods, where two methods are connected if they:

  • Access the same field (shared state)
  • One calls the other (direct method coupling)

A value of 1 means perfect cohesion (all methods connected). Higher values suggest the class could be split into multiple classes.

Uses a stack to handle nested constructs (e.g., inner classes).

Complexity: O(M + F + A) where M = methods, F = fields, A = field accesses.

  • Constructor Details

    • CohesionTracker

      CohesionTracker()
  • Method Details

    • pushScope

      void pushScope()
      Push a new scope for tracking cohesion of a construct.
    • enterMethod

      void enterMethod(String methodName)
      Enter a method context for tracking field accesses.
      Parameters:
      methodName - the name of the method being entered
    • exitMethod

      void exitMethod()
      Exit the current method context.
    • recordFieldAccess

      void recordFieldAccess(String fieldName)
      Record a field access by the current method.
      Parameters:
      fieldName - the name of the field being accessed
    • accountForDefaultOperators

      void accountForDefaultOperators(IAggregateSymbol aggregate)
      Account for default operators which implicitly access all fields. Must be called before popAndCalculateLCOM4() for accurate LCOM4 calculation.
      Parameters:
      aggregate - the aggregate symbol to check for default operators
    • popAndCalculateLCOM4

      int popAndCalculateLCOM4()
      Pop the current scope and calculate LCOM4.
      Returns:
      the LCOM4 value (number of connected components)
    • isEmpty

      boolean isEmpty()
      Check if there are any scopes being tracked.
      Returns:
      true if no scopes are on the stack