Metastatic.Analysis.Coupling (Metastatic v0.10.4)

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: ["x", "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()

@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(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze(Metastatic.language(), term(), keyword()) ::
  {:ok, map()} | {:error, term()}

Analyzes coupling in a document.

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

analyze!(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze!(Metastatic.language(), term(), keyword()) :: map()

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