You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Redesign LCOM (Lack of Cohesion of Methods) calculation using AST-less regex sets #1018
Redesign LCOM (Lack of Cohesion of Methods) calculation using AST-less regex sets
Description
The current LCOM calculation in the core engine is mathematically flawed. It attempts to calculate the Lack of Cohesion of Methods by taking total "state mutation" regex hits and dividing it by the total number of arguments (total_flux / total_args). This completely abstracts away true variable cohesion into a meaningless raw ratio.
We need to redesign LCOM to reflect true object-oriented cohesion, leveraging the engine's existing function block chunking without requiring a full AST.
Redesign Strategy
While we cannot definitively link variable types, object-oriented languages rely heavily on explicit instance variable prefixes. We can extract these via regex to build highly accurate variable access sets for each method.
Extraction: Within each parsed function block inside a class, run a regex to extract any word following an instance prefix (e.g., this\.([a-zA-Z0-9_]+), self\.([a-zA-Z0-9_]+), \$this->([a-zA-Z0-9_]+)).
Set Construction: Create a set of these extracted instance variable names for each method.
Mathematical Evaluation: Apply the standard LCOM formula by checking the intersections of these sets across all methods in the class. If methods share no instance variables, the LCOM score increases.
Action Items
Remove the current total_flux / total_args LCOM math from the core engine.
Implement a regex-based instance variable extractor (self., this., etc.) in the StructuralExtractor or related block parser.
Calculate the true LCOM score by evaluating the intersection of these sets for all methods grouped within a class boundary.
Closing this as removed rather than redesigned, after assessing the redesign strategy above.
The regex-based approach here (extract self./this./$this-> instance-variable sets per method, compare overlap across methods) is technically doable — extracting the sets is more tractable than the AST-dependent metrics recently trimmed elsewhere, since it's pure name-matching within a single file, no cross-file type resolution needed. But two things changed the call:
Even fixed, the metric has systematic (not random) bias. A shared "blob" attribute — self.state, self.config, self.logger — makes a god-object read as maximally cohesive, which is exactly the failure case this metric exists to catch. Python's @property idiom splits one logical field into two regex-visible names, making genuinely cohesive classes read as fragmented. Composition/delegation hides real coupling that never touches self directly. These aren't noise around a correct value — they're consistent, style-driven skew, which is worse than the metric just being imprecise. At best it would be trustworthy as a within-class trend signal (did this class's score get worse over time), not the bounded 0-100 authoritative score this issue originally proposed.
Given the current focus is on metrics we can be confident in rather than ones with known, predictable blind spots, removing lcom_score outright is the more honest outcome — consistent with the direction of #1025 (trimming metrics an AST-less engine can't structurally back). state_entanglement (density of state mutations) stays, since it doesn't share this failure mode.
Activity
squid-protocol commentedon Aug 4, 2026
Closing this as removed rather than redesigned, after assessing the redesign strategy above.
The regex-based approach here (extract
self./this./$this->instance-variable sets per method, compare overlap across methods) is technically doable — extracting the sets is more tractable than the AST-dependent metrics recently trimmed elsewhere, since it's pure name-matching within a single file, no cross-file type resolution needed. But two things changed the call:Prerequisite bugs. The class-boundary regex doesn't handle nested classes (truncates the outer class's method set — filed as bug(core): Nested class boundaries truncate outer class scope #1040), and two of the four function-slicing modes silently drop nested/inner functions (filed as bug(core): Nested functions are silently dropped from extraction #1041). Both would corrupt any per-class/per-method metric today, LCOM or otherwise.
Even fixed, the metric has systematic (not random) bias. A shared "blob" attribute —
self.state,self.config,self.logger— makes a god-object read as maximally cohesive, which is exactly the failure case this metric exists to catch. Python's@propertyidiom splits one logical field into two regex-visible names, making genuinely cohesive classes read as fragmented. Composition/delegation hides real coupling that never touchesselfdirectly. These aren't noise around a correct value — they're consistent, style-driven skew, which is worse than the metric just being imprecise. At best it would be trustworthy as a within-class trend signal (did this class's score get worse over time), not the bounded 0-100 authoritative score this issue originally proposed.Given the current focus is on metrics we can be confident in rather than ones with known, predictable blind spots, removing
lcom_scoreoutright is the more honest outcome — consistent with the direction of #1025 (trimming metrics an AST-less engine can't structurally back).state_entanglement(density of state mutations) stays, since it doesn't share this failure mode.Removed in #1042.
Remove LCOM (Lack of Cohesion of Methods) metric from the core engine (…