← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Cohesion

quality Intermediate
debt(d5/e5/b7/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5). PHPStan and PHPMD (from detection_hints.tools) can flag some cohesion issues like god classes or excessive class size, but the detection_hints explicitly state automated=no — these tools catch symptoms (class size, method count) rather than true cohesion violations. Many low-cohesion designs pass static analysis and only surface through careful code review.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix suggests splitting a class 'along the and boundary' — this is rarely a one-line fix. Extracting responsibilities from a low-cohesion class typically requires creating new classes, moving methods, updating dependencies, and adjusting callers. For a god class, this can span multiple files.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (b7). Low cohesion accumulates as a decay pattern — common_mistakes notes that god classes 'accumulate behaviour because they are already imported'. Once a low-cohesion class becomes central, every new feature gravitates toward it. The applies_to shows this affects all PHP contexts (web, cli, queue-worker), and the architectural/oop tags indicate this shapes system structure. Refactoring away from a god class often means reshaping how the entire codebase is organized.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states developers wrongly believe 'a class with many methods is low-cohesion' when actually cohesion measures purpose alignment, not size. Common_mistakes reinforces the trap: confusing cohesion with coupling, and using line count as the sole metric. These are documented gotchas most developers eventually learn, but the initial intuition is often wrong.

About DEBT scoring →

Also Known As

module cohesion class cohesion high cohesion

TL;DR

The degree to which elements within a module belong together; high cohesion means a class does one thing and does it well.

Explanation

Cohesion measures how focused a module's responsibilities are. A highly cohesive class has all its methods and properties working toward a single, well-defined purpose. Low cohesion — methods that have little relation to each other — indicates the class is doing too many things and should be split (see God Class, Single Responsibility). High cohesion is desirable: it makes classes easier to understand, test, reuse, and maintain. Cohesion and coupling are inversely related — increasing cohesion typically reduces unnecessary coupling.

Common Misconception

A class with many methods is low-cohesion. Cohesion measures whether all methods serve the same purpose — a class can have 30 methods and be highly cohesive if they all operate on the same core concept.

Why It Matters

High cohesion means a class or module does one thing well — low cohesion classes mix unrelated responsibilities, making them harder to test, maintain, and reuse independently.

Common Mistakes

  • Adding helper methods to a class because it is convenient, not because they belong there — erodes cohesion over time.
  • God classes that accumulate behaviour because they are already imported — a symptom of low cohesion.
  • Confusing cohesion with coupling — they are related but distinct; a class can be highly cohesive and still tightly coupled.
  • Not refactoring when a class exceeds 200-300 lines — size is often a cohesion warning sign.

Code Examples

✗ Vulnerable
// Low cohesion — unrelated responsibilities jammed together
class Utils {
    public function formatDate(\DateTime $d): string { ... }
    public function sendEmail(string $to, string $body): void { ... }
    public function hashPassword(string $pw): string { ... }
    public function resizeImage(string $path, int $w): void { ... }
}
✓ Fixed
// High cohesion — each class has one tight purpose
class DateFormatter  { public function format(\DateTime $d): string { ... } }
class Mailer         { public function send(string $to, string $body): void { ... } }
class PasswordHasher { public function hash(string $pw): string { ... } }
class ImageResizer   { public function resize(string $path, int $w): void { ... } }

Added 15 Mar 2026
Edited 22 Mar 2026
Views 38
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 2 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S
No pings yet today
Perplexity 1
Perplexity 10 Amazonbot 7 Ahrefs 5 ChatGPT 4 Google 3 Unknown AI 2
crawler 30 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
If you struggle to describe what a class does without using 'and', it has low cohesion — split it along the 'and' boundary
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Class with methods operating on unrelated data sets; god class mixing UI, business logic, and data access
Auto-detectable: ✗ No phpstan phpmd
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Class Tests: Update

✓ schema.org compliant