# Code Crime Code Crime parses Git history and change data to construct analytical metrics about code quality and change impacts. It is a Rust implementation of the behavioral code-analysis methodology from Adam Tornhill's *Software Design X-Rays* (The Pragmatic Bookshelf, 2018): instead of judging code by how it looks, it judges it by how it is actually worked on. ## Core concept Version control is more than a backup — every commit records what changed, when it changed, and who changed it. Aggregated over a full history, this is a behavioral log of the organization, and it answers questions static analysis cannot: - **Where does maintenance effort actually go?** Change frequency follows a power law — a small fraction of files absorbs most of the work. - **Which code is complex *and* frequently changed?** That combination is a hotspot: the code with the highest refactoring payoff. - **Which files change together although nothing in the source connects them?** Change coupling reveals hidden dependencies and missing abstractions. - **Where are the coordination bottlenecks?** Authorship and knowledge fragmentation predict defects better than any code metric. ## How it works The pipeline has three stages: 1. **Ingest** — walk the commit graph with libgit2. For each commit, extract the files touched (with added/removed line counts), the commit dates, and the author identity, normalized through the repository's `.mailmap`. 2. **Materialize** — load the raw history into an embedded DuckDB database and pre-aggregate it: per-file change counts, co-change pairs, logical component groupings, per-author contributions. 3. **Analyze** — every metric is a SQL view or query over those tables, emitted as CSV/JSON or explored interactively with the DuckDB CLI. ## The stack | Component | Role | Why | | --- | --- | --- | | Rust | Implementation language | History mining is CPU- and memory-bound; a single static binary with predictable memory use fits the job, and libgit2's C API wraps cleanly through safe FFI. | | libgit2 | Git access | Structured in-process APIs (revision walk, diff, blame, historic blobs) instead of brittle parsing of CLI text output; no dependency on an installed git version. | | DuckDB | Metric engine | Every metric is an analytical aggregation — joins, windows, medians, co-occurrence counts — over the same raw layer. An embedded, single-file, columnar engine expresses all of it as plain, inspectable SQL that users can extend without recompiling the tool. | ## Metrics - **Change frequency** — how often each file or module is modified; the interest rate paid on technical debt. Badly written code you never touch charges no interest. - **Hotspots** — change frequency crossed with lines of code, raisable from single files up to architecturally meaningful components via glob/regex mapping. The top hotspots typically absorb 10–15% of all commits and are where refactoring effort pays off. - **Complexity trends** — how a hotspot's complexity evolved across its history, using language-neutral indentation-based complexity sampled per month. Trends — never absolute values — flag components heading for a tipping point. - **Change coupling** — files or methods that repeatedly change together in the same commits, filtered by the methodology's thresholds (at least 20 shared commits and at least half of either file's total commits) and by surprise: a test changing with its code is expected, two unrelated modules are a red flag. - **Clone similarity** — near-identical code regions that evolve together; a clone pair where only one side carries a conditional can be a latent bug by omission. - **Code age** — time since last modification, per file and as a median via blame. Recent and stable old code are healthy; mid-aged code — forgotten yet frequently revisited — carries the highest risk. - **Social metrics** — author counts per component, knowledge maps, and a fragmentation (fractal) value computed from contributed lines of code. High fragmentation correlates with defects and flags missing ownership. - **Multi-repository metrics** — repository-prefixed hotspots, cross-repo logical change sets grouped by author/day or ticket id, and a technology-sprawl index for microservice landscapes. - **Predictive metrics** — complexity-growth warnings against a reference state, rising hotspots, expected-co-change checks for CI, and knowledge-loss simulation before key people leave. ## Reliability caveats These analyses are heuristics that focus expert attention — never a replacement for it, and always read as trends rather than absolute numbers. Social data needs care: at least 150–200 commits of history for meaningful results, `.mailmap` applied for author identity, and distrust of migrated or squashed histories. Test code is included in every analysis: the worst hotspots and coupling problems often live in tests. ## Ethics Social metrics exist to prioritize reviews, tests, and refactorings — never for individual performance evaluation. Measuring people by commits invites meaningless commits and destroys the data source itself. This is an explicit hard line in the methodology and in this tool. ## Status Early development: the crate builds, the metric pipeline is not yet implemented. The build order follows the metric list above — change frequency, lines of code, and hotspots first, since every other analysis consumes them. ## Development ```sh podman build -t vibe-sandbox-rust:latest . # dev container image cargo build cargo test cargo clippy -- -D warnings cargo fmt --check ``` The Dockerfile/.devcontainer provide Rust (stable, minimal profile), the DuckDB CLI, and tooling; the dev container mounts the repo at `/workspace`.