Metastatic.Analysis.Cohesion.Result (Metastatic v0.10.4)

View Source

Result structure for cohesion analysis.

Cohesion measures how well the members (methods/functions) of a container (class/module) work together. High cohesion indicates members are closely related and work toward a common purpose.

Metrics

  • LCOM (Lack of Cohesion of Methods) - Lower is better (0 = perfect cohesion)
  • TCC (Tight Class Cohesion) - Higher is better (0.0-1.0, 1.0 = perfect)
  • LCC (Loose Class Cohesion) - Higher is better (0.0-1.0, 1.0 = perfect)
  • Method Pairs - Total possible method pairs in container
  • Connected Pairs - Pairs that share at least one instance variable

Interpretation

  • LCOM = 0: Perfect cohesion (all methods use shared state)
  • LCOM > 0: Poor cohesion (methods don't interact)
  • TCC > 0.5: Good tight cohesion
  • TCC < 0.3: Poor cohesion, consider splitting

Examples

iex> result = %Metastatic.Analysis.Cohesion.Result{
...>   container_name: "Calculator",
...>   lcom: 0,
...>   tcc: 1.0,
...>   lcc: 1.0,
...>   method_count: 3,
...>   method_pairs: 3,
...>   connected_pairs: 3,
...>   assessment: :excellent
...> }
iex> result.assessment
:excellent

Summary

Functions

Assess cohesion level based on metrics.

Generate recommendations based on assessment.

Generate warnings based on cohesion metrics.

Types

assessment()

@type assessment() :: :excellent | :good | :fair | :poor | :very_poor

t()

@type t() :: %Metastatic.Analysis.Cohesion.Result{
  assessment: assessment(),
  connected_pairs: non_neg_integer(),
  container_name: String.t() | nil,
  container_type: :module | :class | :namespace | nil,
  lcc: float(),
  lcom: non_neg_integer(),
  method_count: non_neg_integer(),
  method_pairs: non_neg_integer(),
  recommendations: [String.t()],
  shared_state: [String.t()],
  tcc: float(),
  warnings: [String.t()]
}

Functions

assess(lcom, tcc)

@spec assess(non_neg_integer(), float()) :: assessment()

Assess cohesion level based on metrics.

Returns :excellent, :good, :fair, :poor, or :very_poor.

Examples

iex> Metastatic.Analysis.Cohesion.Result.assess(0, 0.9)
:excellent

iex> Metastatic.Analysis.Cohesion.Result.assess(2, 0.4)
:fair

iex> Metastatic.Analysis.Cohesion.Result.assess(10, 0.1)
:very_poor

generate_recommendations(assessment, method_count, tcc)

@spec generate_recommendations(assessment(), non_neg_integer(), float()) :: [
  String.t()
]

Generate recommendations based on assessment.

Examples

iex> Metastatic.Analysis.Cohesion.Result.generate_recommendations(:poor, 15, 0.2)
["Consider splitting this container into multiple focused units", "High method count (15) with low cohesion suggests multiple responsibilities", "Group methods that share state into separate classes"]

generate_warnings(lcom, tcc, method_count)

@spec generate_warnings(non_neg_integer(), float(), non_neg_integer()) :: [String.t()]

Generate warnings based on cohesion metrics.

Examples

iex> Metastatic.Analysis.Cohesion.Result.generate_warnings(8, 0.15, 2)
["Very high LCOM (8) indicates poor cohesion", "Low TCC (0.15) suggests methods don't share state", "Only 2 methods - too small to measure cohesion accurately"]