Code Quality Scanning

Codity scans pull request diffs for code quality issues and posts findings as review comments. Scanning runs in two phases: a fast pattern-based pass followed by an optional LLM-based analysis.

Overview

Every time a PR is opened or updated, Codity analyzes the changed lines and reports issues across eight quality categories:

  • Complexity: oversized functions, deep nesting, high cyclomatic complexity
  • Duplication: copy-pasted blocks, DRY violations
  • Error handling: bare except, swallowed exceptions, missing null checks
  • Performance: N+1 queries, blocking I/O in async contexts, unnecessary loops
  • Documentation: missing docstrings or JSDoc for public APIs
  • Testability: global state mutation, hard-coded dependencies
  • Maintainability: magic numbers, poorly named variables, dead code
  • Style: lines exceeding 120 characters

Two-Phase Analysis

Phase 1: Pattern Detection

Phase 1 runs on every PR using static pattern matching. It is fast and deterministic.

RuleCategorySeverityWhat it checks
CQ-001ComplexityHighFunctions adding more than 50 lines
CQ-002ComplexityMediumNesting depth of 5 or more levels
CQ-003Error HandlingHighBare except or empty catch blocks
CQ-004Error HandlingHighException caught and swallowed with pass
CQ-005MaintainabilityLowTODO, FIXME, or HACK comments
CQ-006PerformanceMediumSELECT * or .all() called inside a loop
CQ-007DocumentationLowPublic function missing a docstring
CQ-008MaintainabilityLowMagic numbers used directly in code
CQ-009StyleLowLines longer than 120 characters
CQ-010TestabilityMediumGlobal variable mutation
CQ-011DuplicationLowDuplicate import statements
CQ-012PerformanceHighSynchronous I/O (open, requests.get, sleep) in an async context

Phase 2: LLM Analysis

Phase 2 uses an LLM to review the same diff with deeper reasoning. It catches nuanced issues that pattern matching misses, such as subtle logic errors, architectural concerns, and context-dependent duplication.

Phase 2 is available when a custom LLM API key (BYOK) is configured. Without BYOK, Codity scans up to 10 files per PR using its built-in model. With BYOK, up to 50 files are scanned.

Quality Score

Every scan produces a score from 0 to 100. It starts at 100 and decays based on the number and severity of findings.

Penalty Weights

SeverityPenalty per finding
Critical10
High5
Medium2
Low / Info0.5

Formula

penalty = (critical × 10) + (high × 5) + (medium × 2) + (low × 0.5)
score   = max(0, round(100 × (1 / (1 + penalty / 20))))

This is a harmonic decay, not linear subtraction. Here is why each part exists:

  • 1 + — prevents division by zero when penalty is 0, and sets the clean baseline. With zero findings the denominator is 1, giving 100 / 1 = 100. Every unit of penalty is measured relative to that 1.
  • penalty / 20 — the 20 is the pivot point. When total penalty equals 20, the denominator becomes 2 and the score halves to 50. This means two critical issues (or four high issues) put you at exactly 50.
  • Harmonic decay — each additional finding hurts less than the previous one. Going from 0 to 1 critical issue drops the score by 33 points. Going from 10 to 11 critical issues drops it by roughly 1 point. This reflects reality: the gap between a clean codebase and one with a single critical issue is far more meaningful than the gap between a very poor codebase and a slightly worse one.
  • max(0, ...) — a safety floor. The formula asymptotically approaches zero but never actually reaches it, so this guard exists only for floating-point edge cases.

Example scores

FindingsScore
None100
1 critical67
2 critical50
1 critical + 2 high43
4 high + 5 medium33

Finding Severity Levels

Each finding is assigned one of five severity levels:

SeverityDescription
CriticalMust be fixed before merge
HighSignificant quality or correctness risk
MediumShould be addressed soon
LowMinor quality concern
InfoInformational, no action required

Whole-Repository Scanning

In addition to PR-level scanning, Codity supports scanning your entire repository. Whole-repository scans require a custom LLM API key and scan up to 50 files at a time.

To enable whole-repository scanning:

  1. Go to Settings in the Codity dashboard.
  2. Enable Whole Repository Scan.
  3. Enter your custom LLM API key and select the provider (OpenAI, Anthropic, or Google).

Bring Your Own Key (BYOK)

To configure BYOK:

  1. Go to Settings in the Codity dashboard.
  2. Enter your API key under Custom LLM API Key.
  3. Select the provider.
  4. Save settings.

Downloading Reports

Quality scan reports can be downloaded from the dashboard in two formats:

  • Markdown (.md): human-readable report
  • JSON (.json): machine-readable for integration with other tools

Next Steps