Code Smell
- Pronunciation
- KOHD SMEL
In short
A code smell is a surface sign in code that often points to a deeper design problem, even though the code still works.
What is a code smell?
The term was coined by Kent Beck and made widely known by Martin Fowler's book Refactoring, published in 1999, which catalogued smells and the refactorings that remove them. A smell isn't a bug: the program may run perfectly. It is a hint that the code will be hard to understand, change or test, and that a closer look is worthwhile.
Common smells include long methods and large classes that do too many things; duplicated code that must be fixed in several places; long parameter lists; feature envy, where a method uses another class's data more than its own; shotgun surgery, where one change forces edits in many files; primitive obsession, using plain strings and numbers instead of small types; and magic numbers with no name explaining them.
Each smell suggests specific refactorings: extract a function, introduce a parameter object, move a method to the class whose data it uses, replace a magic number with a named constant. Linters and tools such as SonarQube flag some smells automatically, and code review is where most of them are noticed and discussed.
A common misconception is that every smell must be removed. Smells are heuristics, not rules: a long function that reads clearly from top to bottom may be better left alone, and removing duplication too early can create the wrong abstraction. The point is to notice the signal and decide deliberately, ideally with tests in place before refactoring.
Key takeaways
- A code smell is a sign of a likely design problem, not a bug.
- Kent Beck coined the term; Fowler's Refactoring (1999) popularized it.
- Long methods, duplication and long parameter lists are classic smells.
- Each smell suggests specific refactorings to fix it.
- Smells are heuristics: judge each one in context.
Example
// Smells: magic numbers, duplicated logic, long parameter list
function price(base, isMember, isHoliday, country, couponCode, quantity) {
let total = base * quantity;
if (isMember) total = total * 0.9;
if (isHoliday) total = total * 0.9;
if (country === "TR") total = total * 1.2;
if (country === "DE") total = total * 1.19;
return total;
}
// Refactored: named constants, one option object, a lookup table
const MEMBER_DISCOUNT = 0.9;
const HOLIDAY_DISCOUNT = 0.9;
const VAT = { TR: 1.2, DE: 1.19 };
function priceFor({ base, quantity, member = false, holiday = false, country }) {
let total = base * quantity;
if (member) total *= MEMBER_DISCOUNT;
if (holiday) total *= HOLIDAY_DISCOUNT;
return total * (VAT[country] ?? 1);
}Readers ask
Is a code smell a bug?
No. Code with smells can work correctly. A smell indicates that the code may be hard to maintain or extend, which makes future bugs more likely.
What are the most common code smells?
Long methods, large classes, duplicated code, long parameter lists, feature envy, shotgun surgery, primitive obsession, magic numbers, dead code and comments that explain confusing code instead of the code being made clearer.
How do you fix a code smell?
With a matching refactoring, such as extracting a function, introducing a named constant or moving a method, done in small steps with tests to confirm the behavior doesn't change.
See also
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- Technical DebtSoftware Architecture, p. 45Technical debt is the future cost of extra work created when developers choose a quick or limited solution now instead of a better approach that takes longer.
- Code ReviewVersion Control, p. 4A code review is the practice of having other developers check code changes before they are merged, to catch bugs, improve quality, and share knowledge.
- LintingTesting & Quality, p. 14Linting is the automated analysis of source code, without running it, to flag likely bugs, style problems, and suspicious patterns before the code ships.
- Static AnalysisTesting & Quality, p. 26Static analysis is the automated examination of source code without running it, to find bugs, security vulnerabilities, and quality problems early.
- DRYSoftware Architecture, p. 16DRY is a software design principle stating that every piece of knowledge or logic should have one authoritative representation instead of being duplicated.
Spotted a mistake or something missing on this page?Suggest an edit