Code Smell
Also Known As
code smells
anti-pattern indicator
refactoring trigger
TL;DR
A surface indication in code that usually corresponds to a deeper design problem — not a bug, but a maintainability risk.
Explanation
The term was popularised by Martin Fowler in "Refactoring". A code smell is not necessarily broken — the code may work — but it suggests structural issues that will make the code harder to change safely over time. Common smells: long methods, large classes, long parameter lists, feature envy, data clumps, primitive obsession, and duplicate code. Smells are signals to investigate, not mandates to refactor immediately.
Common Misconception
✗ A code smell means the code is definitely wrong and must be fixed immediately. Smells are indicators that warrant investigation — context matters. A long method in a performance-critical parser may be intentional and correct.
Why It Matters
Code smells are early warning signs of design problems that compound over time — ignoring them leads to systems where every change risks breaking something else. Addressing smells proactively is ten times cheaper than refactoring under pressure.
Common Mistakes
- Treating all code smells as equally urgent — prioritise smells in frequently-changed, business-critical code.
- Refactoring smells without tests in place — you need a safety net before restructuring.
- Conflating code style violations (formatting) with structural smells — they require different remediation.
- Using smell detection tools as a pass/fail gate rather than a signal to investigate.
Code Examples
✗ Vulnerable
// Multiple smells in one function:
function p(\$d) { // cryptic name
global \$db; // global state
if(\$d['t']==1) { // magic number
\$r = \$db->query("SELECT * FROM orders WHERE uid=".\$d['u']); // SQL injection
foreach(\$r as \$row) { echo \$row['total'] * 0.9; } // magic number
}
}
✓ Fixed
function applyLoyaltyDiscount(array \$request): void {
\$orders = \$this->orderRepo->findByUserId(\$request['user_id']);
\$discount = LoyaltyDiscount::RATE; // named constant
foreach (\$orders as \$order) {
echo \$order->total * (1 - \$discount);
}
}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
13 Mar 2026
Edited
22 Mar 2026
Views
39
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Perplexity 10
Amazonbot 6
Ahrefs 6
Unknown AI 3
SEMrush 2
Majestic 1
Google 1
Also referenced
How they use it
crawler 28
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Medium
⚡ Quick Fix
Use the catalogue: long method, god class, duplicate code, feature envy, data clumps, primitive obsession, switch statements, parallel inheritance, lazy class, speculative generality
📦 Applies To
any
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Long methods; duplicated code blocks; deeply nested conditionals; large classes with many responsibilities
Auto-detectable:
✓ Yes
phpmd
phpcs
phpstan
rector
phpcpd
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: High
✗ Manual fix
Fix: Medium
Context: File
Tests: Update