ruchy 4.2.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
# Remaining Complexity Issues - Post v0.7.22 Refactoring

## Executive Summary

After the v0.7.22 refactoring, we've made significant progress but still have critical complexity violations that need addressing before v0.8.0.

## Current State (Post-Refactoring)

### Progress Made
- **evaluate_expr**: Reduced from 209 → 138 (34% improvement)
- Extracted 7 helper methods for method calls
- All 34 interpreter tests passing
- No performance regression

### Remaining Violations

| Function | Current Complexity | Target | Priority | Estimated Effort |
|----------|-------------------|--------|----------|-----------------|
| `Repl::evaluate_expr` | 138 | <50 | CRITICAL | 2-3 days |
| `Value::format_dataframe` | 69 | <30 | HIGH | 1 day |
| `Value::fmt` | 66 | <30 | HIGH | 1 day |
| `Repl::evaluate_binary` | 46 | <30 | MEDIUM | 0.5 day |
| `Repl::save_session` | 45 | <30 | LOW | 0.5 day |

## Detailed Analysis

### 1. evaluate_expr (138 → <50)

**Remaining Complexity Sources:**
- Control flow handling (if/match/for/while): ~40 points
- Collection literals (list/tuple/range): ~20 points
- Function/lambda definitions: ~15 points
- Try/catch/error handling: ~15 points
- DataFrame operations: ~10 points
- Pipeline operations: ~10 points

**Refactoring Strategy:**
```rust
// Phase 2: Extract control flow
fn evaluate_if_expr(&mut self, ...) -> Result<Value>
fn evaluate_match_expr(&mut self, ...) -> Result<Value>
fn evaluate_for_loop(&mut self, ...) -> Result<Value>
fn evaluate_while_loop(&mut self, ...) -> Result<Value>

// Phase 3: Extract collections
fn evaluate_list_literal(&mut self, ...) -> Result<Value>
fn evaluate_tuple_literal(&mut self, ...) -> Result<Value>
fn evaluate_range_literal(&mut self, ...) -> Result<Value>

// Phase 4: Extract function handling
fn evaluate_function_def(&mut self, ...) -> Result<Value>
fn evaluate_lambda_def(&mut self, ...) -> Result<Value>
fn evaluate_function_call(&mut self, ...) -> Result<Value>
```

### 2. Value::format_dataframe (69 → <30)

**Complexity Sources:**
- Nested loops for column formatting
- Multiple string formatting branches
- Column width calculations

**Refactoring Strategy:**
- Extract column formatter
- Extract row formatter
- Extract width calculator
- Use builder pattern for table construction

### 3. Value::fmt (66 → <30)

**Complexity Sources:**
- 20+ match arms for different value types
- Nested formatting for collections
- Special handling for DataFrames

**Refactoring Strategy:**
- Delegate to type-specific formatters
- Extract collection formatting
- Use Display trait implementations

## Implementation Plan

### Sprint 1: Complete evaluate_expr Refactoring (2-3 days)
1. Extract control flow handlers
2. Extract collection constructors
3. Extract function/lambda handling
4. Verify complexity < 50

### Sprint 2: Fix Display Functions (2 days)
1. Refactor Value::fmt
2. Refactor Value::format_dataframe
3. Add Display trait implementations

### Sprint 3: Clean Up Remaining (1 day)
1. Fix evaluate_binary complexity
2. Fix save_session complexity
3. Run full PMAT analysis
4. Ensure all functions < 50

## Success Metrics

- [ ] All functions have cyclomatic complexity < 50
- [ ] All functions have cognitive complexity < 30
- [ ] 100% test coverage maintained
- [ ] No performance regression (benchmark suite)
- [ ] PMAT quality gate passes
- [ ] Zero new bugs introduced

## Risk Mitigation

1. **Test Coverage**: Run full test suite after each extraction
2. **Performance**: Benchmark before/after each change
3. **Incremental Changes**: One function extraction at a time
4. **Code Review**: Each refactoring PR reviewed
5. **Rollback Plan**: Git tags at each stable point

## Long-term Goals

After achieving < 50 complexity for all functions:
1. Target < 30 complexity (industry best practice)
2. Implement mutation testing (75% kill rate)
3. Add property-based testing
4. Achieve 95% code coverage
5. Zero cognitive complexity warnings

## Conclusion

The v0.7.22 refactoring was a successful first step, reducing the worst offender by 34%. However, we still have 88 complexity points to remove from evaluate_expr alone. The systematic extraction plan outlined above will achieve our quality goals while maintaining functionality and performance.