Search documentation of Metastatic

# Metastatic.Analysis.Coupling (Metastatic v0.13.2)

View Source

Coupling analysis for containers (modules/classes).

Measures dependencies between containers. Low coupling is desirable for maintainability.

## Coupling Types

- Afferent Coupling (Ca): Number of containers that depend on this container
- Efferent Coupling (Ce): Number of containers this container depends on
- Instability (I): Ce / (Ca + Ce) - ranges from 0 (stable) to 1 (unstable)

## Assessment

- Low coupling (Ce < 5): Excellent - easy to test and maintain
- Moderate coupling (5 <= Ce < 10): Good - acceptable dependencies
- High coupling (10 <= Ce < 20): Fair - consider refactoring
- Very high coupling (Ce >= 20): Poor - difficult to maintain

## Examples

    # Low coupling - no external dependencies
    ast = {:container, [container_type: :class, name: "Calculator"], [
      {:function_def, [name: "add", params: [{:param, [], "x"}, {:param, [], "y"}], visibility: :public], [
       {:binary_op, [category: :arithmetic, operator: :+], [{:variable, [], "x"}, {:variable, [], "y"}]}]}
    ]}

    doc = Document.new(ast, :python)
    {:ok, result} = Coupling.analyze(doc)
    result.efferent_coupling  # => 0
    result.assessment         # => :excellent

## Summary

### Types

result()

### Functions

analyze(doc)

Analyzes coupling in a document.

analyze!(doc)

Analyzes coupling in a document.

## Types

# result()

    @type result() :: %{
      container_name: String.t() | nil,
      efferent_coupling: non_neg_integer(),
      dependencies: [String.t()],
      instability: float(),
      assessment: :excellent | :good | :fair | :poor,
      warnings: [String.t()],
      recommendations: [String.t()]
    }

## Functions

# analyze(doc)

    @spec analyze(Metastatic.Document.t()) :: {:ok, term()} | {:error, term()}

Analyzes coupling in a document.

Accepts either a `Metastatic.Document` struct or a `{language, native_ast}` tuple.

# analyze!(doc)

    @spec analyze!(Metastatic.Document.t()) :: term()

Analyzes coupling in a document.

Accepts either a `Metastatic.Document` struct or a `{language, native_ast}` tuple.

Unlike not-banged version, this one either returns a result or raises