debtmap 0.16.6

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# Rebalanced Scoring

Rebalanced scoring is a multi-dimensional scoring algorithm that prioritizes actual code quality issues over pure file size concerns. It provides a more nuanced approach to technical debt prioritization by considering complexity, coverage gaps, structural problems, and code smells.

## Overview

Traditional scoring often over-emphasizes file size, causing large but simple files to rank higher than complex, untested code. The rebalanced algorithm fixes this by:

1. **De-emphasizing size**: Reduces size weight from ~1.5 to 0.3 (80% reduction)
2. **Emphasizing quality**: Increases weights for complexity (1.0) and coverage gaps (1.0)
3. **Additive bonuses**: Provides +20 bonus for complex + untested code (not multiplicative)
4. **Context-aware detection**: Automatically detects and reduces scores for generated code

**Source**: `src/priority/scoring/rebalanced.rs:1-10`

## Severity Levels

The rebalanced algorithm assigns severity levels based on normalized total scores and risk factors. Scores are normalized to a 0-200 range.

**Source**: `src/priority/scoring/rebalanced.rs:12-30` (Severity enum), `src/priority/scoring/rebalanced.rs:416-448` (determine_severity)

| Severity | Score Threshold | Additional Criteria | Description |
|----------|-----------------|---------------------|-------------|
| **CRITICAL** | > 120 | OR complexity > 60 AND coverage > 40 | Requires immediate attention |
| **HIGH** | > 80 | OR complexity > 40 AND coverage > 20 OR structural > 50 | High priority for next sprint |
| **MEDIUM** | > 40 | OR single moderate issue (complexity/coverage/structural > 30) | Plan for future sprint |
| **LOW** | Everything else | - | Minor concerns, size-only issues |

**Severity Determination Logic** (from `src/priority/scoring/rebalanced.rs:416-448`):

```rust
fn determine_severity(components: &ScoreComponents, ...) -> Severity {
    let total = components.weighted_total(&ScoreWeights::default());

    // CRITICAL: Total score > 120 OR high complexity + low coverage
    if total > 120.0 || (components.complexity_score > 60.0 && components.coverage_score > 40.0) {
        return Severity::Critical;
    }

    // HIGH: Total score > 80 OR moderate complexity + coverage gap OR severe structural issue
    if total > 80.0
        || (components.complexity_score > 40.0 && components.coverage_score > 20.0)
        || components.structural_score > 50.0 {
        return Severity::High;
    }

    // MEDIUM: Total score > 40 OR single moderate issue
    if total > 40.0
        || components.complexity_score > 30.0
        || components.coverage_score > 30.0
        || components.structural_score > 30.0 {
        return Severity::Medium;
    }

    // LOW: Everything else
    Severity::Low
}
```

## Score Components

The rebalanced algorithm computes five distinct scoring components, each contributing to the weighted total.

**Source**: `src/priority/scoring/rebalanced.rs:32-55` (ScoreComponents struct)

| Component | Weight Range | Default Weight | Description |
|-----------|--------------|----------------|-------------|
| **complexity_score** | 0-100 | 1.0 | Cyclomatic + cognitive complexity combined |
| **coverage_score** | 0-80 | 1.0 | Testing coverage deficit with complexity bonus |
| **structural_score** | 0-60 | 0.8 | God objects and architectural issues |
| **size_score** | 0-30 | 0.3 | File/function size (reduced from legacy ~1.5) |
| **smell_score** | 0-40 | 0.6 | Long functions, deep nesting, impure logic |

### ScoreComponents Struct

```rust
/// Individual scoring components with their contributions
pub struct ScoreComponents {
    pub complexity_score: f64, // Weight: 0-100
    pub coverage_score: f64,   // Weight: 0-80
    pub structural_score: f64, // Weight: 0-60
    pub size_score: f64,       // Weight: 0-30 (reduced from current)
    pub smell_score: f64,      // Weight: 0-40
}
```

**Source**: `src/priority/scoring/rebalanced.rs:32-40`

### Weighted Total Calculation

The weighted total is normalized to a 0-200 range:

```rust
pub fn weighted_total(&self, weights: &ScoreWeights) -> f64 {
    let raw_total = self.complexity_score * weights.complexity_weight
        + self.coverage_score * weights.coverage_weight
        + self.structural_score * weights.structural_weight
        + self.size_score * weights.size_weight
        + self.smell_score * weights.smell_weight;

    // Normalize to 0-200 range
    // Theoretical max: 100×1.0 + 80×1.0 + 60×0.8 + 30×0.3 + 40×0.6 = 237
    (raw_total / 237.0) * 200.0
}
```

**Source**: `src/priority/scoring/rebalanced.rs:44-54`

## Presets

Debtmap provides four weight presets for different prioritization strategies.

**Source**: `src/priority/scoring/rebalanced.rs:73-127` (ScoreWeights presets)

### Balanced (Default)

The default preset prioritizes complexity and coverage over size.

```toml
[scoring_rebalanced]
preset = "balanced"
```

**Weights**:
- complexity_weight: 1.0
- coverage_weight: 1.0
- structural_weight: 0.8
- size_weight: 0.3
- smell_weight: 0.6

**Use when**: Standard development with focus on actual code quality.

**Source**: `src/priority/scoring/rebalanced.rs:74-83`

### Quality-Focused

Maximum emphasis on code quality, minimal concern for file size.

```toml
[scoring_rebalanced]
preset = "quality-focused"
```

**Weights**:
- complexity_weight: 1.2
- coverage_weight: 1.1
- structural_weight: 0.9
- size_weight: 0.2
- smell_weight: 0.7

**Use when**: You want maximum emphasis on code quality over size.

**Source**: `src/priority/scoring/rebalanced.rs:85-94`

### Size-Focused (Legacy)

Restores legacy behavior for backward compatibility.

```toml
[scoring_rebalanced]
preset = "size-focused"
```

**Weights**:
- complexity_weight: 0.5
- coverage_weight: 0.4
- structural_weight: 0.6
- size_weight: 1.5
- smell_weight: 0.3

**Use when**: Maintaining legacy scoring behavior or when file size is the primary concern.

**Source**: `src/priority/scoring/rebalanced.rs:96-105`

### Test-Coverage-Focused

Emphasizes testing gaps above all other factors.

```toml
[scoring_rebalanced]
preset = "test-coverage"
```

**Weights**:
- complexity_weight: 0.8
- coverage_weight: 1.3
- structural_weight: 0.6
- size_weight: 0.2
- smell_weight: 0.5

**Use when**: Prioritizing test coverage improvements.

**Source**: `src/priority/scoring/rebalanced.rs:107-116`

### Preset Comparison Table

| Preset | Complexity | Coverage | Structural | Size | Smells | Best For |
|--------|------------|----------|------------|------|--------|----------|
| balanced | 1.0 | 1.0 | 0.8 | 0.3 | 0.6 | Standard development |
| quality-focused | 1.2 | 1.1 | 0.9 | 0.2 | 0.7 | Quality-first teams |
| size-focused | 0.5 | 0.4 | 0.6 | 1.5 | 0.3 | Legacy compatibility |
| test-coverage | 0.8 | 1.3 | 0.6 | 0.2 | 0.5 | Coverage campaigns |

### Preset Name Aliases

Presets support multiple naming conventions for convenience:

**Source**: `src/priority/scoring/rebalanced.rs:118-127`

| Preset | Aliases |
|--------|---------|
| balanced | `balanced` |
| quality-focused | `quality-focused`, `quality_focused`, `quality` |
| size-focused | `size-focused`, `size_focused`, `legacy` |
| test-coverage | `test-coverage`, `test_coverage`, `testing` |

## Generated Code Detection

The rebalanced scoring automatically detects and reduces scores for generated code, applying a 90% reduction to the size score.

**Source**: `src/priority/scoring/rebalanced.rs:450-467` (is_generated_file), `src/priority/scoring/rebalanced.rs:246-249` (reduction logic)

### Detection Patterns

Generated files are identified by common naming patterns:

```rust
let generated_patterns = [
    ".generated.rs",
    ".pb.rs",     // Protocol buffers
    ".g.rs",      // Grammar generated files
    "_pb.rs",     // Alternative protobuf naming
    "generated/", // Generated directory
    "/gen/",      // Gen directory
];
```

**Source**: `src/priority/scoring/rebalanced.rs:455-462`

### Score Reduction

When a file matches a generated pattern, the size score is reduced by 90%:

```rust
// Apply generated code detection and scoring reduction
if is_generated_file(&func.file) {
    // Reduce size score by 90% for generated code
    components.size_score *= 0.1;
}
```

**Source**: `src/priority/scoring/rebalanced.rs:246-249`

### Examples

| File Path | Pattern Match | Size Score Adjustment |
|-----------|---------------|----------------------|
| `src/proto/api.pb.rs` | `.pb.rs` | 90% reduction |
| `src/generated/schema.rs` | `generated/` | 90% reduction |
| `src/parser.g.rs` | `.g.rs` | 90% reduction |
| `src/main.rs` | None | No adjustment |

## Configuration

Rebalanced scoring is configured in your `.debtmap.toml` configuration file.

**Source**: `src/config/scoring.rs:495-554` (RebalancedScoringConfig)

### Enabling Rebalanced Scoring

Add the `[scoring_rebalanced]` section to activate rebalanced scoring:

```toml
# .debtmap.toml

[scoring_rebalanced]
preset = "balanced"  # Activates rebalanced scoring with balanced preset
```

### Using Presets

Select a preset to use predefined weight configurations:

```toml
[scoring_rebalanced]
preset = "quality-focused"
```

### Custom Weight Overrides

Override individual weights while using a preset as base:

```toml
[scoring_rebalanced]
preset = "balanced"
complexity_weight = 1.2      # Override complexity weight
coverage_weight = 1.0        # Keep default
# Other weights inherit from preset
```

### Full Custom Configuration

Define all weights manually without a preset:

```toml
[scoring_rebalanced]
# Custom weights (no preset)
complexity_weight = 1.0
coverage_weight = 1.0
structural_weight = 0.8
size_weight = 0.3
smell_weight = 0.6
```

### RebalancedScoringConfig Structure

```rust
pub struct RebalancedScoringConfig {
    /// Preset name (balanced, quality-focused, size-focused, test-coverage)
    pub preset: Option<String>,

    /// Custom complexity weight (overrides preset if specified)
    pub complexity_weight: Option<f64>,

    /// Custom coverage weight (overrides preset if specified)
    pub coverage_weight: Option<f64>,

    /// Custom structural weight (overrides preset if specified)
    pub structural_weight: Option<f64>,

    /// Custom size weight (overrides preset if specified)
    pub size_weight: Option<f64>,

    /// Custom smell weight (overrides preset if specified)
    pub smell_weight: Option<f64>,
}
```

**Source**: `src/config/scoring.rs:496-521`

## Scoring Rationale

Each debt item includes a detailed rationale explaining why a score was assigned. The rationale includes primary factors, bonuses, and context adjustments.

**Source**: `src/priority/scoring/rebalanced.rs:130-223` (ScoringRationale)

### ScoringRationale Struct

```rust
pub struct ScoringRationale {
    pub primary_factors: Vec<String>,
    pub bonuses: Vec<String>,
    pub context_adjustments: Vec<String>,
}
```

**Source**: `src/priority/scoring/rebalanced.rs:131-136`

### Primary Factors

Primary factors are the main contributors to the score:

- **High cyclomatic complexity** (complexity_score > 40): `"High cyclomatic complexity (+{score})"`
- **Significant coverage gap** (coverage_score > 30): `"Significant coverage gap (+{score})"`
- **Structural issues** (structural_score > 30): `"Structural issues (+{score})"`

### Bonuses

Additive enhancements to the score:

- **Complex + untested** (complexity > 40 AND coverage > 20): `"Complex + untested: +20 bonus applied"`
- **Code smells** (smell_score > 20): `"Code smells detected (+{score})"`

### Context Adjustments

Explanations for score adjustments based on context:

- **Size context-adjusted** (0 < size_score < 10): `"File size context-adjusted (reduced weight for file type)"`
- **Size de-emphasized** (size_weight < 0.5): `"Size de-emphasized (weight: {weight})"`

### Example Rationale Output

```
Debt Item: src/payment/processor.rs:142 - process_payment()
Score: 95.3 (CRITICAL)

Primary factors:
  - High cyclomatic complexity (+100.0)
  - Significant coverage gap (+57.2)

Bonuses:
  - Complex + untested: +20 bonus applied
  - Code smells detected (+25.0)

Context adjustments:
  - Size de-emphasized (weight: 0.3)
```

## Complexity Scoring Details

The complexity score is computed from both cyclomatic and cognitive complexity.

**Source**: `src/priority/scoring/rebalanced.rs:265-317` (score_complexity)

### Cyclomatic Complexity Thresholds

| Cyclomatic Complexity | Base Score |
|----------------------|------------|
| > 30 | 100.0 |
| > 20 | 80.0 |
| > 15 | 60.0 |
| > 10 | 40.0 |
| > 5 | 20.0 |
| <= 5 | 0.0 |

### Cognitive Complexity Bonus

An additive bonus is applied based on cognitive complexity:

| Cognitive Complexity | Bonus |
|---------------------|-------|
| > 50 | +20.0 |
| > 30 | +15.0 |
| > 20 | +10.0 |
| > 15 | +5.0 |
| <= 15 | 0.0 |

The final complexity score is capped at 100.0:

```rust
(cyclomatic_score + cognitive_bonus).min(100.0)
```

### Example Calculations

| Function | Cyclomatic | Cognitive | Base Score | Bonus | Final |
|----------|------------|-----------|------------|-------|-------|
| Simple getter | 2 | 3 | 0.0 | 0.0 | 0.0 |
| Moderate logic | 12 | 18 | 40.0 | 5.0 | 45.0 |
| Complex parser | 25 | 45 | 80.0 | 15.0 | 95.0 |
| Very complex | 35 | 60 | 100.0 | 20.0 | 100.0 (capped) |

## Score Normalization

Weighted totals are normalized to a 0-200 range for consistent comparison across projects.

**Source**: `src/priority/scoring/rebalanced.rs:44-54`

### Normalization Formula

```
normalized_score = (raw_total / 237.0) * 200.0
```

### Theoretical Maximum

The theoretical maximum raw score is 237:
- Complexity: 100 × 1.0 = 100
- Coverage: 80 × 1.0 = 80
- Structural: 60 × 0.8 = 48
- Size: 30 × 0.3 = 9
- Smells: 40 × 0.6 = 24
- **Total**: 237

This maps to a normalized score of 200.

## CLI Usage

### Enable Rebalanced Scoring via Config

Create or modify `.debtmap.toml`:

```toml
[scoring_rebalanced]
preset = "balanced"
```

### Compare Standard vs Rebalanced Scoring

```bash
# Create test config with rebalanced scoring
cat > .debtmap-rebalanced.toml <<EOF
[scoring_rebalanced]
preset = "balanced"
EOF

# Compare results
debtmap analyze . --format terminal                            # Standard scoring
debtmap analyze . --config .debtmap-rebalanced.toml --format terminal  # Rebalanced scoring
```

### Test Different Presets

```bash
# Quality-focused preset
cat > .debtmap-quality.toml <<EOF
[scoring_rebalanced]
preset = "quality-focused"
EOF

debtmap analyze . --config .debtmap-quality.toml --top 10
```

## Best Practices

1. **Start with balanced preset** for standard development workflows
2. **Use quality-focused** when code quality is the primary concern
3. **Use test-coverage** during coverage improvement campaigns
4. **Use size-focused (legacy)** only for backward compatibility
5. **Review rationale output** to understand why items are prioritized
6. **Combine with file-level analysis** for comprehensive debt assessment
7. **Track severity distributions** over time to measure improvement

## Migration from Legacy Scoring

### Breaking Changes

- Scores will change significantly for all debt items
- Large files with low complexity will rank lower
- Complex untested code will rank higher
- Size-based prioritization reduced by 80%

### Restoring Legacy Behavior

```toml
[scoring_rebalanced]
preset = "size-focused"
```

### Gradual Migration Steps

1. **Test first**: Add `[scoring_rebalanced]` section to a test config file
2. **Compare**: Run analysis with both standard and rebalanced scoring
3. **Evaluate**: Review how priorities change
4. **Adopt**: Switch your primary config after validation
5. **Tune**: Adjust preset or custom weights based on team priorities

## See Also

- [File-Level Scoring]file-level.md - For architectural issue identification
- [Scoring Configuration]../configuration/scoring.md - Full scoring configuration options
- [Analysis Guide]../analysis-guide/index.md - Interpreting analysis results