Metastatic.Analysis.Complexity.Nesting
(Metastatic v0.10.4)
View Source
Maximum nesting depth calculation.
Nesting depth measures how many levels deep the code is nested, which affects readability and maintainability. Deeply nested code is harder to understand and reason about.
3-Tuple Format
All MetaAST nodes use the uniform 3-tuple structure:
{type_atom, keyword_meta, children_or_value}
What Increases Nesting
conditionalbranchesloopbodieslambdabodiesexception_handlingblocks- Nested
blockstructures (when inside above)
Thresholds
- 0-2: Good, easy to understand
- 3-4: Moderate, acceptable
- 5+: High, should be refactored
Examples
# No nesting: depth = 0
iex> ast = {:literal, [subtype: :integer], 42}
iex> Metastatic.Analysis.Complexity.Nesting.calculate(ast)
0
# Single conditional: depth = 1
iex> ast = {:conditional, [], [{:variable, [], "x"}, {:literal, [subtype: :integer], 1}, {:literal, [subtype: :integer], 2}]}
iex> Metastatic.Analysis.Complexity.Nesting.calculate(ast)
1
# Nested conditional: depth = 2
iex> ast = {:conditional, [], [
...> {:variable, [], "x"},
...> {:conditional, [], [{:variable, [], "y"}, {:literal, [subtype: :integer], 1}, {:literal, [subtype: :integer], 2}]},
...> {:literal, [subtype: :integer], 3}]}
iex> Metastatic.Analysis.Complexity.Nesting.calculate(ast)
2
Summary
Functions
Calculates maximum nesting depth for a MetaAST node.
Functions
@spec calculate(Metastatic.AST.meta_ast()) :: non_neg_integer()
Calculates maximum nesting depth for a MetaAST node.
Returns the depth as a non-negative integer (minimum 0).
Examples
iex> ast = {:binary_op, [category: :arithmetic, operator: :+], [{:variable, [], "x"}, {:literal, [subtype: :integer], 5}]}
iex> Metastatic.Analysis.Complexity.Nesting.calculate(ast)
0
iex> ast = {:loop, [loop_type: :while], [{:variable, [], "condition"}, {:literal, [subtype: :integer], 1}]}
iex> Metastatic.Analysis.Complexity.Nesting.calculate(ast)
1