Skip to main content

Module cohesion

Module cohesion 

Source
Expand description

Cohesion command - LCOM4 (Lack of Cohesion of Methods) analysis for Python classes.

LCOM4 measures class cohesion by counting connected components in the method-field graph:

  • LCOM4 = 1: All methods are connected (cohesive class)
  • LCOM4 > 1: Methods form disconnected groups (split candidate)

§Algorithm

  1. Parse class, extract methods and field accesses (self.x)
  2. Build bipartite graph: methods <-> fields they access
  3. Add edges for intra-class method calls (self.method())
  4. Count connected components via union-find with path compression

§TIGER Mitigations

  • T06: Union-find with path compression AND union by rank
  • E01: --timeout flag (default 30s)
  • E04: MAX_METHODS_PER_CLASS and MAX_FIELDS_PER_CLASS limits
  • E05: MAX_ITERATIONS for union-find operations

§Example

tldr cohesion src/models.py
tldr cohesion src/models.py --min-methods 3 --include-dunder
tldr cohesion src/ --format text

Structs§

CohesionArgs
Compute LCOM4 (Lack of Cohesion of Methods) metric for Python classes.
UnionFind
Union-Find (Disjoint Set Union) data structure with path compression and union by rank.

Enums§

OutputFormat
Output format for cohesion command

Functions§

format_cohesion_text
Format a cohesion report as human-readable text.
run
Run cohesion analysis (for programmatic use).