Skip to content

Refactor outdentNode() to extract helper functions #256

Closed

Description

@malibio

Context

From PR #246 code review - the outdentNode() operation has high cyclomatic complexity and would benefit from extracting helper functions to improve maintainability.

Current State (Updated Assessment)

The outdentNode() operation in reactive-node-service.svelte.ts (lines 827-1009) is 183 lines. After architectural changes since this issue was created:

  • The function is functionally correct with all tests passing
  • Code is well-commented with clear phase separation
  • Backend now handles ordering via fractional ordering (simplification)
  • ReactiveStructureTree is the single source of truth for hierarchy

Revised Approach: Light Refactoring

Instead of aggressive extraction (which adds indirection risk), we'll do a targeted refactoring that:

  1. Extracts meaningful, reusable helpers - Only where there's clear benefit
  2. Improves type safety - Add result types for validation
  3. Maintains readability - Keep related logic together
  4. Preserves behavior - No functional changes

Helpers to Extract

1. Validation Helper

type OutdentValidationResult = {
  node: Node;
  oldParentId: string;
  newParentId: string | null;
  siblingsBelow: string[];
  newDepth: number;
};

function validateOutdent(nodeId: string): OutdentValidationResult | null

Consolidates all pre-condition checks and data gathering into one place.

2. Rollback Helper

function rollbackOutdentChanges(
  nodeId: string,
  originalUIState: NodeUIState,
  originalRootNodeIds: string[],
  siblingsBelow: string[],
  oldParentId: string,
  newParentId: string | null
): void

Isolates rollback logic for reuse and clarity.

What We're NOT Extracting

  • Optimistic UI updates - Tightly coupled to local state, extracting adds complexity
  • Backend operation - Already in an IIFE, extracting gains nothing
  • Sibling transfer loop - Clear inline, extracting fragments the logic

Acceptance Criteria (Revised)

  • Extract validation helper (validateOutdent)
  • Extract rollback helper (rollbackOutdentChanges)
  • Add OutdentValidationResult type for type safety
  • Main outdentNode() function reduced to ~100-120 lines (realistic target)
  • All existing tests continue to pass
  • No functional changes - pure refactoring

Benefits

  • Better testability: Validation logic can be unit tested independently
  • Cleaner error handling: Rollback is isolated and reusable
  • Type safety: Result type documents the validation contract
  • Maintainability: Clear separation without over-abstraction

Implementation Notes

  • Preserve Pattern A: All helpers use declarative dependencies
  • No behavioral changes: Functionality remains identical
  • Keep DEV assertions: Maintain existing runtime validation

Related

Priority

Low - Code quality improvement, not blocking. Light touch approach.

Activity

  1. self-assigned this
    on Dec 4, 2025
  2. malibio commented on Dec 4, 2025

    @malibio
    CollaboratorAuthor

    Test Baseline:

    • Frontend: 1569 passed (76 test files)
    • Rust: 567 passed, 1 failed (pre-existing failure in tools_test.rs)

    Starting refactoring work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions