Structural Metrics

Code and widget tree quality measures.

Widget Tree Metrics

MetricTargetDescription
Max depth≤10Tree nesting levels
Max children≤50Direct children count
Total nodes≤1000Without virtualization

Code Metrics

MetricTargetDescription
Cyclomatic complexity≤10Branches per function
Cognitive complexity≤15Mental burden
Function length≤50 linesReadability
File length≤500 linesMaintainability

Tree Depth Analysis

fn max_depth(widget: &dyn Widget) -> usize {
    let child_depths: Vec<usize> = widget.children()
        .iter()
        .map(|c| max_depth(c.as_ref()))
        .collect();

    1 + child_depths.into_iter().max().unwrap_or(0)
}

Node Count

fn node_count(widget: &dyn Widget) -> usize {
    1 + widget.children()
        .iter()
        .map(|c| node_count(c.as_ref()))
        .sum::<usize>()
}

Complexity Warning

DepthStatus
1-5Good
6-10Acceptable
11-15Warning
>15Refactor

Refactoring Patterns

// Before: Deep nesting
Column {
    children: vec![Row { children: vec![Column { ... }] }]
}

// After: Extract component
struct MyComponent { ... }
impl Widget for MyComponent { ... }

Verified Test

#[test]
fn test_structural_tree_depth() {
    // Recursive depth calculation
    fn depth(levels: &[usize]) -> usize {
        if levels.is_empty() {
            0
        } else {
            1 + levels.iter().max().copied().unwrap_or(0)
        }
    }

    // Flat tree: depth 1
    assert_eq!(depth(&[]), 0);
    assert_eq!(depth(&[0, 0, 0]), 1);

    // Nested tree: depth = 1 + max child depth
    assert_eq!(depth(&[1, 2, 1]), 3);  // 1 + 2

    // Deep tree warning threshold
    let max_recommended = 10;
    let actual_depth = 8;
    assert!(actual_depth <= max_recommended);
}