codesearch 0.1.7

A fast, intelligent CLI tool with multiple search modes (regex, fuzzy, semantic), code analysis, and dead code detection for popular programming languages
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
# Design Metrics Implementation

## Overview

Comprehensive implementation of software design quality metrics for measuring coupling, cohesion, and architectural stability.

## Implemented Metrics

### 1. Afferent Coupling (Ca)

**Definition**: Number of classes or modules depending on a given module.

**Interpretation**:
- **High Ca (>5)**: Module is critical; many other modules depend on it
- **Impact**: Changes to this module affect many other modules
- **Use Case**: Identify core/critical modules that need extra care

**Example**:
```
Module A: Ca = 8
→ 8 other modules depend on Module A
→ Changes to A will impact 8 modules
→ A is a critical module
```

### 2. Efferent Coupling (Ce)

**Definition**: Number of classes or modules a given module depends on.

**Interpretation**:
- **High Ce (>5)**: Module has high reliance on external components
- **Impact**: Changes in dependencies affect this module
- **Use Case**: Identify modules with too many dependencies

**Example**:
```
Module B: Ce = 10
→ Module B depends on 10 other modules
→ Changes in any of those 10 modules may affect B
→ B is highly dependent
```

### 3. Instability (I)

**Definition**: Measures the balance between afferent and efferent coupling.

**Formula**: `I = Ce / (Ca + Ce)`

**Range**: 0.0 to 1.0

**Interpretation**:
- **I = 0.0**: Maximally stable (many dependents, no dependencies)
- **I = 1.0**: Maximally unstable (no dependents, many dependencies)
- **I < 0.3**: Stable module (good for core/infrastructure)
- **I > 0.7**: Unstable module (good for high-level/UI code)

**Example**:
```
Module C: Ca = 6, Ce = 2
I = 2 / (6 + 2) = 0.25 (Stable)

Module D: Ca = 1, Ce = 8
I = 8 / (1 + 8) = 0.89 (Unstable)
```

### 4. Abstractness (A)

**Definition**: Ratio of abstract classes/interfaces to total classes in a module.

**Formula**: `A = Abstract Classes / Total Classes`

**Range**: 0.0 to 1.0

**Interpretation**:
- **A = 0.0**: Completely concrete (no abstractions)
- **A = 1.0**: Completely abstract (all interfaces/traits)
- **High A**: More flexible, easier to extend

### 5. Distance from Main Sequence (D)

**Definition**: Measures how far a module is from the ideal balance of stability and abstractness.

**Formula**: `D = |A + I - 1|`

**Range**: 0.0 to 1.0

**Interpretation**:
- **D = 0.0**: On the main sequence (ideal balance)
- **D > 0.5**: Far from ideal (problematic)
- **Zone of Pain**: High stability (I=0), Low abstractness (A=0)
- **Zone of Uselessness**: Low stability (I=1), High abstractness (A=1)

### 6. Package Cohesion (LCOM)

**Definition**: Lack of Cohesion of Methods - measures how well classes in a package work together.

**Formula**: `LCOM = Non-sharing method pairs / Total method pairs`

**Range**: 0.0 to 1.0

**Interpretation**:
- **LCOM = 0.0**: Perfect cohesion (all methods share fields)
- **LCOM = 1.0**: No cohesion (no methods share fields)
- **LCOM < 0.5**: Good cohesion
- **LCOM > 0.7**: Poor cohesion (consider splitting class)

**Module Cohesion**: `Cohesion = 1 - Average(LCOM)`

---

## Implementation Details

### Module: `src/designmetrics.rs` (680 LOC)

**Data Structures**:
```rust
pub struct DesignMetrics {
    pub modules: HashMap<String, ModuleMetrics>,
    pub overall_stats: OverallStats,
}

pub struct ModuleMetrics {
    pub afferent_coupling: usize,      // Ca
    pub efferent_coupling: usize,      // Ce
    pub instability: f64,              // I
    pub abstractness: f64,             // A
    pub distance_from_main: f64,       // D
    pub cohesion: f64,                 // 1 - LCOM
    pub classes: Vec<ClassMetrics>,
    pub dependencies: Vec<String>,
    pub dependents: Vec<String>,
}

pub struct ClassMetrics {
    pub methods: Vec<String>,
    pub fields: Vec<String>,
    pub method_field_usage: HashMap<String, HashSet<String>>,
    pub lcom: f64,
}
```

**Analysis Process**:
1. **First Pass**: Scan all files, extract dependencies
2. **Calculate Ce**: Count dependencies for each module
3. **Second Pass**: Calculate Ca by finding reverse dependencies
4. **Calculate I**: Apply formula I = Ce / (Ca + Ce)
5. **Extract Classes**: Parse class structures
6. **Calculate LCOM**: Analyze method-field usage
7. **Calculate Cohesion**: Average LCOM across classes
8. **Overall Stats**: Aggregate metrics across all modules

---

## CLI Usage

### Basic Analysis
```bash
codesearch design-metrics .
```

### With Filters
```bash
# Specific extensions
codesearch design-metrics . -e rs,py,js

# Exclude directories
codesearch design-metrics . --exclude target,node_modules

# Detailed output
codesearch design-metrics . --detailed

# JSON output
codesearch design-metrics . --format json
```

---

## Output Examples

### Summary Output
```
Design Metrics Analysis
============================================================

Overall Statistics:
  Total modules: 25
  Average afferent coupling (Ca): 2.4
  Average efferent coupling (Ce): 3.1
  Average instability (I): 0.56
  Average cohesion: 0.78

⚠️  Highly Coupled Modules:
  - core_module (Ca=8, Ce=10)
  - utils (Ca=12, Ce=3)

⚠️  Unstable Modules (I > 0.7):
  - ui_component (I = 0.85)
  - temp_handler (I = 0.92)

⚠️  Low Cohesion Modules:
  - mixed_utils (cohesion = 0.35)

🔴 Critical Modules (Ca > 5):
  - core_module (Ca = 8)
  - utils (Ca = 12)

✅ Stable Modules (I < 0.3):
  - database (I = 0.15)
  - config (I = 0.22)
```

### Detailed Module Output
```
Module: core_module
  File: src/core/module.rs
  Afferent Coupling (Ca): 8 modules depend on this
  Efferent Coupling (Ce): 3 dependencies
  Instability (I): 0.27
  Abstractness (A): 0.60
  Distance from Main: 0.13
  Cohesion: 0.85
  Dependents: ui, api, service, worker, cache, logger, metrics, monitor
  Dependencies: config, database, utils
  Classes:
    - CoreService (LCOM: 0.15, methods: 12, fields: 8)
    - CoreHandler (LCOM: 0.20, methods: 8, fields: 5)
```

---

## Interpretation Guide

### Ideal Module Characteristics

**Stable Core Modules** (Infrastructure):
- Low Instability (I < 0.3)
- High Abstractness (A > 0.5)
- High Afferent Coupling (Ca > 5)
- Low Efferent Coupling (Ce < 3)
- High Cohesion (> 0.7)

**Flexible Application Modules** (UI/Business Logic):
- High Instability (I > 0.7)
- Low Abstractness (A < 0.5)
- Low Afferent Coupling (Ca < 3)
- Moderate Efferent Coupling (Ce 3-7)
- High Cohesion (> 0.7)

### Warning Signs

**🔴 Critical Issues**:
- Ca > 10: Too many dependents (refactor into smaller modules)
- Ce > 10: Too many dependencies (violates SRP)
- I > 0.9 with Ca > 5: Unstable but critical (dangerous)
- Cohesion < 0.3: Poor class design (split classes)
- D > 0.7: Far from main sequence (architectural problem)

**⚠️ Moderate Issues**:
- Ca > 5: Critical module (needs careful testing)
- Ce > 5: High coupling (consider dependency injection)
- I > 0.7: Unstable (acceptable for UI/high-level code)
- Cohesion < 0.5: Low cohesion (review class responsibilities)

**✅ Good Indicators**:
- 0.2 < I < 0.6: Balanced stability
- Cohesion > 0.7: Well-designed classes
- D < 0.3: Near main sequence
- Ca + Ce < 8: Reasonable coupling

---

## Use Cases

### 1. Refactoring Prioritization
```bash
codesearch design-metrics . --detailed
```
Focus on:
- Modules with high coupling (Ca + Ce > 15)
- Modules with low cohesion (< 0.5)
- Modules far from main sequence (D > 0.5)

### 2. Impact Analysis
```bash
codesearch design-metrics . | grep "Critical Modules"
```
Before changing a module, check its Ca:
- Ca > 5: High impact change (extensive testing needed)
- Ca < 2: Low impact change (safer to modify)

### 3. Architecture Validation
```bash
codesearch design-metrics . --format json > metrics.json
```
Validate architectural principles:
- Core modules should be stable (I < 0.3)
- UI modules should be unstable (I > 0.7)
- All modules should have good cohesion (> 0.6)

### 4. Code Review
Check new modules:
- Ensure reasonable coupling (Ca + Ce < 10)
- Verify good cohesion (> 0.6)
- Check stability matches layer (core=stable, UI=unstable)

---

## Integration with Other Metrics

### Combined Analysis
```bash
# Design + Complexity
codesearch design-metrics . --detailed
codesearch complexity . --threshold 10

# Design + Dependencies
codesearch design-metrics .
codesearch depgraph . --circular-only

# Design + Dead Code
codesearch design-metrics .
codesearch deadcode .
```

### Correlation Insights
- **High Ce + High Complexity**: Overly complex module
- **High Ca + Dead Code**: Critical but unused code
- **Low Cohesion + Duplicates**: Poor abstraction
- **High I + Circular Deps**: Architectural issues

---

## Language Support

**Full Support** (with class/method extraction):
- Rust (struct, enum, trait, impl)
- Python (class, def)
- JavaScript/TypeScript (class, function)
- Java/Kotlin (class, interface, method)
- Go (struct, func)

**Partial Support** (dependency analysis only):
- C/C++ (header dependencies)
- Other languages (import/use statements)

---

## Performance

**Analysis Speed**:
- Small projects (<100 files): < 1 second
- Medium projects (100-1000 files): 1-5 seconds
- Large projects (1000-10000 files): 5-30 seconds

**Memory Usage**:
- Proportional to number of modules and classes
- Typical: 10-50 MB for medium projects

---

## Limitations

1. **Dynamic Dependencies**: Cannot track runtime/reflection-based dependencies
2. **Indirect Coupling**: Only measures direct dependencies
3. **Language Features**: Limited support for advanced features (macros, generics)
4. **Heuristic-Based**: Uses pattern matching, not full semantic analysis
5. **Single Codebase**: Doesn't track external library dependencies

---

## Best Practices

### For Stable Modules (Core/Infrastructure)
- Keep I < 0.3 (stable)
- Maximize A (use traits/interfaces)
- Minimize Ce (few dependencies)
- Accept high Ca (many dependents is OK)

### For Unstable Modules (UI/Application)
- Allow I > 0.7 (unstable is OK)
- Keep A low (concrete implementations)
- Manage Ce (reasonable dependencies)
- Keep Ca low (few dependents)

### For All Modules
- Maintain cohesion > 0.6
- Keep Ca + Ce < 10
- Stay near main sequence (D < 0.3)
- Regular monitoring and refactoring

---

## Testing

**Unit Tests**: 6 tests covering:
- Module metrics creation
- Instability calculation
- LCOM calculation
- Critical module detection
- Zero coupling edge cases

Run tests:
```bash
cargo test designmetrics
```

---

## Future Enhancements

1. **Temporal Analysis**: Track metrics over time
2. **Threshold Configuration**: Customizable warning levels
3. **Visualization**: Generate coupling/cohesion graphs
4. **Recommendations**: Automated refactoring suggestions
5. **Team Metrics**: Per-developer/team coupling analysis
6. **CI Integration**: Fail builds on metric violations

---

## References

- **Martin, Robert C.** - "Clean Architecture" (Stability metrics)
- **Chidamber & Kemerer** - "A Metrics Suite for Object-Oriented Design" (LCOM)
- **ISO/IEC 25010** - Software Quality Model
- **Fowler, Martin** - "Refactoring" (Code smells related to coupling)

---

## Conclusion

Design metrics provide quantitative measures of software architecture quality:

✅ **Afferent Coupling (Ca)** - Measures module criticality  
✅ **Efferent Coupling (Ce)** - Measures dependency burden  
✅ **Instability (I)** - Measures flexibility vs stability  
✅ **Abstractness (A)** - Measures abstraction level  
✅ **Distance (D)** - Measures architectural health  
✅ **Cohesion** - Measures class design quality  

Use these metrics to:
- Identify refactoring priorities
- Assess architectural health
- Guide design decisions
- Monitor technical debt
- Improve code maintainability