cognitive-debt: Measure Code Readability Before It Becomes a Problem
Source: Dev.to
We’ve all been there
You find a file that “works perfectly” but nobody dares touch it.
The tests pass. There are no bugs. Yet the moment you try to refactor it, you realize why everyone avoids it—it’s impossible to understand.
That’s cognitive debt.
Cognitive Debt is the accumulated mental effort required to understand, predict, and safely modify a system, independent of its functional correctness.
A file can be:
- ✅ Syntactically correct
- ✅ Fully tested
- ✅ Following all linting rules
and still be mentally exhausting to work with.
Most code‑quality tools (ESLint, Prettier, SonarQube) focus on correctness or style. cognitive‑debt focuses on comprehension.
The Problem Most Tools Miss
Traditional tools measure:
- Syntax errors ❌
- Code‑style violations ❌
- Test coverage ❌
but they don’t measure:
- Can a human actually understand this?
- How long will it take to comprehend?
- Will changing this file break other files?
- Is this code getting more or less complex over time?
That’s the gap cognitive‑debt fills.
What We’re Announcing: 3 New Features
1️⃣ Change Impact Forecaster
Before you refactor a file, predict the risk.
cognitive-debt impact src/utils/auth.js
Sample output
Risk Level: HIGH
Ripple Effects: 12 files depend on this
Cognitive Load: 42/100 (Poor)
Actionable Advice:
→ This file is complex AND has high coupling
→ Write regression tests BEFORE making changes
→ Consider breaking it into 3 smaller modules
Why this matters
- See upfront if a refactor will be risky.
- Identify “fragile” files that pass tests but are hard to change.
- Make data‑driven decisions about refactoring strategy.
2️⃣ Cognitive Debt Diff
Track whether your code is improving or degrading over time.
Compare Git branches
cognitive-debt diff main..feature-branch
Sample output
⚠️ DEBT INCREASED
- Total Lines: +150
- Dependencies: +3
- Avg Function Length: +12 lines
Files Improved:
✅ src/handlers/auth.js (50→65, score improved)
✅ src/utils/validation.js (60→55, cleaner)
Files Degraded:
🚨 src/api/request.js (nesting depth 3→5)
🚨 src/middleware/logger.js (param count 4→7)
Why this matters
- Prevent cognitive debt from sneaking in.
- Developers can see their impact on code quality.
- Perfect for code‑review metrics.
- Track progress over sprints and quarters.
3️⃣ HTML Reports & JSON Export
Beautiful dashboards for teams + machine‑readable data for automation.
Generate an HTML dashboard
cognitive-debt src/ --output report.html

The report opens in your browser with:
- Score distribution across all files
- Interactive charts and visualizations
- Time‑to‑understand estimates
- Detailed breakdowns per file
- Recommended refactoring priorities
Generate JSON for automation
cognitive-debt src/ --output report.json
Use the JSON in:
- CI/CD pipelines (fail builds if cognitive debt > threshold)
- Custom dashboards (track trends over time)
- Slack bots (notify team of high‑debt files)
- Executive reports (quantify technical burden)
How It Works: The Math Behind It
cognitive‑debt measures 5 dimensions:
| # | Dimension | Weight | Description |
|---|---|---|---|
| 1 | Naming Clarity | 30 % | Vague names (data, tmp, obj, a, b) force the brain to hold mental mappings. Bad names are the #1 cause of confusion. |
| 2 | Nesting Depth | 10 % per level | Miller’s Law: the brain holds 7 ± 2 items. Deep nesting (5+ levels) exceeds that limit. |
| 3 | Parameter Count | 5 % per param | More parameters → exponentially harder to use correctly. |
| 4 | Function Length | 0.5 % per line | Long functions require constant context switching; you lose track of earlier logic. |
| 5 | Dependencies | 2 % per import | Importing many modules forces you to understand many files simultaneously. |
Nesting Depth Example
// Hard to understand (5 levels)
if (user) {
if (user.active) {
if (user.permissions) {
if (user.permissions.includes('admin')) {
if (request.isValid()) {
// Finally, we do something
}
}
}
}
}
// Easy to understand (flat)
if (!isAuthorizedAdmin(user, request)) return;
// Do something
Parameter Count Example
// Easy
function validateEmail(email) { /* … */ }
// Hard
function processUser(id, name, email, phone, role, permissions,
status, timestamp, metadata) { /* … */ }
The Transparency Principle
Unlike other tools (especially AI‑powered ones), cognitive‑debt uses simple, auditable math.
- No black boxes
- No ML magic
- No mysterious algorithms
- Just transparent formulas
You can verify the score yourself and even customize thresholds in .cognitivedebtrc.json for your team.
Real‑World Use Cases
For Code Reviews
Reviewer: "Why is this file hard to review?"
cognitive-debt: "50/100 (Fair). Main issues:
- 5 functions over 50 lines
- 7 levels of nesting in auth.js
- 40 % of variable names unclear (data, tmp, val)"
For Onboarding
New developers can see which files are hardest to understand before diving in.
For CI/CD Pipelines
# Fail the build if any file drops below 40/100
cognitive-debt src/ || exit 1
For Refactoring Decisions
Track whether your refactoring actually improved readability:
cognitive-debt diff v1 v2
For Team Metrics
Monthly dashboard showing code‑quality trends:
- Is average cognitive debt improving?
- Which files are becoming harder to maintain?
- What’s our onboarding burden?
Why We Built This
The Reality
- Developers spend 70 % of their time reading code, not writing it.
- Cognitive load is the #1 barrier to onboarding new developers.
- “Technical debt” is often really cognitive debt.
- Most tools ignore this completely.
Our Philosophy
- Transparency – You should understand the math.
- Privacy – Your code never leaves your machine.
- Education – Explain why, not just what.
- Empathy – Code is communication between humans.
Getting Started
Install globally
npm install -g cognitive-debt
Analyze your code
cognitive-debt src/
Generate reports
cognitive-debt src/ --output report.html
cognitive-debt src/ --output report.json
Predict refactoring risk
cognitive-debt impact src/utils/auth.js
Track improvements
cognitive-debt diff main..feature-branch
What Makes This Different
| Feature | ESLint/Prettier | Cyclomatic Complexity | SonarQube | cognitive‑debt |
|---|---|---|---|---|
| Measures | Syntax & Style | Logic paths | Everything | Readability |
| Goal | Consistency | Testability | Compliance | Comprehension |
| Output | Errors | Single number | Dashboard | Actionable insights |
| Philosophy | “Is it wrong?” | “Hard to test?” | “Compliant?” | “Hard to read?” |
Open Source & Community
cognitive-debt is open source on GitHub. We welcome:
- Feature requests
- Bug reports
- Contributions
- Custom analyzers
Links
- 📦 npm:
- 💻 GitHub:
- 🐛 Issues:
A Word of Caution
This tool is powerful, but use it wisely:
-
❌ Don’t use it to punish developers. Cognitive debt is often systemic, not personal.
-
❌ Don’t use it as a hard gate without discussion. Some problems legitimately require complex solutions.
-
❌ Don’t optimise for score alone. A perfect score with wrong logic is useless.
-
✅ Do use it to foster empathy for future maintainers.
-
✅ Do use it to identify risk before refactoring.
-
✅ Do use it to improve onboarding and developer experience.
The Quote
“The best code is code that doesn’t need to be read. The second‑best code is code that’s easy to read.”
Let’s make our codebases easier to understand. Try cognitive‑debt today.
Have questions? Want to contribute? Join us on GitHub!